🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

cachetree-redis

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cachetree-redis - npm Package Compare versions

Comparing version

to
1.1.0

70

lib/store.js
/**
* Module imports
*/
var redis = require('redis');
var Buffer = require('buffer').Buffer,
redis = require('redis');

@@ -24,2 +25,3 @@ /**

var type,
autoCast,
delimiter = ':',

@@ -37,2 +39,8 @@ client,

});
// Set the automatic cast property
autoCast = options.autoCast !== false;
Object.defineProperty(this, 'autoCast', {
value: autoCast,
writable: false
});
// Create the Redis connection

@@ -73,12 +81,18 @@ if (options.client) {

RedisStore.prototype.get = function() {
var args = slice.call(arguments),
var self = this,
args = slice.call(arguments),
cb = args.pop(),
key = this.cacheKey(args.shift()),
len = args.length,
done = function(err, data) {
};
len = args.length;
if (typeof cb !== 'function') return this;
if (key) {
if (args.length === 0) {
this.client.hgetall(key, cb);
this.client.hgetall(key, function(err, data) {
if (data && data === Object(data)) {
Object.keys(data).forEach(function(value) {
data[value] = self._parse(data[value]);
});
}
cb(err, data);
});
} else {

@@ -89,7 +103,7 @@ this.client.hmget(key, args, function(err, data) {

if (args.length === 1) {
obj = data.shift();
obj = self._parse(data.shift());
} else if (args.length === data.length) {
obj = {};
args.forEach(function(val, index) {
obj[val] = data[index];
obj[val] = self._parse(data[index]);
});

@@ -118,3 +132,4 @@ }

RedisStore.prototype.set = function() {
var args = slice.call(arguments),
var self = this,
args = slice.call(arguments),
cb = args.pop(),

@@ -145,10 +160,15 @@ key = this.cacheKey(args.shift()),

if (len === 1) {
if (args[0] === Object(args[0])) data = args[0];
if (args[0] === Object(args[0])) {
data = args[0];
Object.keys(data).forEach(function(val) {
data[val] = self._stringify(data[val]);
});
}
} else {
data = {};
if (len === 2) {
data[args[0]] = args[1];
data[args[0]] = self._stringify(args[1]);
} else if (len > 2) {
for (var i = 0; i < len; i += 2) {
if (i + 1 < len) data[args[i]] = args[i + 1];
if (i + 1 < len) data[args[i]] = self._stringify(args[i + 1]);
}

@@ -283,1 +303,27 @@ }

};
/**
* Stringify data for Redis
*
* @param {Object|Buffer} value Value to stringify
* @return {String|Buffer} stringified value
*/
RedisStore.prototype._stringify = function(value) {
if (!this.autoCast || Buffer.isBuffer(value) || typeof value === 'string') return value;
return JSON.stringify(value);
};
/**
* Parse a stringified value from Redis
*
* @param {String|Buffer} value Value to parse
* @return {Object|Buffer} parsed value
*/
RedisStore.prototype._parse = function(value) {
if (!this.autoCast || Buffer.isBuffer(value) || typeof value !== 'string') return value;
try {
return JSON.parse(value);
} catch (e) {}
return value;
};

2

package.json
{
"name": "cachetree-redis",
"description": "Redis storage backend for Cachetree",
"version": "1.0.1",
"version": "1.1.0",
"author": "David Wood <bitprobe@gmail.com>",

@@ -6,0 +6,0 @@ "repository": {

@@ -35,2 +35,3 @@ # cachetree-redis [![Build Status](https://secure.travis-ci.org/davidwood/cachetree-redis.png)](http://travis-ci.org/davidwood/cachetree-redis)

* `delimiter`: Redis key delimiter, defaults to `:`
* `autoCast`: Automatically stringify and parse values, defaults to `true`

@@ -37,0 +38,0 @@ The returned store exposes the underlying Redis client through a property named `client`.

/*global describe: true, it:true, beforeEach: true, afterEach: true, before: true, after: true */
var assert = require('assert'),
Buffer = require('buffer').Buffer,
fakeredis = require('fakeredis'),

@@ -36,3 +37,4 @@ client = fakeredis.createClient(),

charlie: 'dash dot dash dot',
delta: 'dash dot dot'
delta: 'dash dot dot',
xray: '13'
}

@@ -135,2 +137,4 @@ },

