Skip to content

Commit 58ee7a4

Browse files
author
Jonathan Muller
committed
Merge branch 'release/0.3.0'
2 parents b7e99e9 + 98cf024 commit 58ee7a4

File tree

4 files changed

+490
-10
lines changed

4 files changed

+490
-10
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,55 @@ var redisCache = cacheManager.caching({
138138
// proceed with redisCache
139139
```
140140

141+
### Seamless compression (currently only supports Node's built-in zlib / gzip implementation)
142+
143+
```js
144+
// Compression can be configured for the entire cache.
145+
var redisCache = cacheManager.caching({
146+
store: redisStore,
147+
host: 'localhost', // default value
148+
port: 6379, // default value
149+
auth_pass: 'XXXXX',
150+
db: 0,
151+
ttl: 600,
152+
compress: true
153+
});
154+
155+
// Or on a per command basis. (only applies to get / set commands)
156+
redisCache.set('foo', 'bar', { compress: false }, function(err) {
157+
if (err) {
158+
throw err;
159+
}
160+
161+
redisCache.get('foo', { compress: false }, function(err, result) {
162+
console.log(result);
163+
// >> 'bar'
164+
redisCache.del('foo', function(err) {});
165+
});
166+
});
167+
168+
// Setting the compress option to true will enable a default configuration
169+
// for best speed using gzip. For advanced use, a configuration object may
170+
// also be passed with implementation-specific parameters. Currently, only
171+
// the built-in zlib/gzip implementation is supported.
172+
var zlib = require('zlib');
173+
var redisCache = cacheManager.caching({
174+
store: redisStore,
175+
host: 'localhost', // default value
176+
port: 6379, // default value
177+
auth_pass: 'XXXXX',
178+
db: 0,
179+
ttl: 600,
180+
compress: {
181+
type: 'gzip',
182+
params: {
183+
level: zlib.Z_BEST_COMPRESSION
184+
}
185+
}
186+
});
187+
```
188+
Currently, all implementation-specific configuration parameters are passed directly to the `zlib.gzip` and `zlib.gunzip` methods. Please see the [Node Zlib documentation](https://nodejs.org/dist/latest-v6.x/docs/api/zlib.html#zlib_class_options) for available options.
189+
141190
Tests
142191
-----
143192

index.js

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var RedisPool = require('sol-redis-pool');
44
var EventEmitter = require('events').EventEmitter;
55
var redisUrl = require('redis-url');
6+
var zlib = require('zlib');
67

78
/**
89
* The cache manager Redis Store module
@@ -12,6 +13,11 @@ var redisUrl = require('redis-url');
1213
* @param {Number} args.port - The Redis server port
1314
* @param {Number} args.db - The Redis server db
1415
* @param {function} args.isCacheableValue - function to override built-in isCacheableValue function (optional)
16+
* @param {boolean|Object} args.compress - (optional) Boolean / Config Object for pluggable compression.
17+
* Setting this to true will use a default gzip configuration for best speed. Passing in a config
18+
* object will forward those settings to the underlying compression implementation. Please see the
19+
* Node zlib documentation for a list of valid options for gzip:
20+
* https://nodejs.org/dist/latest-v4.x/docs/api/zlib.html#zlib_class_options
1521
*/
1622
function redisStore(args) {
1723
var self = {
@@ -28,6 +34,20 @@ function redisStore(args) {
2834
redisOptions.port = redisOptions.port || 6379;
2935
redisOptions.db = redisOptions.db || 0;
3036

37+
// default compress config
38+
redisOptions.detect_buffers = true;
39+
var compressDefault = {
40+
type: 'gzip',
41+
params: {
42+
level: zlib.Z_BEST_SPEED
43+
}
44+
};
45+
46+
// if compress is boolean true, set default
47+
if (redisOptions.compress === true) {
48+
redisOptions.compress = compressDefault;
49+
}
50+
3151
var pool = new RedisPool(redisOptions, poolSettings);
3252

3353
pool.on('error', function(err) {
@@ -61,16 +81,30 @@ function redisStore(args) {
6181
}
6282

6383
if (opts.parse) {
84+
85+
if (result && opts.compress) {
86+
return zlib.gunzip(result, opts.compress.params || {}, function (gzErr, gzResult) {
87+
if (gzErr) {
88+
return cb && cb(gzErr);
89+
}
90+
try {
91+
gzResult = JSON.parse(gzResult);
92+
} catch (e) {
93+
return cb && cb(e);
94+
}
95+
96+
return cb && cb(null, gzResult);
97+
});
98+
}
99+
64100
try {
65101
result = JSON.parse(result);
66102
} catch (e) {
67103
return cb && cb(e);
68104
}
69105
}
70106

71-
if (cb) {
72-
cb(null, result);
73-
}
107+
return cb && cb(null, result);
74108
};
75109
}
76110

@@ -138,21 +172,28 @@ function redisStore(args) {
138172
* @method get
139173
* @param {String} key - The cache key
140174
* @param {Object} [options] - The options (optional)
175+
* @param {boolean|Object} options.compress - compression configuration
141176
* @param {Function} cb - A callback that returns a potential error and the response
142177
*/
143178
self.get = function(key, options, cb) {
144179
if (typeof options === 'function') {
145180
cb = options;
181+
options = {};
182+
}
183+
options.parse = true;
184+
185+
var compress = (options.compress || options.compress === false) ? options.compress : redisOptions.compress;
186+
if (compress) {
187+
options.compress = (compress === true) ? compressDefault : compress;
188+
key = new Buffer(key);
146189
}
147190

148191
connect(function(err, conn) {
149192
if (err) {
150193
return cb && cb(err);
151194
}
152195

153-
conn.get(key, handleResponse(conn, cb, {
154-
parse: true
155-
}));
196+
conn.get(key, handleResponse(conn, cb, options));
156197
});
157198
};
158199

@@ -163,6 +204,7 @@ function redisStore(args) {
163204
* @param {String} value - The value to set
164205
* @param {Object} [options] - The options (optional)
165206
* @param {Object} options.ttl - The ttl value
207+
* @param {boolean|Object} options.compress - compression configuration
166208
* @param {Function} [cb] - A callback that returns a potential error, otherwise null
167209
*/
168210
self.set = function(key, value, options, cb) {
@@ -179,16 +221,34 @@ function redisStore(args) {
179221
options = options || {};
180222

181223
var ttl = (options.ttl || options.ttl === 0) ? options.ttl : redisOptions.ttl;
224+
var compress = (options.compress || options.compress === false) ? options.compress : redisOptions.compress;
225+
if (compress === true) {
226+
compress = compressDefault;
227+
}
182228

183229
connect(function(err, conn) {
184230
if (err) {
185231
return cb && cb(err);
186232
}
187233
var val = JSON.stringify(value) || '"undefined"';
188-
if (ttl) {
189-
conn.setex(key, ttl, val, handleResponse(conn, cb));
234+
235+
// Refactored to remove duplicate code.
236+
function persist(pErr, pVal) {
237+
if (pErr) {
238+
return cb && cb(pErr);
239+
}
240+
241+
if (ttl) {
242+
conn.setex(key, ttl, pVal, handleResponse(conn, cb));
243+
} else {
244+
conn.set(key, pVal, handleResponse(conn, cb));
245+
}
246+
}
247+
248+
if (compress) {
249+
zlib.gzip(val, compress.params || {}, persist);
190250
} else {
191-
conn.set(key, val, handleResponse(conn, cb));
251+
persist(null, val);
192252
}
193253
});
194254
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cache-manager-redis",
3-
"version": "0.2.6",
3+
"version": "0.3.0",
44
"description": "Redis store for the node-cache-manager",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)