Skip to content

Commit c068694

Browse files
committed
**Breaking** Added instantiation so a single cache could be used for multiple frameworks or usages.
1 parent 8f0e292 commit c068694

File tree

5 files changed

+42
-32
lines changed

5 files changed

+42
-32
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ This will cache the response for 30 seconds, as described by the expires header.
1616
```javascript
1717
// Express.js Web Server
1818
var app = require('express')(),
19-
crispHttpCache = require('crisp-http-cache');
20-
21-
app.use(crispHttpCache({
22-
cacheOptions: {
23-
maxSize: 50
24-
}
25-
}));
19+
CrispHttpCache = require('crisp-http-cache');
20+
21+
var cache = new CrispHttpCache({
22+
cacheOptions: {
23+
maxSize: 50
24+
}
25+
});
26+
app.use(cache.getExpressMiddleware());
2627

2728
app.get('/hello', function (req, res) {
2829
res.set('expires', new Date(Date.now() + 30000).toUTCString());
@@ -38,6 +39,8 @@ var listener = app.listen(9001, function() {
3839

3940
## Options
4041

42+
`crisp-http-cache` should be instantiated and the following options can be passed in an object to configure the cache. The middleware can be accessed by calling the getter corresponding to your desired framework.
43+
4144
| Option | Type | Default | Description |
4245
| ------ | ---- | ------- | ----------- |
4346
| `enabled` | (boolean) | `true` | A master switch of if we should cache or not, useful to set this to `false` while debugging. |

demo/demo-server.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
var crispHttpCache = require('../main'),
1+
var CrispHttpCache = require('../main'),
22
express = require('express');
33

44
var app = express();
55

6-
app.use(crispHttpCache({
6+
var cache = new CrispHttpCache({
77
cacheOptions: {
88
maxSize: 50
99
}
10-
}));
10+
});
11+
12+
app.use(cache.getExpressMiddleware());
1113

1214
app.get('/hello', function(req, res) {
1315
res.set('expires', new Date(Date.now() + 30000));

main.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var CrispCache = require('crisp-cache'),
1919
*
2020
* @return {crispHttpCache~middleware}
2121
*/
22-
function crispHttpCache(options) {
22+
function CrispHttpCache(options) {
2323
if (options === undefined) {
2424
options = {};
2525
}
@@ -39,7 +39,9 @@ function crispHttpCache(options) {
3939
}
4040
}
4141
this.cache = new CrispCache(this.cacheOptions);
42+
}
4243

44+
CrispHttpCache.prototype.getExpressMiddleware = function () {
4345
return function (req, res, next) {
4446
if (this.enabled) {
4547
this.shouldCache(req, res, function (err, shouldCache) {
@@ -73,15 +75,15 @@ function crispHttpCache(options) {
7375
}
7476
else {
7577
debug("Cache values did not pass compareCache, re-running.");
76-
interceptRes(req, res, key, this.getTtl, this.cache, this.transformHeaders);
78+
this._interceptRes(req, res, key, this.getTtl, this.cache, this.transformHeaders);
7779
return next();
7880
}
79-
});
81+
}.bind(this));
8082

8183
}
8284
else {
8385
debug("Cache miss for: " + key);
84-
interceptRes(req, res, key, this.getTtl, this.cache, this.transformHeaders);
86+
this._interceptRes(req, res, key, this.getTtl, this.cache, this.transformHeaders);
8587
return next();
8688
}
8789
}.bind(this));
@@ -91,21 +93,21 @@ function crispHttpCache(options) {
9193
//shouldCache function said we should skip caching.
9294
return next();
9395
}
94-
});
96+
}.bind(this));
9597
}
9698
else {
9799
//Caching disabled, skip
98100
return next();
99101
}
100102
}.bind(this);
101-
}
103+
};
102104

103-
function interceptRes(req, res, key, getTtl, cache) {
105+
CrispHttpCache.prototype._interceptRes = function(req, res, key, getTtl, cache) {
104106
saveBody(res);
105-
preFinish(res, function(res, data, cb) {
107+
preFinish(res, function (res, data, cb) {
106108
this.transformHeaders(res);
107109
cb(null, res);
108-
});
110+
}.bind(this));
109111
onFinished(res, function (err, res) {
110112
if (res.statusCode < 200 || res.statusCode >= 300) {
111113
debug("Non 2XX status code, not saving");
@@ -122,19 +124,21 @@ function interceptRes(req, res, key, getTtl, cache) {
122124
getTtl(req, res, function (err, ttl) {
123125
debug(" - With TTL: " + ttl);
124126
// If we didn't get a hangup, we can cache it.
125-
if(res.body && res.body.length) {
127+
if (res.body && res.body.length) {
126128
cache.set(key, cachedEntry, {size: res.body.length, expiresTtl: ttl});
127129
}
128130
});
129131
});
130-
}
132+
};
133+
134+
module.exports = CrispHttpCache;
131135

132136
function preFinish(res, cb) {
133137
var origSend = res.send;
134138

135-
res.send = function(data) {
139+
res.send = function (data) {
136140
var sendArgs = arguments;
137-
cb(res, data, function(err, res) {
141+
cb(res, data, function (err, res) {
138142
origSend.apply(res, sendArgs);
139143
});
140144
}
@@ -337,12 +341,12 @@ function _transformHeaders(res, estExpiresInterval) {
337341
}
338342

339343
function _parseDateString(date) {
340-
if(date === 'Infinity') {
344+
if (date === 'Infinity') {
341345
return Infinity;
342346
}
343-
else if(!isNaN(parseInt(date))) {
347+
else if (!isNaN(parseInt(date))) {
344348
return parseFloat(date);
345-
} else if(!isNaN(Date.parse(date))) {
349+
} else if (!isNaN(Date.parse(date))) {
346350
return new Date(date);
347351
}
348352
else {
@@ -362,8 +366,6 @@ function _parseContentTypeCharset(contentTypeString) {
362366
return false;
363367
}
364368

365-
module.exports = crispHttpCache;
366-
367369
/**
368370
* A function that is provided with the current context of the request
369371
* @callback crispHttpCache~contextCallback

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-crisp-http-cache",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Middleware providing a crispy fresh cache for HTTP responses. ",
55
"main": "main.js",
66
"scripts": {

test/frameworks/expressjs.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var assert = require('assert'),
22
async = require('async'),
3-
crispHttpCache = require('../../main'),
3+
CrispHttpCache = require('../../main'),
44
express = require('express'),
55
request = require('supertest'),
66
sinon = require('sinon');
@@ -106,11 +106,14 @@ describe("Express Middleware", function () {
106106
});
107107

108108
function setupExpress(app) {
109-
app.use(crispHttpCache({
109+
110+
var cache = new CrispHttpCache({
110111
cacheOptions: {
111112
maxSize: 50
112113
}
113-
}));
114+
});
115+
116+
app.use(cache.getExpressMiddleware());
114117

115118
app.get('/hello', function (req, res) {
116119
res.set('expires', new Date(Date.now() + 30000).toUTCString());

0 commit comments

Comments
 (0)