assert.deepEqual(values, data.icao);
assert.equal(typeof values.xray, 'number');
assert.strictEqual(values.xray, 13);
done();

@@ -140,2 +144,11 @@ });

it('should return numeric values as a number', function(done) {
store.get('icao', 'xray', function(err, value) {
assert.ok(!err);
assert.equal(typeof value, 'number');
assert.strictEqual(value, 13);
done();
});
});
});

@@ -205,6 +218,6 @@

it('should accept an object of field and values', function(done) {
store.set('icao', { alpha: 'dot dash', bravo: 'dash dot dot dot', charlie: 'dash dot dash dot' }, function(err) {
store.set('icao', { alpha: 'dot dash', bravo: 'dash dot dot dot', charlie: 'dash dot dash dot', xray: 13 }, function(err) {
assert.ok(!err);
store.client.hgetall('icao', function(err, data) {
assert.deepEqual(data, { alpha: 'dot dash', bravo: 'dash dot dot dot', charlie: 'dash dot dash dot' });
assert.deepEqual(data, { alpha: 'dot dash', bravo: 'dash dot dot dot', charlie: 'dash dot dash dot', xray: '13' });
done();

@@ -360,3 +373,3 @@ });

store.client.hgetall('icao', function(err, data) {
assert.deepEqual(data, { bravo: 'dash dot dot dot', charlie: 'dash dot dash dot', delta: 'dash dot dot' });
assert.deepEqual(data, { bravo: 'dash dot dot dot', charlie: 'dash dot dash dot', delta: 'dash dot dot', xray: '13' });
done();

@@ -371,3 +384,3 @@ });

store.client.hgetall('icao', function(err, data) {
assert.deepEqual(data, { bravo: 'dash dot dot dot', charlie: 'dash dot dash dot', delta: 'dash dot dot' });
assert.deepEqual(data, { bravo: 'dash dot dot dot', charlie: 'dash dot dash dot', delta: 'dash dot dot', xray: '13' });
done();

@@ -382,3 +395,3 @@ });

store.client.hgetall('icao', function(err, data) {
assert.deepEqual(data, { bravo: 'dash dot dot dot', delta: 'dash dot dot' });
assert.deepEqual(data, { bravo: 'dash dot dot dot', delta: 'dash dot dot', xray: '13' });
done();

@@ -393,3 +406,3 @@ });

store.client.hgetall('icao', function(err, data) {
assert.deepEqual(data, { bravo: 'dash dot dot dot', delta: 'dash dot dot' });
assert.deepEqual(data, { bravo: 'dash dot dot dot', delta: 'dash dot dot', xray: '13' });
done();

@@ -399,3 +412,3 @@ });

});
});

@@ -495,2 +508,42 @@

describe('._parse(value)', function() {
it('should not parse the value if the autoCast property is false', function() {
var inst = new RedisStore({ client: store.client, autoCast: false });
assert.strictEqual(inst._parse('1234'), '1234');
});
it('should not parse the value if a buffer', function() {
var inst = new RedisStore({ client: store.client }),
buffer = new Buffer(100);
assert.strictEqual(inst._parse(buffer), buffer);
});
it('should parse string values', function() {
var inst = new RedisStore({ client: store.client });
assert.strictEqual(inst._parse('1234'), 1234);
});
});
describe('._stringify(value)', function() {
it('should not stringify the value if the autoCast property is false', function() {
var inst = new RedisStore({ client: store.client, autoCast: false });
assert.strictEqual(inst._stringify(1234), 1234);
});
it('should not stringify the value if a buffer', function() {
var inst = new RedisStore({ client: store.client }),
buffer = new Buffer(100);
assert.strictEqual(inst._stringify(buffer), buffer);
});
it('should stringify string values', function() {
var inst = new RedisStore({ client: store.client });
assert.strictEqual(inst._stringify(1234), '1234');
});
});
});