@ohos-ports/express-redis-cache 0.0.0-stage → 1.1.3-beta.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/.travis.yml +6 -0
- package/CHANGELOG.md +93 -0
- package/LICENSE +21 -0
- package/README.md +494 -2
- package/bin/express-redis-cache.js +270 -0
- package/example/example.js +27 -0
- package/express/index.html +2 -0
- package/express/server.js +90 -0
- package/index.js +13 -0
- package/lib/ExpressRedisCache/add.js +80 -0
- package/lib/ExpressRedisCache/del.js +92 -0
- package/lib/ExpressRedisCache/expire.js +69 -0
- package/lib/ExpressRedisCache/get.js +92 -0
- package/lib/ExpressRedisCache/route.js +221 -0
- package/lib/ExpressRedisCache/size.js +54 -0
- package/lib/ExpressRedisCache.js +170 -0
- package/lib/sizeof.js +73 -0
- package/package.json +82 -4
- package/test/ExpressRedisCache.js +77 -0
- package/test/add.js +119 -0
- package/test/del.js +99 -0
- package/test/expire.js +127 -0
- package/test/express.js +229 -0
- package/test/get.js +123 -0
- package/test/route.js +286 -0
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
module.exports = (function () {
|
|
4
|
+
|
|
5
|
+
'use strict';
|
|
6
|
+
|
|
7
|
+
require('colors');
|
|
8
|
+
|
|
9
|
+
var pkg = require('../package.json');
|
|
10
|
+
|
|
11
|
+
var domain = require('domain').create();
|
|
12
|
+
|
|
13
|
+
domain.on('error', function (error) {
|
|
14
|
+
throw error;
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
domain.run(function () {
|
|
18
|
+
|
|
19
|
+
/** Get configuration from command line if any **/
|
|
20
|
+
|
|
21
|
+
var port, host, prefix, skip = [];
|
|
22
|
+
|
|
23
|
+
process.argv = process.argv.map(function (argv, i, all) {
|
|
24
|
+
if ( argv === '--prefix' && process.argv[(i + 1)] ) {
|
|
25
|
+
prefix = process.argv[(i + 1)];
|
|
26
|
+
skip.push(i + 1);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
else if ( argv === '--port' && process.argv[(i + 1)] ) {
|
|
30
|
+
port = process.argv[(i + 1)];
|
|
31
|
+
skip.push(i + 1);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
else if ( argv === '--host' && process.argv[(i + 1)] ) {
|
|
35
|
+
host = process.argv[(i + 1)];
|
|
36
|
+
skip.push(i + 1);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
else if ( skip.indexOf(i) === -1 ) {
|
|
40
|
+
return argv;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
skip = undefined;
|
|
45
|
+
|
|
46
|
+
/** Iniatilize express-redis-cache **/
|
|
47
|
+
|
|
48
|
+
var cache;
|
|
49
|
+
|
|
50
|
+
function connect () {
|
|
51
|
+
cache = require('../')({
|
|
52
|
+
host: host,
|
|
53
|
+
port: port,
|
|
54
|
+
prefix: prefix || pkg.config.prefix
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
cache
|
|
58
|
+
|
|
59
|
+
.on('error', function (error) {
|
|
60
|
+
error.stack.split(/\n/).forEach(function (line) {
|
|
61
|
+
console.log(line.yellow);
|
|
62
|
+
});
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
.on('message', function (message) {
|
|
66
|
+
console.log(message.grey);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** A function to pretty print a cache entry **/
|
|
71
|
+
|
|
72
|
+
function formatEntry (entry) {
|
|
73
|
+
try {
|
|
74
|
+
var moment = require('moment');
|
|
75
|
+
|
|
76
|
+
var iso = new Date(+entry.touched).toISOString();
|
|
77
|
+
|
|
78
|
+
/** Name **/
|
|
79
|
+
console.log(' ' + entry.name.blue.bold);
|
|
80
|
+
|
|
81
|
+
/** Touched **/
|
|
82
|
+
console.log(' %s %s', 'touched'.yellow,
|
|
83
|
+
moment(iso, moment.ISO_8601).fromNow());
|
|
84
|
+
|
|
85
|
+
/** Expire **/
|
|
86
|
+
console.log(' %s %s', 'expires'.yellow,
|
|
87
|
+
(function () {
|
|
88
|
+
if ( entry.expire < 0 || typeof expire === 'undefined' ) {
|
|
89
|
+
return 'NEVER';
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
var expire = ( +entry.touched + ((+entry.expire || 0) * 1000));
|
|
93
|
+
|
|
94
|
+
var iso2 = new Date(expire).toISOString();
|
|
95
|
+
|
|
96
|
+
return moment(iso2, moment.ISO_8601).fromNow();
|
|
97
|
+
}
|
|
98
|
+
})());
|
|
99
|
+
|
|
100
|
+
/** Object size in bytes **/
|
|
101
|
+
console.log(' %s %s bytes ' + '%s KB'.grey,
|
|
102
|
+
'size'.yellow,
|
|
103
|
+
require('../lib/sizeof')(entry),
|
|
104
|
+
(require('../lib/sizeof')(entry) / 1024).toFixed(2));
|
|
105
|
+
|
|
106
|
+
/** Body length **/
|
|
107
|
+
console.log(' %s %s', 'body'.yellow, entry.body.length +
|
|
108
|
+
' characters');
|
|
109
|
+
}
|
|
110
|
+
catch ( error ) {
|
|
111
|
+
return console.log(' Not readable'.red, entry, error);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Switch action **/
|
|
116
|
+
|
|
117
|
+
switch ( process.argv[2] ) {
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
HELP
|
|
121
|
+
**/
|
|
122
|
+
|
|
123
|
+
case undefined:
|
|
124
|
+
case '-h':
|
|
125
|
+
case '--help':
|
|
126
|
+
case 'help':
|
|
127
|
+
|
|
128
|
+
console.log(' express-redis-cache v%s'.yellow, pkg.version);
|
|
129
|
+
console.log();
|
|
130
|
+
console.log(' Cache the response of a HTTP request inside a Redis list and let you manage your cache entries'.cyan);
|
|
131
|
+
console.log();
|
|
132
|
+
console.log(' Commands:');
|
|
133
|
+
console.log();
|
|
134
|
+
|
|
135
|
+
console.log(' add <name> <content> <expire> --type=<type>'.bold.yellow);
|
|
136
|
+
console.log(' Add a new cache entry');
|
|
137
|
+
console.log(' # Cache simple text');
|
|
138
|
+
console.log(' express-redis-cache add "test" "This is a test";'.cyan);
|
|
139
|
+
console.log(' # Cache a file');
|
|
140
|
+
console.log(' express-redis-cache add "home" "$(cat index.html)";'.cyan);
|
|
141
|
+
console.log(' # Cache a JSON object');
|
|
142
|
+
console.log(" express-redis-cache add \"user1:location\" '{ \"lat\": 4.7453, \"lng\": -31.332 }' --type json;".cyan);
|
|
143
|
+
console.log(' # Cache a text that will expire in one hour');
|
|
144
|
+
console.log(' express-redis-cache add "offer" "everything 25% off for the next hour" $(( 60 * 60 ));'.cyan);
|
|
145
|
+
console.log();
|
|
146
|
+
|
|
147
|
+
console.log(' get <name>'.bold.yellow);
|
|
148
|
+
console.log(' Get a single cache entry by name');
|
|
149
|
+
console.log(' # Get all cache entries for default prefix');
|
|
150
|
+
console.log(' express-redis-cache get'.cyan);
|
|
151
|
+
console.log();
|
|
152
|
+
|
|
153
|
+
console.log(' del <name>'.bold.yellow);
|
|
154
|
+
console.log(' Delete a cache entry');
|
|
155
|
+
console.log(' express-redis-cache del favorite-movie'.cyan);
|
|
156
|
+
console.log();
|
|
157
|
+
|
|
158
|
+
console.log(' size'.bold.yellow);
|
|
159
|
+
console.log(' Get cache size for all entries');
|
|
160
|
+
console.log();
|
|
161
|
+
|
|
162
|
+
console.log(' NOTE'.bold.yellow);
|
|
163
|
+
|
|
164
|
+
console.log(' express-redis-cache connects to Redis using localhost as default and Redis default port. You can override that using the options: --port <port> --host <host>. Ditto for --prefix <prefix>.'.cyan);
|
|
165
|
+
|
|
166
|
+
console.log();
|
|
167
|
+
|
|
168
|
+
break;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
LS
|
|
172
|
+
**/
|
|
173
|
+
|
|
174
|
+
case 'ls':
|
|
175
|
+
|
|
176
|
+
connect();
|
|
177
|
+
|
|
178
|
+
cache.ls(domain.intercept(function (entries) {
|
|
179
|
+
|
|
180
|
+
cache.client.quit();
|
|
181
|
+
|
|
182
|
+
if ( ! entries.length ) {
|
|
183
|
+
console.log(' [empty]'.grey);
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
entries.forEach(formatEntry);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
}));
|
|
190
|
+
break;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
SIZE
|
|
194
|
+
**/
|
|
195
|
+
|
|
196
|
+
case 'size':
|
|
197
|
+
|
|
198
|
+
connect();
|
|
199
|
+
|
|
200
|
+
cache.size(domain.intercept(function (size) {
|
|
201
|
+
|
|
202
|
+
cache.client.quit();
|
|
203
|
+
|
|
204
|
+
console.log('Size: %d bytes, %d KB, %d MB',
|
|
205
|
+
size,
|
|
206
|
+
(size / 1024).toFixed(2),
|
|
207
|
+
(size / (1024 * 1024)).toFixed(2));
|
|
208
|
+
|
|
209
|
+
}));
|
|
210
|
+
break;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
ADD
|
|
214
|
+
**/
|
|
215
|
+
|
|
216
|
+
case 'add':
|
|
217
|
+
if ( ! process.argv[3] || ! process.argv[4] ) {
|
|
218
|
+
throw new Error('Missing arguments');
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
connect();
|
|
222
|
+
|
|
223
|
+
cache.add(process.argv[3], process.argv[4], { expire: +process.argv[5] }, domain.intercept(function (name, entry) {
|
|
224
|
+
|
|
225
|
+
cache.client.quit();
|
|
226
|
+
|
|
227
|
+
entry.name = name;
|
|
228
|
+
|
|
229
|
+
formatEntry(entry);
|
|
230
|
+
}));
|
|
231
|
+
break;
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
DEL
|
|
235
|
+
**/
|
|
236
|
+
|
|
237
|
+
case 'del':
|
|
238
|
+
|
|
239
|
+
connect();
|
|
240
|
+
|
|
241
|
+
cache.del(process.argv[3], domain.intercept(function (occ) {
|
|
242
|
+
|
|
243
|
+
cache.client.quit();
|
|
244
|
+
|
|
245
|
+
console.log(' %d deletions', occ);
|
|
246
|
+
}));
|
|
247
|
+
break;
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
GET
|
|
251
|
+
**/
|
|
252
|
+
|
|
253
|
+
case 'get':
|
|
254
|
+
|
|
255
|
+
connect();
|
|
256
|
+
|
|
257
|
+
cache.get(process.argv[3], domain.intercept(function (entries) {
|
|
258
|
+
|
|
259
|
+
cache.client.quit();
|
|
260
|
+
|
|
261
|
+
entries.forEach(function (entry) {
|
|
262
|
+
formatEntry(entry);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
}));
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
})();
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
var app = require('express')(),
|
|
2
|
+
cache = require('../')({ expire: 5 });
|
|
3
|
+
|
|
4
|
+
cache.on('message', function(message){
|
|
5
|
+
console.log("cache", message);
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
cache.on('error', function(error){
|
|
9
|
+
console.error("cache", error);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
var port = 3000;
|
|
13
|
+
app.listen(port);
|
|
14
|
+
console.log("Server listening on http://localhost:" + port);
|
|
15
|
+
|
|
16
|
+
// Serve simple page with timestamp cached for 5 seconds
|
|
17
|
+
app.get('/:skip_cache?',
|
|
18
|
+
cache.route(),
|
|
19
|
+
function (req, res) {
|
|
20
|
+
if (req.params.skip_cache) {
|
|
21
|
+
res.use_express_redis_cache = false;
|
|
22
|
+
console.log ("Cache disabled on this request");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
var currTime = new Date();
|
|
26
|
+
res.send("Date and time: " + currTime);
|
|
27
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
var path = require('path');
|
|
4
|
+
|
|
5
|
+
/* ======== express ======== */
|
|
6
|
+
|
|
7
|
+
var express = require('express');
|
|
8
|
+
var app = express();
|
|
9
|
+
|
|
10
|
+
app.set('port', process.env.PORT || 3027);
|
|
11
|
+
|
|
12
|
+
/* ======== cache ======== */
|
|
13
|
+
var cache_options = {
|
|
14
|
+
expire : 3,
|
|
15
|
+
host : process.env.EX_RE_CA_HOST || 'localhost',
|
|
16
|
+
port : process.env.EX_RE_CA_PORT || 6379,
|
|
17
|
+
prefix : process.env.EX_RE_CA_PREFIX || 'erct:'
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
var cache = require('../')(cache_options);
|
|
21
|
+
var moment = require('moment');
|
|
22
|
+
|
|
23
|
+
cache.on('error', function (error) {
|
|
24
|
+
console.log('cache error', {
|
|
25
|
+
message: error.message
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
/* ======== body parser ======== */
|
|
30
|
+
|
|
31
|
+
var bodyParser = require('body-parser');
|
|
32
|
+
|
|
33
|
+
app.use(bodyParser.urlencoded({ extended: false }));
|
|
34
|
+
app.use(bodyParser.json());
|
|
35
|
+
|
|
36
|
+
/* ======== home ======== */
|
|
37
|
+
|
|
38
|
+
function handle_timestamp (req, res) {
|
|
39
|
+
res.set({'Content-Type': 'text/json'});
|
|
40
|
+
var timestamp = { timestamp: moment().unix()};
|
|
41
|
+
return res.json(timestamp);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Cache the time for 1 second as
|
|
45
|
+
// { timestamp: 1424309866 }
|
|
46
|
+
app.all('/1sec',
|
|
47
|
+
cache.route({expire: 1}),
|
|
48
|
+
handle_timestamp
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
app.all('/default_expire',
|
|
52
|
+
cache.route(),
|
|
53
|
+
handle_timestamp
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
app.all('/never_expire',
|
|
57
|
+
cache.route({expire: -1}),
|
|
58
|
+
handle_timestamp
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
app.all('/delete_never_expire',
|
|
62
|
+
function(req, res) {
|
|
63
|
+
cache.del("/never_expire", function(err, count){
|
|
64
|
+
if (err) {
|
|
65
|
+
return res.send(500);
|
|
66
|
+
}
|
|
67
|
+
return res.send("count:" + count);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
}
|
|
71
|
+
);
|
|
72
|
+
app.all('/',
|
|
73
|
+
|
|
74
|
+
cache.route(),
|
|
75
|
+
|
|
76
|
+
function (req, res) {
|
|
77
|
+
res.send('Now is ' + new Date());
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
/* ======== server ======== */
|
|
81
|
+
|
|
82
|
+
var server = require('http').createServer(app);
|
|
83
|
+
|
|
84
|
+
server.listen(app.get('port'), function () {
|
|
85
|
+
console.log('express-redis-cache test server started on port ' + app.get('port'));
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
server.on('error', function (error) {
|
|
89
|
+
console.log({ 'server error': error });
|
|
90
|
+
});
|
package/index.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
module.exports = (function () {
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
var config = require('../../package.json').config;
|
|
6
|
+
|
|
7
|
+
/** Add
|
|
8
|
+
*
|
|
9
|
+
* @function
|
|
10
|
+
* @description Add a new cache entry
|
|
11
|
+
* @return void
|
|
12
|
+
* @callback
|
|
13
|
+
* @arg {String} name - The cache entry name
|
|
14
|
+
* @arg {String} body - The cache entry body
|
|
15
|
+
* @arg {Object} options - Optional { type: String, expire: Number }
|
|
16
|
+
* @arg {Number} expire - The cache life time in seconds [OPTIONAL]
|
|
17
|
+
* @arg {Function} callback
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
function add (name, body, options, callback) {
|
|
21
|
+
var self = this;
|
|
22
|
+
|
|
23
|
+
/** Adjust arguments in case @options is omitted **/
|
|
24
|
+
if ( ! callback && (typeof options === 'function') ) {
|
|
25
|
+
callback = options;
|
|
26
|
+
options = {};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
var domain = require('domain').create();
|
|
30
|
+
|
|
31
|
+
domain.on('error', domain.bind(function (error) {
|
|
32
|
+
self.emit('error', error);
|
|
33
|
+
self.connected = false;
|
|
34
|
+
callback(error);
|
|
35
|
+
}));
|
|
36
|
+
|
|
37
|
+
domain.run(function () {
|
|
38
|
+
|
|
39
|
+
if (self.connected === false) {
|
|
40
|
+
return callback(null, []); // simulate cache miss
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The new cache entry **/
|
|
44
|
+
var entry = {
|
|
45
|
+
body: body,
|
|
46
|
+
type: options.type || config.type,
|
|
47
|
+
touched: +new Date(),
|
|
48
|
+
expire: (typeof options.expire !== 'undefined' && options.expire !== false) ? options.expire : self.expire
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
var size = require('../sizeof')(entry);
|
|
52
|
+
|
|
53
|
+
var prefix = self.prefix.match(/:$/) ? self.prefix.replace(/:$/, '')
|
|
54
|
+
: self.prefix;
|
|
55
|
+
|
|
56
|
+
/* Save as a Redis hash */
|
|
57
|
+
var redisKey = prefix + ':' + name;
|
|
58
|
+
self.client.hmset(redisKey, entry,
|
|
59
|
+
domain.intercept(function (res) {
|
|
60
|
+
var calculated_size = (size / 1024).toFixed(2);
|
|
61
|
+
/** If @expire then tell Redis to expire **/
|
|
62
|
+
if ( typeof entry.expire === 'number' && entry.expire > 0 ) {
|
|
63
|
+
self.client.expire(redisKey, +entry.expire,
|
|
64
|
+
domain.intercept(function () {
|
|
65
|
+
self.emit('message', require('util').format('SET %s ~%d Kb %d TTL (sec)', redisKey, calculated_size, +entry.expire));
|
|
66
|
+
callback(null, name, entry, res);
|
|
67
|
+
}));
|
|
68
|
+
}
|
|
69
|
+
else
|
|
70
|
+
{
|
|
71
|
+
self.emit('message', require('util').format('SET %s ~%d Kb', redisKey, calculated_size));
|
|
72
|
+
callback(null, name, entry, res);
|
|
73
|
+
}
|
|
74
|
+
}));
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return add;
|
|
79
|
+
|
|
80
|
+
})();
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
module.exports = (function () {
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/** Delete cache entries
|
|
6
|
+
*
|
|
7
|
+
* @method ExpressRedisCache.del
|
|
8
|
+
* @description Delete entry by name
|
|
9
|
+
* @return null
|
|
10
|
+
* @arg {String} name - The entry name
|
|
11
|
+
* @arg {Function} callback
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
function del (name, callback) {
|
|
15
|
+
var self = this;
|
|
16
|
+
|
|
17
|
+
if ( typeof name !== 'string' ) {
|
|
18
|
+
return this.emit('error', new Error('ExpressRedisCache.del: missing first argument String'));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if ( typeof callback !== 'function' ) {
|
|
22
|
+
return this.emit('error', new Error('ExpressRedisCache.del: missing second argument Function'));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
var domain = require('domain').create();
|
|
26
|
+
|
|
27
|
+
domain.on('error', function onDelError (error) {
|
|
28
|
+
callback(error);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
domain.run(function delRun () {
|
|
32
|
+
|
|
33
|
+
/** Get prefix */
|
|
34
|
+
|
|
35
|
+
var prefix = self.prefix.match(/:$/) ? self.prefix.replace(/:$/, '')
|
|
36
|
+
: self.prefix;
|
|
37
|
+
|
|
38
|
+
/** Tell Redis to delete hash */
|
|
39
|
+
|
|
40
|
+
var redisKey = prefix + ':' + name;
|
|
41
|
+
|
|
42
|
+
/** Detect wilcard syntax */
|
|
43
|
+
|
|
44
|
+
var hasWildcard = redisKey.indexOf('*') >= 0;
|
|
45
|
+
|
|
46
|
+
/** If has wildcard */
|
|
47
|
+
|
|
48
|
+
if ( hasWildcard ) {
|
|
49
|
+
|
|
50
|
+
/** Get a list of keys using the wildcard */
|
|
51
|
+
|
|
52
|
+
self.client.keys(redisKey, domain.intercept(function onKeys (keys) {
|
|
53
|
+
|
|
54
|
+
require('async').each(keys,
|
|
55
|
+
|
|
56
|
+
function onEachKey (key, callback) {
|
|
57
|
+
self.client.del(key, domain.intercept(function () {
|
|
58
|
+
self.emit('message', require('util').format('DEL %s', key));
|
|
59
|
+
callback();
|
|
60
|
+
}));
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
function onEachKeyDone (error) {
|
|
64
|
+
|
|
65
|
+
if ( error ) {
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
callback(null, keys.length);
|
|
70
|
+
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
}));
|
|
74
|
+
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** No wildcard **/
|
|
78
|
+
|
|
79
|
+
else {
|
|
80
|
+
self.client.del(redisKey,
|
|
81
|
+
domain.intercept(function onKeyDeleted (deletions) {
|
|
82
|
+
self.emit('message', require('util').format('DEL %s', redisKey));
|
|
83
|
+
callback(null, +deletions);
|
|
84
|
+
}));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return del;
|
|
91
|
+
|
|
92
|
+
})();
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
module.exports = (function () {
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
// Convert HTTP status codes into 1xx, 2xx, 3xx, 4xx, 5xx
|
|
6
|
+
function maskStatus (statusCode) {
|
|
7
|
+
return String(statusCode).match(/(\d)\d\d/)[1] + 'xx';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// Turn the policy argument into a function that consumes a
|
|
11
|
+
// status code and returns the expiration value
|
|
12
|
+
function createExpirationPolicy (options) {
|
|
13
|
+
if (typeof options !== 'object' && typeof options !== 'number' && typeof options !== 'function') {
|
|
14
|
+
throw new Error('expire option cannot be type ' + typeof options);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// If what's passed in is a function, return the function.
|
|
18
|
+
if ( typeof options === 'function' ) {
|
|
19
|
+
return options;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Internally store expiration as an object,
|
|
23
|
+
// so if a number is provided, use that as the default
|
|
24
|
+
if ( typeof options === 'number' ) {
|
|
25
|
+
options = {
|
|
26
|
+
'xxx': options
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
for (var k in options) {
|
|
31
|
+
// Ensure that keys are in the form xxx, 4xx, or 400
|
|
32
|
+
if (!k.match(/xxx/i) && !k.match(/[1-5]xx/i) && !k.match(/[1-5][0-9]{2}/)) {
|
|
33
|
+
throw new Error('invalid statusCode ' + k);
|
|
34
|
+
}
|
|
35
|
+
// Ensure that the expiration values are numbers
|
|
36
|
+
if (typeof options[k] !== 'number') {
|
|
37
|
+
throw new Error('invalid expiration for statusCode ' + k);
|
|
38
|
+
}
|
|
39
|
+
// Convert keys to lower case
|
|
40
|
+
var v = options[k];
|
|
41
|
+
delete options[v];
|
|
42
|
+
options[k.toLowerCase()] = v;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Ensure that there is a default so we can always return a value
|
|
46
|
+
if (!options.hasOwnProperty('xxx')) {
|
|
47
|
+
throw new Error('no default expiration provided');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return function (req, res) {
|
|
51
|
+
var statusCode = res.statusCode;
|
|
52
|
+
|
|
53
|
+
// Look for exact status code matches first
|
|
54
|
+
if (options.hasOwnProperty(statusCode)) {
|
|
55
|
+
return options[statusCode];
|
|
56
|
+
}
|
|
57
|
+
// Test for a 4xx style match
|
|
58
|
+
else if (options.hasOwnProperty(maskStatus(statusCode))) {
|
|
59
|
+
return options[maskStatus(statusCode)];
|
|
60
|
+
}
|
|
61
|
+
// Fallback to the default expiration value
|
|
62
|
+
else {
|
|
63
|
+
return options.xxx;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return createExpirationPolicy;
|
|
69
|
+
})();
|