|
| 1 | +--- |
| 2 | +title: Working with HTTP Cache Response Header |
| 3 | +description: Tutorial on how to work with HTTP Cache Response header in Fano Framework |
| 4 | +--- |
| 5 | + |
| 6 | +<h1 class="major">HTTP Cache Header</h1> |
| 7 | + |
| 8 | +## Adding HttP Cache Header |
| 9 | + |
| 10 | +To add http cache header ([RFC 7324](https://tools.ietf.org/html/rfc7234)), Fano Framework provides built-in [middleware](/middlewares) |
| 11 | +`TCacheControlMiddleware`. |
| 12 | + |
| 13 | +To add HTTP cache header, create middleware or register its factory class `TCacheControlMiddlewareFactory` within container. |
| 14 | + |
| 15 | +``` |
| 16 | +container.add( |
| 17 | + 'cacheControl', |
| 18 | + TCacheControlMiddlewareFactory.create().useETag() |
| 19 | +); |
| 20 | +``` |
| 21 | +and then, attach middleware to [route](/working-with-router) that will need to be added with HTTP cache header. |
| 22 | + |
| 23 | +``` |
| 24 | +router.get( |
| 25 | + '/', |
| 26 | + container['homeController'] as IRequestHandler |
| 27 | +).add(container['cacheControl'] as IMiddleware); |
| 28 | +``` |
| 29 | +## How it works |
| 30 | +When this middleware attached to route, additional steps is taken: |
| 31 | + |
| 32 | +- If method is not cacheable (not GET nor HEAD) then nothing is done else, |
| 33 | +- `Cache-Control` header is added to response header, |
| 34 | +- If ETag is used then MD5 hash of response body is computed and `ETag` header is added to response with hash value of response body. |
| 35 | +- If request contains `If-None-Match` header, its value is compared with ETag value, if they are matched or `If-Non-Match` equals `"*"`, then response is considered not modified. |
| 36 | +- If response contains header `Last-Modified` and request contains header `If-Modified-Since`, both headers values are compared. If `Last-Modified` is older then response is considered not modified. |
| 37 | +- If response is not modified, instance `TNotModifiedResponse` is returned instead. Read [Not modified response](/working-with-response#not-modified-response) section for more information this class. |
| 38 | + |
| 39 | +## Setting Cache-Control header value |
| 40 | + |
| 41 | +### Set type |
| 42 | +`cacheType()` method set type of cache, accepts value of `ctPrivate` or `ctPublic` value. If it is not set `ctPrivate` is assumed. |
| 43 | +``` |
| 44 | +container.add( |
| 45 | + 'cacheControl', |
| 46 | + TCacheControlMiddlewareFactory.create().cacheType(ctPublic) |
| 47 | +); |
| 48 | +``` |
| 49 | + |
| 50 | +### Prevent cache to be stored |
| 51 | +`noStore()` method enable `no-store` value. |
| 52 | +``` |
| 53 | +container.add( |
| 54 | + 'cacheControl', |
| 55 | + TCacheControlMiddlewareFactory.create().noStore() |
| 56 | +); |
| 57 | +``` |
| 58 | +### Set max-age value |
| 59 | +`maxAge()` method set `max-age` value. |
| 60 | +``` |
| 61 | +container.add( |
| 62 | + 'cacheControl', |
| 63 | + //set max-age=3600 (1 hour) |
| 64 | + TCacheControlMiddlewareFactory.create().maxAge(60*60) |
| 65 | +); |
| 66 | +``` |
| 67 | +if max-age is set with 0 value, it implies `no-cache`. |
| 68 | + |
| 69 | +### Enable ETag header |
| 70 | +`useETag()` method enable `ETag` header. When enable, `ETag` response header is added to response with value equals to MD5 hash of response body. |
| 71 | + |
| 72 | +``` |
| 73 | +container.add( |
| 74 | + 'cacheControl', |
| 75 | + TCacheControlMiddlewareFactory.create().useETag() |
| 76 | +); |
| 77 | +``` |
| 78 | +### Set must-revalidate |
| 79 | +`mustRevalidate` method enable `must-revalidate` value. |
| 80 | + |
| 81 | +``` |
| 82 | +container.add( |
| 83 | + 'cacheControl', |
| 84 | + TCacheControlMiddlewareFactory.create().mustRevalidate() |
| 85 | +); |
| 86 | +``` |
| 87 | + |
| 88 | +All methods above can be chained, |
| 89 | +``` |
| 90 | +container.add( |
| 91 | + 'cacheControl', |
| 92 | + TCacheControlMiddlewareFactory.create() |
| 93 | + .cacheType(ctPublic) |
| 94 | + .mustRevalidate() |
| 95 | + .useETag() |
| 96 | +); |
| 97 | +``` |
| 98 | + |
| 99 | +## Explore more |
| 100 | + |
| 101 | +- [Working with Request](/working-with-request) |
| 102 | +- [Working with Response](/working-with-response) |
| 103 | +- [Middlewares](/middlewares) |
0 commit comments