@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,92 @@
|
|
|
1
|
+
module.exports = (function () {
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/** Get
|
|
6
|
+
*
|
|
7
|
+
* @function
|
|
8
|
+
* @description
|
|
9
|
+
* @return void
|
|
10
|
+
* @callback
|
|
11
|
+
* @arg {String} name - The cache entry name to get (wildcards accepted)
|
|
12
|
+
* @arg {Function} callback - Callback
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
function get (name, callback) {
|
|
16
|
+
var self = this;
|
|
17
|
+
|
|
18
|
+
var domain = require('domain').create();
|
|
19
|
+
|
|
20
|
+
domain.on('error', function (error) {
|
|
21
|
+
self.emit('error', error);
|
|
22
|
+
callback(error);
|
|
23
|
+
domain.exit();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
domain.run(function () {
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
if ( typeof name === 'function' ) {
|
|
30
|
+
callback = name;
|
|
31
|
+
name = '*';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
var prefix = self.prefix.match(/:$/) ? self.prefix.replace(/:$/, '')
|
|
35
|
+
: self.prefix;
|
|
36
|
+
|
|
37
|
+
var redisKey = prefix + ':' + (name ||'*');
|
|
38
|
+
|
|
39
|
+
/** Detect wildcard syntax */
|
|
40
|
+
var hasWildcard = redisKey.indexOf('*') >= 0;
|
|
41
|
+
|
|
42
|
+
var fetchKey = function (key, cb) {
|
|
43
|
+
self.client.hgetall(key, domain.intercept(function (result) {
|
|
44
|
+
if ( result ) {
|
|
45
|
+
var names = key.split(':');
|
|
46
|
+
result.name = names[1];
|
|
47
|
+
result.prefix = names[0];
|
|
48
|
+
self.emit('message', require('util').format('GET %s ~%d Kb', key,
|
|
49
|
+
(require('../sizeof')(result) / 1024).toFixed(2)));
|
|
50
|
+
}
|
|
51
|
+
cb(null, result);
|
|
52
|
+
}));
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** If has wildcard */
|
|
56
|
+
if ( hasWildcard ) {
|
|
57
|
+
/** Get a list of keys using the wildcard */
|
|
58
|
+
self.client.keys(prefix + ':' + name, domain.intercept(function (keys) {
|
|
59
|
+
if ( ! keys.length ) {
|
|
60
|
+
callback(null, []);
|
|
61
|
+
return domain.exit();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
require('async').parallel(keys.map(function (key) {
|
|
65
|
+
return function (cb) {
|
|
66
|
+
return fetchKey(key, cb);
|
|
67
|
+
};
|
|
68
|
+
}), domain.intercept(function (results) {
|
|
69
|
+
callback(null, results);
|
|
70
|
+
domain.exit();
|
|
71
|
+
}));
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
/** No wildcard **/
|
|
75
|
+
else {
|
|
76
|
+
fetchKey(redisKey, domain.intercept(function (result) {
|
|
77
|
+
if( result ) {
|
|
78
|
+
callback(null, [ result ]);
|
|
79
|
+
domain.exit();
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
callback(null, []);
|
|
83
|
+
domain.exit();
|
|
84
|
+
}
|
|
85
|
+
}));
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return get;
|
|
91
|
+
|
|
92
|
+
}) ();
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
module.exports = (function () {
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/** route() - Create a middleware
|
|
6
|
+
*
|
|
7
|
+
* @method ExpressRedisCache.route
|
|
8
|
+
* @return {Function} middleware
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
function route () {
|
|
12
|
+
|
|
13
|
+
/** The middleware to return
|
|
14
|
+
*
|
|
15
|
+
* @type Function
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
var middleware;
|
|
19
|
+
|
|
20
|
+
/** A reference to this
|
|
21
|
+
*
|
|
22
|
+
* @type ExpressRedisCache
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
var self = this;
|
|
26
|
+
|
|
27
|
+
/** The route options
|
|
28
|
+
*
|
|
29
|
+
* @type List
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
var options = arguments;
|
|
33
|
+
|
|
34
|
+
/** The domain handler
|
|
35
|
+
*
|
|
36
|
+
* @type Domain
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
var domain = require('domain').create();
|
|
40
|
+
|
|
41
|
+
/** The domain error handler
|
|
42
|
+
*
|
|
43
|
+
* @type Domain
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
domain.on('error', function (error) {
|
|
47
|
+
self.emit('error', error);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
domain.run(function () {
|
|
51
|
+
// Build the middleware function
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @function
|
|
55
|
+
* @arg {IncomingMessage} req
|
|
56
|
+
* @arg {HttpResponse} res
|
|
57
|
+
* @arg {Function} next
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
middleware = function expressRedisCache_Middleware (req, res, next) {
|
|
61
|
+
|
|
62
|
+
if ( res.expressRedisCache ) {
|
|
63
|
+
self.emit('deprecated', {
|
|
64
|
+
deprecated: 'expressRedisCache',
|
|
65
|
+
substitute: 'use_express_redis_cache',
|
|
66
|
+
file: __fileName,
|
|
67
|
+
line: __line
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
res.use_express_redis_cache = res.expressRedisCache;
|
|
71
|
+
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// If cache is disabled, call next()
|
|
75
|
+
if ( res.use_express_redis_cache === false ) {
|
|
76
|
+
return next();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// If the cache isn't connected, call next()
|
|
80
|
+
|
|
81
|
+
if ( self.connected === false || self.client.connected === false ) {
|
|
82
|
+
return next();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Cache entry name
|
|
86
|
+
*
|
|
87
|
+
* @type String
|
|
88
|
+
* @description The name the cache entry will be saved under
|
|
89
|
+
* @default req.originalUrl
|
|
90
|
+
*/
|
|
91
|
+
|
|
92
|
+
var name = req.originalUrl;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @deprecated `res.expressRedisCacheName` is deprecated starting on v0.1.1, `use res.express_redis_cache_name` instead
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
if ( res.expressRedisCacheName ) {
|
|
99
|
+
self.emit('deprecated', {
|
|
100
|
+
deprecated: 'expressRedisCacheName',
|
|
101
|
+
substitute: 'express_redis_cache_name',
|
|
102
|
+
file: __fileName,
|
|
103
|
+
line: __line
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
res.express_redis_cache_name = res.expressRedisCacheName;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// If a cache has been explicitly attached to `res` then use it as name
|
|
110
|
+
|
|
111
|
+
if ( res.express_redis_cache_name ) {
|
|
112
|
+
name = res.express_redis_cache_name;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// If route() was called with a string as its first argument, use this string as name
|
|
116
|
+
|
|
117
|
+
if ( typeof options[0] === 'string' ) {
|
|
118
|
+
name = options[0];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if ( typeof options[0] === 'object' && typeof options[0].name === 'string' ) {
|
|
122
|
+
name = options[0].name;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Name cannot have wildcards in them */
|
|
126
|
+
|
|
127
|
+
if ( /\*/.test(name) ) {
|
|
128
|
+
return next(new Error('Name can not have wildcards'));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The seconds entry lives
|
|
132
|
+
*
|
|
133
|
+
* @type Number
|
|
134
|
+
* @default this.expire
|
|
135
|
+
*/
|
|
136
|
+
var expire = self.expire;
|
|
137
|
+
if ( typeof options[0] === 'object' ) {
|
|
138
|
+
if ( typeof options[0].expire === 'number' || typeof options[0].expire === 'object' || typeof options[0].expire === 'function') {
|
|
139
|
+
expire = options[0].expire;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if ( typeof options[0] === 'number' ) {
|
|
144
|
+
expire = options[0];
|
|
145
|
+
}
|
|
146
|
+
else if ( typeof options[1] === 'number' || typeof options[1] === 'object') {
|
|
147
|
+
expire = options[1];
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
var binary = false;
|
|
151
|
+
if ( typeof options[0] === 'object' && typeof options[0].binary === 'boolean' ) {
|
|
152
|
+
binary = options[0].binary;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
var expirationPolicy = require('./expire')(expire);
|
|
156
|
+
|
|
157
|
+
/** attempt to get cache **/
|
|
158
|
+
self.get(name, domain.bind(function (error, cache) {
|
|
159
|
+
|
|
160
|
+
/** If there was an error with cache then call next **/
|
|
161
|
+
|
|
162
|
+
if ( error ) {
|
|
163
|
+
return next();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** if it's cached, display cache **/
|
|
167
|
+
|
|
168
|
+
if ( cache.length && cache[0].body != null ) {
|
|
169
|
+
res.contentType(cache[0].type || "text/html");
|
|
170
|
+
if(binary){ //Convert back to binary buffer
|
|
171
|
+
res.send(new Buffer(cache[0].body, 'base64'));
|
|
172
|
+
}else{
|
|
173
|
+
res.send(cache[0].body);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** otherwise, cache request **/
|
|
178
|
+
else {
|
|
179
|
+
|
|
180
|
+
/** wrap res.send **/
|
|
181
|
+
var send = res.send.bind(res);
|
|
182
|
+
|
|
183
|
+
res.send = function (body) {
|
|
184
|
+
|
|
185
|
+
/** send output to HTTP client **/
|
|
186
|
+
var ret = send(body);
|
|
187
|
+
|
|
188
|
+
/** convert binary to base64 string **/
|
|
189
|
+
if(binary && typeof body !== 'string'){
|
|
190
|
+
body = new Buffer(body).toString('base64');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** save only strings to cache **/
|
|
194
|
+
if ( typeof body !== 'string' ) {
|
|
195
|
+
return ret;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Create the new cache **/
|
|
199
|
+
self.add(name, body, {
|
|
200
|
+
type: (typeof this.getHeader === 'function' ? this.getHeader('content-type') : (this._headers && this._headers['content-type'])),
|
|
201
|
+
expire: expirationPolicy(req, res)
|
|
202
|
+
},
|
|
203
|
+
domain.intercept(function (name, cache) {}));
|
|
204
|
+
|
|
205
|
+
return ret;
|
|
206
|
+
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
return next();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
}));
|
|
213
|
+
};
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
return middleware;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return route;
|
|
220
|
+
|
|
221
|
+
}) ();
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
|
|
3
|
+
cache.ls()
|
|
4
|
+
##########
|
|
5
|
+
|
|
6
|
+
This script is supposed to be called via `index.js`
|
|
7
|
+
|
|
8
|
+
**/
|
|
9
|
+
|
|
10
|
+
module.exports = function (
|
|
11
|
+
/* Function */ callback // Callback
|
|
12
|
+
) {
|
|
13
|
+
|
|
14
|
+
/** set in /index.js **/
|
|
15
|
+
var self = this;
|
|
16
|
+
|
|
17
|
+
var domain = require('domain').create();
|
|
18
|
+
|
|
19
|
+
domain.on('error', function (error) {
|
|
20
|
+
callback(error);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
domain.run(function () {
|
|
24
|
+
|
|
25
|
+
/** Tell Redis to fetch the keys name beginning by prefix **/
|
|
26
|
+
self.client.keys(self.prefix + '*', domain.intercept(function (keys) {
|
|
27
|
+
|
|
28
|
+
var size = 0;
|
|
29
|
+
|
|
30
|
+
require('async').parallel(
|
|
31
|
+
|
|
32
|
+
/** for each keys **/
|
|
33
|
+
|
|
34
|
+
keys.map(function (key) {
|
|
35
|
+
return function (cb) {
|
|
36
|
+
|
|
37
|
+
self.get(this.key.replace(new RegExp('^' + self.prefix), ''), cb);
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
.bind({ key: key });
|
|
41
|
+
}),
|
|
42
|
+
|
|
43
|
+
domain.intercept(function (results) {
|
|
44
|
+
results.forEach(function (result) {
|
|
45
|
+
size += require('../sizeof')(result);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
callback(null, size);
|
|
49
|
+
}));
|
|
50
|
+
|
|
51
|
+
}));
|
|
52
|
+
|
|
53
|
+
});
|
|
54
|
+
};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
module.exports = (function () {
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
var config = require('../package.json').config;
|
|
6
|
+
|
|
7
|
+
/** ExpressRedisCache
|
|
8
|
+
*
|
|
9
|
+
* @class
|
|
10
|
+
* @description Main class
|
|
11
|
+
* @extends EventEmitter
|
|
12
|
+
* @arg {Object} options? - Options (view README)
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
function ExpressRedisCache (options) {
|
|
16
|
+
|
|
17
|
+
/** The request options
|
|
18
|
+
*
|
|
19
|
+
* @type Object
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
this.options = options || {};
|
|
23
|
+
|
|
24
|
+
/** The entry name prefix
|
|
25
|
+
*
|
|
26
|
+
* @type String
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
this.prefix = this.options.prefix || config.prefix;
|
|
30
|
+
|
|
31
|
+
/** The host to connect to (default host if null)
|
|
32
|
+
*
|
|
33
|
+
* @type String
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
this.host = this.options.host || "localhost";
|
|
37
|
+
|
|
38
|
+
/** The port to connect to (default port if null)
|
|
39
|
+
*
|
|
40
|
+
* @type Number
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
this.port = this.options.port || "6379";
|
|
44
|
+
|
|
45
|
+
/** The password of Redis server (optional)
|
|
46
|
+
*
|
|
47
|
+
* @type String
|
|
48
|
+
*/
|
|
49
|
+
this.auth_pass = this.options.auth_pass;
|
|
50
|
+
|
|
51
|
+
/** An alias to disable expiration for a specific route
|
|
52
|
+
*
|
|
53
|
+
* var cache = new ExpressRedisCache();
|
|
54
|
+
* cache.route('page', cache.FOREVER); // cache will not expire
|
|
55
|
+
*
|
|
56
|
+
* @type number
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
this.FOREVER = -1;
|
|
60
|
+
|
|
61
|
+
/** Set expiration time in seconds, default is -1 (No expire)
|
|
62
|
+
* @type number
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
this.expire = this.options.expire || this.FOREVER;
|
|
66
|
+
|
|
67
|
+
/** Whether or not express-redis-cache is connected to Redis
|
|
68
|
+
*
|
|
69
|
+
* @type Boolean
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
this.connected = false;
|
|
73
|
+
|
|
74
|
+
/** The Redis Client
|
|
75
|
+
*
|
|
76
|
+
* @type Object (preferably a client from the official Redis module)
|
|
77
|
+
*/
|
|
78
|
+
|
|
79
|
+
this.client = this.options.client || require('redis').createClient(this.port, this.host, { auth_pass: this.auth_pass });
|
|
80
|
+
|
|
81
|
+
/** If client can emit */
|
|
82
|
+
|
|
83
|
+
if ( this.client.on ) {
|
|
84
|
+
this.client.on('error', function (error) {
|
|
85
|
+
this.emit('error', error);
|
|
86
|
+
}.bind(this));
|
|
87
|
+
|
|
88
|
+
this.client.on('connect', function () {
|
|
89
|
+
this.connected = true;
|
|
90
|
+
this.emit('connected', { host: this.host, port: this.port });
|
|
91
|
+
this.emit('message', 'OK connected to redis://' + this.client.address);
|
|
92
|
+
}.bind(this));
|
|
93
|
+
|
|
94
|
+
this.client.on('end', function () {
|
|
95
|
+
this.connected = false;
|
|
96
|
+
this.emit('disconnected', { host: this.host, port: this.port });
|
|
97
|
+
this.emit('message', 'Disconnected from redis://' + this.client.host + ':' + this.client.port);
|
|
98
|
+
}.bind(this));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Extend Event Emitter */
|
|
103
|
+
|
|
104
|
+
require('util').inherits(ExpressRedisCache, require('events').EventEmitter);
|
|
105
|
+
|
|
106
|
+
/** js-comment
|
|
107
|
+
*
|
|
108
|
+
* @method
|
|
109
|
+
* @description Get -
|
|
110
|
+
* @return void{Object}
|
|
111
|
+
* @arg {Object} arg - About arg
|
|
112
|
+
*/
|
|
113
|
+
|
|
114
|
+
ExpressRedisCache.prototype.get = require('./ExpressRedisCache/get');
|
|
115
|
+
|
|
116
|
+
/** js-comment
|
|
117
|
+
*
|
|
118
|
+
* @method
|
|
119
|
+
* @description This is a method
|
|
120
|
+
* @return void{Object}
|
|
121
|
+
* @arg {Object} arg - About arg
|
|
122
|
+
*/
|
|
123
|
+
|
|
124
|
+
ExpressRedisCache.prototype.add = require('./ExpressRedisCache/add');
|
|
125
|
+
|
|
126
|
+
/** js-comment
|
|
127
|
+
*
|
|
128
|
+
* @method
|
|
129
|
+
* @description This is a method
|
|
130
|
+
* @return void{Object}
|
|
131
|
+
* @arg {Object} arg - About arg
|
|
132
|
+
*/
|
|
133
|
+
|
|
134
|
+
ExpressRedisCache.prototype.del = require('./ExpressRedisCache/del');
|
|
135
|
+
|
|
136
|
+
/** js-comment
|
|
137
|
+
*
|
|
138
|
+
* @method
|
|
139
|
+
* @description This is a method
|
|
140
|
+
* @return void{Object}
|
|
141
|
+
* @arg {Object} arg - About arg
|
|
142
|
+
*/
|
|
143
|
+
|
|
144
|
+
ExpressRedisCache.prototype.route = require('./ExpressRedisCache/route');
|
|
145
|
+
|
|
146
|
+
/** js-comment
|
|
147
|
+
*
|
|
148
|
+
* @method
|
|
149
|
+
* @description This is a method
|
|
150
|
+
* @return void{Object}
|
|
151
|
+
* @arg {Object} arg - About arg
|
|
152
|
+
*/
|
|
153
|
+
|
|
154
|
+
ExpressRedisCache.prototype.size = require('./ExpressRedisCache/size');
|
|
155
|
+
|
|
156
|
+
/** js-comment
|
|
157
|
+
*
|
|
158
|
+
* @function
|
|
159
|
+
* @description Factory for ExpressRedisCache
|
|
160
|
+
* @return ExpressRedisCache
|
|
161
|
+
* @arg {Object} arg - About arg
|
|
162
|
+
*/
|
|
163
|
+
|
|
164
|
+
ExpressRedisCache.init = function (options) {
|
|
165
|
+
return new ExpressRedisCache(options);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
return ExpressRedisCache;
|
|
169
|
+
|
|
170
|
+
})();
|
package/lib/sizeof.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/*
|
|
2
|
+
|
|
3
|
+
sizeof.js
|
|
4
|
+
|
|
5
|
+
A function to calculate the approximate memory usage of objects
|
|
6
|
+
|
|
7
|
+
Created by Stephen Morley - http://code.stephenmorley.org/ - and released under
|
|
8
|
+
the terms of the CC0 1.0 Universal legal code:
|
|
9
|
+
|
|
10
|
+
http://creativecommons.org/publicdomain/zero/1.0/legalcode
|
|
11
|
+
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/* Returns the approximate memory usage, in bytes, of the specified object. The
|
|
15
|
+
* parameter is:
|
|
16
|
+
*
|
|
17
|
+
* object - the object whose size should be determined
|
|
18
|
+
*/
|
|
19
|
+
module.exports = function sizeof(object){
|
|
20
|
+
|
|
21
|
+
// initialise the list of objects and size
|
|
22
|
+
var objects = [object];
|
|
23
|
+
var size = 0;
|
|
24
|
+
var key;
|
|
25
|
+
|
|
26
|
+
// loop over the objects
|
|
27
|
+
for (var index = 0; index < objects.length; index ++){
|
|
28
|
+
|
|
29
|
+
// determine the type of the object
|
|
30
|
+
switch (typeof objects[index]){
|
|
31
|
+
|
|
32
|
+
// the object is a boolean
|
|
33
|
+
case 'boolean': size += 4; break;
|
|
34
|
+
|
|
35
|
+
// the object is a number
|
|
36
|
+
case 'number': size += 8; break;
|
|
37
|
+
|
|
38
|
+
// the object is a string
|
|
39
|
+
case 'string': size += 2 * objects[index].length; break;
|
|
40
|
+
|
|
41
|
+
// the object is a generic object
|
|
42
|
+
case 'object':
|
|
43
|
+
|
|
44
|
+
// if the object is not an array, add the sizes of the keys
|
|
45
|
+
if (Object.prototype.toString.call(objects[index]) != '[object Array]'){
|
|
46
|
+
for (key in objects[index]) size += 2 * key.length;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// loop over the keys
|
|
50
|
+
for (key in objects[index]){
|
|
51
|
+
|
|
52
|
+
// determine whether the value has already been processed
|
|
53
|
+
var processed = false;
|
|
54
|
+
for (var search = 0; search < objects.length; search ++){
|
|
55
|
+
if (objects[search] === objects[index][key]){
|
|
56
|
+
processed = true;
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// queue the value to be processed if appropriate
|
|
62
|
+
if (!processed) objects.push(objects[index][key]);
|
|
63
|
+
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// return the calculated size
|
|
71
|
+
return size;
|
|
72
|
+
|
|
73
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,84 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ohos-ports/express-redis-cache",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.1.3-beta.1",
|
|
4
|
+
"description": "A module to make Express interact with Redis (create, get, delete). You can automatically cache all your most popular routes in Redis.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"express-redis-cache": "bin/express-redis-cache.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "mocha --exit --check-leaks -R nyan test"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/ohos-ports/ohos-ports.git",
|
|
15
|
+
"directory": "ports/express-redis-cache/1.1.3"
|
|
16
|
+
},
|
|
17
|
+
"author": {
|
|
18
|
+
"name": "Francois Vespa",
|
|
19
|
+
"url": "https://github.com/co2-git"
|
|
20
|
+
},
|
|
21
|
+
"contributors": [
|
|
22
|
+
{
|
|
23
|
+
"name": "faceair",
|
|
24
|
+
"url": "https://github.com/faceair"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"name": "barwin",
|
|
28
|
+
"url": "https://github.com/barwin"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"name": "rv-kip",
|
|
32
|
+
"url": "https://github.com/rv-kip"
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
"name": "amaurigabriel",
|
|
36
|
+
"url": "https://github.com/amaurigabriel"
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
"name": "benmcmeen",
|
|
40
|
+
"url": "https://github.com/benmcmeen"
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
"name": "pwmckenna",
|
|
44
|
+
"url": "https://github.com/pwmckenna"
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
"name": "ramanpreetnara",
|
|
48
|
+
"url": "https://github.com/ramanpreetnara"
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"name": "mdbox",
|
|
52
|
+
"url": "https://github.com/mdbox"
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"name": "dam1",
|
|
56
|
+
"url": "https://github.com/dam1"
|
|
57
|
+
}
|
|
58
|
+
],
|
|
59
|
+
"license": "MIT",
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"async": "^2.6.1",
|
|
62
|
+
"colors": "^1.3.0",
|
|
63
|
+
"redis": "^2.4.2"
|
|
64
|
+
},
|
|
65
|
+
"directories": {
|
|
66
|
+
"test": "test"
|
|
67
|
+
},
|
|
68
|
+
"config": {
|
|
69
|
+
"prefix": "erc:",
|
|
70
|
+
"type": "text/html"
|
|
71
|
+
},
|
|
72
|
+
"devDependencies": {
|
|
73
|
+
"body-parser": "^1.18.3",
|
|
74
|
+
"express": "^4.16.3",
|
|
75
|
+
"method-override": "^2.3.10",
|
|
76
|
+
"mocha": "^5.2.0",
|
|
77
|
+
"moment": "^2.22.2",
|
|
78
|
+
"request": "^2.87.0",
|
|
79
|
+
"should": "^13.2.1"
|
|
80
|
+
},
|
|
81
|
+
"bugs": {
|
|
82
|
+
"url": "https://github.com/ohos-ports/ohos-ports/issues"
|
|
83
|
+
}
|
|
84
|
+
}
|