You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Because caching of simple data/responses should ALSO be simple, and calculating milliseconds or manually caching entries seems prehistoric.
12
+
Because route-caching of simple data/responses should ALSO be simple.
11
13
12
14
## Installation
13
-
14
15
```
15
16
npm install apicache
16
17
```
17
18
18
19
## Dependencies
19
-
20
-
None (unless using Redis)
20
+
None
21
21
22
22
## Usage
23
-
24
23
To use, simply inject the middleware (example: `apicache.middleware('5 minutes', [optionalMiddlewareToggle])`) into your routes. Everything else is automagic.
#### Prevent cache-control header "max-age" from automatically being set to expiration age
119
+
```js
120
+
let cache =apicache.options({
121
+
headers: {
122
+
'cache-control':'no-cache'
123
+
}
124
+
})
125
+
.middleware
126
+
127
+
let cache5min =cache('5 min') // continue to use normally
128
+
```
129
+
119
130
## API
120
131
121
-
-`apicache.clear([target])` - clears cache target (key or group), or entire cache if no value passed, returns new index.
132
+
-`apicache.options([globalOptions])` - getter/setter for global options. If used as a setter, this function is chainable, allowing you to do things such as... say... return the middleware.
133
+
-`apicache.middleware([duration], [toggleMiddleware], [localOptions])` - the actual middleware that will be used in your routes. `duration` is in the following format "[length][unit]", as in `"10 minutes"` or `"1 day"`. A second param is a middleware toggle function, accepting request and response params, and must return truthy to enable cache for the request. Third param is the options that will override global ones and affect this middleware only.
134
+
-`middleware.options([localOptions])` - getter/setter for middleware-specific options that will override global ones.
122
135
-`apicache.getIndex()` - returns current cache index [of keys]
123
-
-`apicache.middleware([duration], [toggleMiddleware])` - the actual middleware that will be used in your routes. `duration` is in the following format "[length][unit]", as in `"10 minutes"` or `"1 day"`. A second param is a middleware toggle function, accepting request and response params, and must return truthy to enable cache for the request.
124
-
-`apicache.options([options])` - getter/setter for options. If used as a setter, this function is chainable, allowing you to do things such as... say... return the middleware.
136
+
-`apicache.clear([target])` - clears cache target (key or group), or entire cache if no value passed, returns new index.
125
137
-`apicache.newInstance([options])` - used to create a new ApiCache instance (by default, simply requiring this library shares a common instance)
126
138
-`apicache.clone()` - used to create a new ApiCache instance with the same options as the current one
127
139
128
140
#### Available Options (first value is default)
129
141
130
142
```js
131
143
{
132
-
debug:false|true, // if true, enables console output
133
-
defaultDuration:3600000, // should be a number (in ms), defaults to 1 hour
134
-
enabled:true|false, // if false, turns off caching globally (useful on dev)
135
-
redisClient: client, // if provided, uses the [node-redis](https://github.com/NodeRedis/node_redis) client instead of [memory-cache](https://github.com/ptarjan/node-cache)
136
-
appendKey: [], // if you want the key (which is the URL) to be appended by something in the req object, put req properties here that point to what you want appended. I.E. req.session.id would be ['session', 'id']
144
+
debug:false|true, // if true, enables console output
145
+
defaultDuration:3600000, // should be a number (in ms), defaults to 1 hour
146
+
enabled:true|false, // if false, turns off caching globally (useful on dev)
147
+
redisClient: client, // if provided, uses the [node-redis](https://github.com/NodeRedis/node_redis) client instead of [memory-cache](https://github.com/ptarjan/node-cache)
148
+
appendKey: [], // if you want the key (which is the URL) to be appended by something in the req object, put req properties here that point to what you want appended. I.E. req.session.id would be ['session', 'id']
137
149
statusCodes: {
138
-
exclude: [], // list status codes to specifically exclude (e.g. [404, 403] cache all responses unless they had a 404 or 403 status)
139
-
include: [], // list status codes to require (e.g. [200] caches ONLY responses with a success/200 code)
150
+
exclude: [], // list status codes to specifically exclude (e.g. [404, 403] cache all responses unless they had a 404 or 403 status)
151
+
include: [], // list status codes to require (e.g. [200] caches ONLY responses with a success/200 code)
152
+
},
153
+
headers: {
154
+
// 'cache-control': 'no-cache' // example of header overwrite
140
155
}
141
156
}
142
157
```
@@ -209,6 +224,7 @@ The presence of this header flag will bypass the cache, ensuring you aren't look
209
224
210
225
Special thanks to all those that use this library and report issues, but especially to the following active users that have helped add to the core functionality!
211
226
227
+
-[@svozza](https://github.com/svozza) - added restify tests, test suite refactor, and fixed header issue with restify. Node v7 + Restify v5 conflict resolution, etc, etc. Triple thanks!!!
212
228
-[@rutgernation](https://github.com/rutgernation) - JSONP support
0 commit comments