Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional Cache Value Compression #2

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

var mongodb = require('mongodb')
, Promise = require('./promise')
, noop = function () {};
, noop = function () {}
, zlib = require('zlib');

/**
* Export `MongoStore`.
Expand All @@ -29,6 +30,7 @@ function MongoStore(options, bucket) {
this.coll = options.collection || bucket._name || 'cacheman';
this.collection = null;
this.connected = false;
this.compression = options.compression || false;

var store = this
, db = options.db || 'cache'
Expand Down Expand Up @@ -82,6 +84,10 @@ MongoStore.prototype.get = function get(key, fn) {
return fn(null, null);
}
try {
if(data.compressed){
return decompressDataValue(data.value, fn);
}

fn(null, data.value);
} catch (err) {
fn(err);
Expand Down Expand Up @@ -124,11 +130,13 @@ MongoStore.prototype.set = function set(key, val, ttl, fn) {
fn(err);
}

store.conn.then(function then(err, db){
db.collection(store.coll).update(query, data, options, function update(err, data) {
if (err) return fn(err);
if (!data) return fn(null, null);
fn(null, val);
compressDataValue(store, data, function(err, data){
store.conn.then(function then(err, db){
db.collection(store.coll).update(query, data, options, function update(err, data) {
if (err) return fn(err);
if (!data) return fn(null, null);
fn(null, val);
});
});
});
};
Expand Down Expand Up @@ -170,3 +178,31 @@ MongoStore.prototype.clear = function clear(key, fn) {
db.collection(store.coll).remove({}, { safe: true }, fn);
});
};

/**
* Non-exported Helpers
*/

var compressDataValue = function(opts, data, callback){
// Compression option turned off
if(!opts.compression) return callback(null, data);

// Data is not of a "compressable" type (currently only Buffer)
if(!Buffer.isBuffer(data.value)) return callback(null, data);

zlib.gzip(data.value, function(err, compressedvalue){
// If compression was successful, then use the compressed data.
// Otherwise, save the original data.
if(!err) {
data.value = compressedvalue;
data.compressed = true;
}

callback(err, data);
});
};

var decompressDataValue = function(value, callback){
var v = (value.buffer && Buffer.isBuffer(value.buffer)) ? value.buffer : value;
zlib.gunzip(v, callback);
};
Binary file added test/large.bin
Binary file not shown.
55 changes: 55 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var assert = require('assert')
, fs = require('fs')
, crypto = require('crypto')
, Cache = require('../')
, cache;

Expand Down Expand Up @@ -117,4 +119,57 @@ describe('cacheman-mongo', function () {
});
});

});

describe('cacheman-mongo compression', function () {

before(function(done){
cache = new Cache({ compression: true }, {});
done();
});

after(function(done){
cache.clear('test');
done();
});

it('should store compressable item compressed', function (done) {
var value = Date.now().toString();

cache.set('test1', new Buffer(value), function (err) {
if (err) return done(err);
cache.get('test1', function (err, data) {
if (err) return done(err);
assert.equal(data.toString(), value);
done();
});
});
});

it('should store non-compressable item normally', function (done) {
var value = Date.now().toString();

cache.set('test1', value, function (err) {
if (err) return done(err);
cache.get('test1', function (err, data) {
if (err) return done(err);
assert.equal(data, value);
done();
});
});
});

it('should store large compressable item compressed', function (done) {
var value = fs.readFileSync('./test/large.bin'), // A file larger than the 16mb MongoDB document size limit
md5 = function(d){ return crypto.createHash('md5').update(d).digest('hex'); };

cache.set('test1', value, function (err) {
if (err) return done(err);
cache.get('test1', function (err, data) {
if (err) return done(err);
assert.equal(md5(data), md5(value));
done();
});
});
});
});