Skip to content

Commit 4ae232b

Browse files
author
Kevin R. Whitley
authored
Merge pull request #97 from kwhitley/node-support-7.7
Node support 7.7
2 parents 0af9811 + 37e85ca commit 4ae232b

10 files changed

+2120
-359
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/npm-debug.log
44
.DS_STORE
55
*.rdb
6+
.nyc_output

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
language: node_js
22
node_js:
33
- 4.0.0
4+
- 6.0.0
5+
- 7.0.0
6+
- 8.0.0
7+
after_success: npm run coverage

README.md

+39-19
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
A simple API response caching middleware for Express/Node using plain-english durations. Supports Redis or built-in memory engine.
1+
A simple API response caching middleware for Express/Node using plain-english durations.
22
=======
3+
#### Supports Redis or built-in memory engine with auto-clearing.
34

4-
[![npm version](https://badge.fury.io/js/apicache.svg)](https://badge.fury.io/js/apicache)
5+
[![npm version](https://badge.fury.io/js/apicache.svg)](https://www.npmjs.com/package/apicache)
6+
[![node version support](https://img.shields.io/node/v/apicache.svg)](https://www.npmjs.com/package/apicache)
57
[![Build Status via Travis CI](https://travis-ci.org/kwhitley/apicache.svg?branch=master)](https://travis-ci.org/kwhitley/apicache)
8+
[![Coverage Status](https://coveralls.io/repos/github/kwhitley/apicache/badge.svg?branch=master)](https://coveralls.io/github/kwhitley/apicache?branch=master)
69
[![NPM downloads](https://img.shields.io/npm/dt/apicache.svg?style=flat-square)](https://www.npmjs.com/package/apicache)
710

811
## Why?
9-
10-
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.
1113

1214
## Installation
13-
1415
```
1516
npm install apicache
1617
```
1718

1819
## Dependencies
19-
20-
None (unless using Redis)
20+
None
2121

2222
## Usage
23-
2423
To use, simply inject the middleware (example: `apicache.middleware('5 minutes', [optionalMiddlewareToggle])`) into your routes. Everything else is automagic.
2524

2625
#### Cache a route
@@ -116,27 +115,43 @@ app.get('/api/found', cacheSuccesses, (req, res) => {
116115
})
117116
```
118117

118+
#### 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+
119130
## API
120131

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.
122135
- `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.
125137
- `apicache.newInstance([options])` - used to create a new ApiCache instance (by default, simply requiring this library shares a common instance)
126138
- `apicache.clone()` - used to create a new ApiCache instance with the same options as the current one
127139

128140
#### Available Options (first value is default)
129141

130142
```js
131143
{
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']
137149
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
140155
}
141156
}
142157
```
@@ -209,6 +224,7 @@ The presence of this header flag will bypass the cache, ensuring you aren't look
209224

210225
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!
211226

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!!!
212228
- [@rutgernation](https://github.com/rutgernation) - JSONP support
213229
- [@enricsangra](https://github.com/enricsangra) - added x-apicache-force-fetch header
214230
- [@tskillian](https://github.com/tskillian) - custom appendKey path support
@@ -217,10 +233,10 @@ Special thanks to all those that use this library and report issues, but especia
217233
- [@nmors](https://github.com/nmors) - redis support
218234
- [@maytis](https://github.com/maytis), [@ashwinnaidu](https://github.com/ashwinnaidu) - redis expiration
219235
- [@killdash9](https://github.com/killdash9) - restify support and response accumulator method
220-
- [@svozza](https://github.com/svozza) - added restify tests, test suite refactor, and fixed header issue with restify. Double thanks!!!
221236
- [@ubergesundheit](https://github.com/ubergesundheit) - Corrected buffer accumulation using res.write with Buffers
222237
- [@danielsogl](https://github.com/danielsogl) - Keeping dev deps up to date
223238
- [@peteboere](https://github.com/peteboere) - Node v7 headers update
239+
- [@vectart](https://github.com/vectart) - Added middleware local options support
224240

225241
### Bugfixes
226242

@@ -243,4 +259,8 @@ Special thanks to all those that use this library and report issues, but especia
243259
- **v0.8.5** - dev dependencies update (thanks @danielsogl)
244260
- **v0.8.6, v0.8.7** - README update
245261
- **v0.8.8** - corrected to use node v7+ headers (thanks @peteboere)
262+
- **v0.9.0** - corrected Node v7.7 & v8 conflicts with restify (huge thanks to @svozza
263+
for chasing this down and fixing upstream in restify itself). Added coveralls. Added
264+
middleware.localOptions support (thanks @vectart). Added ability to overwrite/embed headers
265+
(e.g. "cache-control": "no-cache") through options.
246266

0 commit comments

Comments
 (0)