@@ -112,6 +112,32 @@ curl https://myserver.dev/user?force=true # BYPASS (skip cache copy)
112112
113113In that case, the ` x-cache-status ` will reflect a ` 'BYPASS' ` value.
114114
115+ Additionally, you can configure a stale ttl:
116+
117+ ``` js
118+ const cacheableResponse = require (' cacheable-response' )
119+
120+ const ssrCache = cacheableResponse ({
121+ get : ({ req, res }) => ({
122+ data: doSomething (req),
123+ ttl: 86400000 , // 24 hours
124+ staleTtl: 3600000 // 1h
125+ }),
126+ send : ({ data, res, req }) => res .send (data)
127+ })
128+ ```
129+
130+ The stale ttl maximizes your cache HITs, allowing you to serve a no fresh cache copy _ while_ doing revalidation on the background.
131+
132+ ``` bash
133+ curl https://myserver.dev/user # MISS (first access)
134+ curl https://myserver.dev/user # HIT (served from cache)
135+ curl https://myserver.dev/user # STALE (23 hours later, background revalidation)
136+ curl https://myserver.dev/user? force=true # HIT (fresh cache copy for the next 24 hours)
137+ ```
138+
139+ The library provides enough good sensible defaults for most common scenarios and you can tune these values based on your use case.
140+
115141## API
116142
117143### cacheableResponse([ options] )
@@ -165,13 +191,6 @@ async function get ({ req, res }) {
165191}
166192```
167193
168- ##### getKey
169-
170- Type: ` function ` <br />
171- Default: ` ({ req }) => normalizeUrl(req.url) `
172-
173- It determinates how the cache key should be computed, receiving ` req, res ` as input.
174-
175194The method will received ` ({ req, res }) ` and it should be returns:
176195
177196- ** data** ` object ` |` string ` : The content to be saved on the cache.
@@ -182,6 +201,13 @@ Any other property can be specified and will passed to `.send`.
182201
183202In case you want to bypass the cache, preventing caching a value (e.g., when an error occurred), you should return ` undefined ` or ` null ` .
184203
204+ ##### key
205+
206+ Type: ` function ` <br />
207+ Default: ` ({ req }) => req.url) `
208+
209+ It determinates how the cache key should be computed, receiving ` req, res ` as input.
210+
185211##### send
186212
187213_ Required_ <br />
0 commit comments