Skip to content

Commit b81928d

Browse files
committed
adding cache-control feature documentation
1 parent f9706f2 commit b81928d

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,32 @@ service.get('/numbers', (req, res) => {
7777
})
7878
```
7979

80-
### Invalidating caches
80+
### Caching on the browser side (304 status codes)
81+
> From version `1.2.x` you can also use the HTTP compatible `Cache-Control` header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
82+
When using the Cache-Control header, you can omit the custom `x-cache-timeout` header as the timeout can be passed using the `max-age` directive.
83+
84+
#### Direct usage:
85+
```js
86+
res.setHeader('cache-control', `private, no-cache, max-age=${60 * 5}`)
87+
res.setHeader('etag', '1')
88+
89+
res.end('5 minutes cacheable content here....')
90+
```
91+
92+
#### Indirect usage:
93+
When using:
94+
```js
95+
res.setHeader('x-cache-timeout', '5 minutes')
96+
```
97+
The middleware will now transparently generate default `Cache-Control` and `ETag` headers as described below:
98+
```js
99+
res.setHeader('cache-control', `private, no-cache, max-age=300`)
100+
res.setHeader('etag', '1')
101+
```
102+
This will enable browser clients to keep a copy of the cache on their side, but still being forced to validate
103+
the cache state on the server before using the cached response, therefore supporting gateway based cache invalidation.
104+
105+
### Invalidating caches
81106
Services can easily expire cache entries on demand, i.e: when the data state changes. Here we use the `x-cache-expire` header to indicate the cache entries to expire using a matching pattern:
82107
```js
83108
res.setHeader('x-cache-expire', '*/numbers')

0 commit comments

Comments
 (0)