Skip to content

Commit 8f8a352

Browse files
committed
update documentation
1 parent c07f933 commit 8f8a352

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

docs/README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,49 @@ API endpoints also use the Symfony serializer, so you can accept complex types a
181181
parameters and body parameters. Note that API endpoints are expected to begin with `/api/`. This is currently a
182182
hard-coded requirement; please open an issue if you have a use-case that requires this to be configurable.
183183

184+
## Caching
185+
186+
`Cache-Control` headers are automatically handled for you by the framework if you use a few attributes. When deciding
187+
which header to send, it will use the least amount of caching time.
188+
For example, consider these two dependent components:
189+
190+
```php
191+
#[\Bottledcode\SwytchFramework\Cache\UserSpecific]
192+
#[\Bottledcode\SwytchFramework\Cache\MaxAge(120)]
193+
#[\Bottledcode\SwytchFramework\Cache\Revalidate(\Bottledcode\SwytchFramework\Cache\RevalidationEnum::EveryRequest)]
194+
#[\Bottledcode\SwytchFramework\Template\Attributes\Component('delivery')]
195+
class Delivery {
196+
public function render() {
197+
return "<status />";
198+
}
199+
}
200+
201+
#[\Bottledcode\SwytchFramework\Cache\UserSpecific]
202+
#[\Bottledcode\SwytchFramework\Cache\MaxAge(500)]
203+
#[\Bottledcode\SwytchFramework\Cache\Revalidate(\Bottledcode\SwytchFramework\Cache\RevalidationEnum::WhenStale)]
204+
#[\Bottledcode\SwytchFramework\Template\Attributes\Component('status')]
205+
class Status {}
206+
```
207+
208+
In this case, the `Cache-Control` header will be `private max-age=120 no-cache` to reflect the most restrictive caching.
209+
210+
Here's a full listing of supported attributes:
211+
212+
| name | description |
213+
|--------------|----------------------------------------------------------------------|
214+
| CachePublic | Allows proxy/CDN servers to cache the response (default) |
215+
| MaxAge | Specify how many seconds a response should be considered 'fresh' for |
216+
| NeverCache | Never cache the response |
217+
| Revalidate | Specify how clients should validate their cached data |
218+
| UserSpecific | Prevent proxy/CDN servers from caching the response |
219+
220+
## Etags
221+
222+
Further, for API requests,
223+
any class that implements `\Bottledcode\SwytchFramework\Cache\Control\GeneratesEtagInterface` can return an array of
224+
strings that will be used to generate an ETAG for that request, before the request is executed.
225+
A `304` will be returned if the ETAG matches and the API handler won't be called.
226+
184227
## Context-Aware Escaping
185228

186229
The Swytch Framework automatically escapes all output inside `{` brackets `}` in your output. It is fully context-aware,
@@ -544,6 +587,6 @@ component.
544587

545588
# Unit Testing
546589

547-
Unit testing components is a first-class citizen in the Swytch Framework.
590+
Unit testing components is a first-class citizen in the Swytch Framework.
548591
We suggest using `pestphp/pest` along with `spatie/pest-plugin-snapshots` to use snapshot testing.
549592
The template repository has some excellent examples of how to set this up using the Streaming Compiler.

src/Cache/CachePublic.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Bottledcode\SwytchFramework\Cache\Control\Tokenizer;
66

7+
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_CLASS)]
78
readonly class CachePublic extends AbstractCache
89
{
910
public function tokenize(Tokenizer $tokenizer): Tokenizer

0 commit comments

Comments
 (0)