koa-cash 4.1.0 → 4.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -7
- package/index.js +7 -0
- package/package.json +2 -9
package/README.md
CHANGED
|
@@ -27,9 +27,9 @@ Table of Contents
|
|
|
27
27
|
* [`get()`](#get)
|
|
28
28
|
* [`set()`](#set)
|
|
29
29
|
* [Example](#example)
|
|
30
|
-
* [Max age](#max-age)
|
|
30
|
+
* [Max age (optional)](#max-age-optional)
|
|
31
|
+
* [CashClear](#cashclear)
|
|
31
32
|
* [Notes](#notes)
|
|
32
|
-
* [Usage](#usage-1)
|
|
33
33
|
* [Contributors](#contributors)
|
|
34
34
|
* [License](#license)
|
|
35
35
|
* [Links](#links)
|
|
@@ -174,9 +174,11 @@ app.use(koaCash({
|
|
|
174
174
|
|
|
175
175
|
See [@ladjs/koa-cache-responses](https://github.com/ladjs/koa-cache-responses) test folder more examples (e.g. Redis with `ioredis`).
|
|
176
176
|
|
|
177
|
-
### Max age
|
|
177
|
+
### Max age (optional)
|
|
178
178
|
|
|
179
|
-
|
|
179
|
+
```js
|
|
180
|
+
const cached = await ctx.cashed(maxAge) // maxAge is passed to your caching strategy
|
|
181
|
+
```
|
|
180
182
|
|
|
181
183
|
This is how you enable a route to be cached. If you don't call `await ctx.cashed()`, then this route will not be cached nor will it attempt to serve the request from the cache.
|
|
182
184
|
|
|
@@ -184,14 +186,20 @@ This is how you enable a route to be cached. If you don't call `await ctx.cashed
|
|
|
184
186
|
|
|
185
187
|
If `cached` is `true`, then the current request has been served from cache and **you should early `return`**. Otherwise, continue setting `ctx.body=` and this will cache the response.
|
|
186
188
|
|
|
189
|
+
### CashClear
|
|
190
|
+
|
|
191
|
+
```js
|
|
192
|
+
ctx.cashClear('/')
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
This is a special method available on the ctx that you can use to clear the cache for a specific key.
|
|
196
|
+
|
|
187
197
|
## Notes
|
|
188
198
|
|
|
189
|
-
* Only `GET` and `HEAD` requests are cached.
|
|
199
|
+
* Only `GET` and `HEAD` requests are cached. (Unless overridden)
|
|
190
200
|
* Only `200` responses are cached. Don't set `304` status codes on these routes - this middleware will handle it for you
|
|
191
201
|
* The underlying store should be able to handle `Date` objects as well as `Buffer` objects. Otherwise, you may have to serialize/deserialize yourself.
|
|
192
202
|
|
|
193
|
-
## Usage
|
|
194
|
-
|
|
195
203
|
## Contributors
|
|
196
204
|
|
|
197
205
|
| Name | Website |
|
package/index.js
CHANGED
|
@@ -36,6 +36,12 @@ module.exports = function(options) {
|
|
|
36
36
|
if (!get) throw new Error('.get not defined');
|
|
37
37
|
if (!set) throw new Error('.set not defined');
|
|
38
38
|
|
|
39
|
+
// allow for manual cache clearing
|
|
40
|
+
function cashClear(key) {
|
|
41
|
+
// console.log(`Removing cache key: ${key}`);
|
|
42
|
+
set(key, false);
|
|
43
|
+
}
|
|
44
|
+
|
|
39
45
|
// ctx.cashed(maxAge) => boolean
|
|
40
46
|
async function cashed(maxAge) {
|
|
41
47
|
// uncacheable request method
|
|
@@ -84,6 +90,7 @@ module.exports = function(options) {
|
|
|
84
90
|
async function middleware(ctx, next) {
|
|
85
91
|
ctx.vary('Accept-Encoding');
|
|
86
92
|
ctx.cashed = cashed.bind(ctx);
|
|
93
|
+
ctx.cashClear = cashClear.bind(ctx);
|
|
87
94
|
|
|
88
95
|
await next();
|
|
89
96
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koa-cash",
|
|
3
3
|
"description": "HTTP response caching for Koa. HTTP response caching for Koa. Supports Redis, in-memory store, and more!",
|
|
4
|
-
"version": "4.1.
|
|
4
|
+
"version": "4.1.1",
|
|
5
5
|
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)",
|
|
6
6
|
"ava": {
|
|
7
7
|
"verbose": true,
|
|
@@ -38,7 +38,6 @@
|
|
|
38
38
|
"eslint": "6.x",
|
|
39
39
|
"eslint-config-xo-lass": "latest",
|
|
40
40
|
"fixpack": "latest",
|
|
41
|
-
"husky": "latest",
|
|
42
41
|
"into-stream": "^5.1.1",
|
|
43
42
|
"koa": "^2.12.0",
|
|
44
43
|
"lint-staged": "latest",
|
|
@@ -46,7 +45,7 @@
|
|
|
46
45
|
"nyc": "latest",
|
|
47
46
|
"remark-cli": "latest",
|
|
48
47
|
"remark-preset-github": "latest",
|
|
49
|
-
"supertest": "
|
|
48
|
+
"supertest": "latest",
|
|
50
49
|
"xo": "0.25"
|
|
51
50
|
},
|
|
52
51
|
"engines": {
|
|
@@ -56,12 +55,6 @@
|
|
|
56
55
|
"index.js"
|
|
57
56
|
],
|
|
58
57
|
"homepage": "https://github.com/koajs/cash",
|
|
59
|
-
"husky": {
|
|
60
|
-
"hooks": {
|
|
61
|
-
"pre-commit": "lint-staged && npm test",
|
|
62
|
-
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
58
|
"keywords": [
|
|
66
59
|
"alternative",
|
|
67
60
|
"amazon",
|