Skip to content

Commit 8a9eba7

Browse files
committed
feat: add EXPIRED cache state
1 parent 2ab79cf commit 8a9eba7

4 files changed

Lines changed: 30 additions & 11 deletions

File tree

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ Instead of serving a real time™ – and costly – response, we can say it is
1616

1717
That will save CPU cycles, saving them for things that really matters.
1818

19+
## Caching states
20+
21+
| Value | Description |
22+
|----------|-------------------------------------------------------------------------------------------------------------------|
23+
| `MISS` | The resource was looked into the cache but did not find it, so a new copy is generated and placed into the cache. |
24+
| `HIT` | The resources was found into the cache, being generated by a previous access. |
25+
| `EXPIRED`| The resouce was found but it is expired, being necessary regerate it. |
26+
| `BYPASS` | The cache is forced to be bypassed, regenerating the resource. |
27+
| `STALE` | The resource is expired but it's served while a new cache copy is generated in background. |
28+
1929
## Install
2030

2131
```bash
@@ -298,8 +308,7 @@ x-cache-expired-at: 1h 59m 60s
298308
cf-cache-status: HIT
299309
```
300310

301-
Note how in this second request `x-cache-status` is still
302-
`MISS`.
311+
Note how in this second request `x-cache-status` is still a `MISS`.
303312

304313
That's because CloudFlare way for caching the content includes caching the response headers.
305314

src/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,15 @@ const cacheableResponse = ({
7979
})
8080

8181
setHeaders({
82-
etag,
83-
res,
8482
createdAt,
83+
etag,
84+
forceExpiration,
85+
hasValue,
8586
isHit,
8687
isStale,
87-
ttl,
88+
res,
8889
staleTtl,
89-
forceExpiration
90+
ttl
9091
})
9192

9293
if (!forceExpiration && !isModified) {

src/util.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,22 @@ const createKey = bypassQueryParameter => ({ req }) => {
3232

3333
const toSeconds = ms => Math.floor(ms / 1000)
3434

35-
const getStatus = ({ isHit, isStale, forceExpiration }) =>
36-
isHit ? (isStale ? 'STALE' : 'HIT') : forceExpiration ? 'BYPASS' : 'MISS'
35+
const getStatus = ({ hasValue, isHit, isStale, forceExpiration }) =>
36+
isHit
37+
? isStale
38+
? 'STALE'
39+
: 'HIT'
40+
: forceExpiration
41+
? 'BYPASS'
42+
: hasValue
43+
? 'EXPIRED'
44+
: 'MISS'
3745

3846
const setHeaders = ({
3947
createdAt,
4048
etag,
4149
forceExpiration,
50+
hasValue,
4251
isHit,
4352
isStale,
4453
res,
@@ -60,7 +69,7 @@ const setHeaders = ({
6069
res.setHeader('Cache-Control', cacheControl)
6170
res.setHeader(
6271
'X-Cache-Status',
63-
getStatus({ isHit, isStale, forceExpiration })
72+
getStatus({ hasValue, isHit, isStale, forceExpiration })
6473
)
6574
res.setHeader('ETag', etag)
6675
}

test/status.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ test('MISS for undefined data value', async t => {
4141
t.is((await got(`${url}/kikobeats`)).headers['x-cache-status'], 'MISS')
4242
})
4343

44-
test('MISS after cache expiration', async t => {
44+
test('EXPIRED after cache expiration', async t => {
4545
const url = await createServer(
4646
cacheableResponse({
4747
staleTtl: false,
@@ -62,7 +62,7 @@ test('MISS after cache expiration', async t => {
6262
const doRequest = () => got(`${url}/kikobeats`)
6363

6464
t.is((await doRequest()).headers['x-cache-status'], 'MISS')
65-
t.is((await doRequest()).headers['x-cache-status'], 'MISS')
65+
t.is((await doRequest()).headers['x-cache-status'], 'EXPIRED')
6666
})
6767

6868
test('BYPASS for forcing refresh', async t => {

0 commit comments

Comments
 (0)