Skip to content

Commit

Permalink
limited redis support
Browse files Browse the repository at this point in the history
  • Loading branch information
qzaidi committed Jan 28, 2014
1 parent 1aa5b1e commit 9b6a3c9
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 58 deletions.
9 changes: 8 additions & 1 deletion index.js
Expand Up @@ -36,8 +36,15 @@ var cache = {
Create: function(options) {
var nextResetTime;
var anonFnId = 0;
var store;

var store = this.store = lru.init(options);
if (options && options.redis) {
store = require('./redis').init(options);
} else {
store = require('./lru').init(options);
}

this.store = store;
this.stats = { hit: 0, miss: 0, reset: 0};

if (options && options.reset) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "obcache",
"version": "0.0.6",
"version": "0.0.7",
"description": "Object Caching",
"main": "index.js",
"scripts": {
Expand Down
101 changes: 51 additions & 50 deletions redis.js
Expand Up @@ -3,67 +3,68 @@
var redis = require('redis');

var redisStore = {
prefix: 'obc:',

maxAge: 60000,
init: function(options) {

client: null,
var client = redis.createClient(options.redis);
var maxAge = options && options.maxAge || 60000;
var prefix = 'obc:';

init: function(options) {
this.maxAge = options.maxAge || 60000;
this.client = redis.createClient(options.redis);
},
var rcache = {
maxAge : maxAge,
client : client,
get : function(key, cb) {
key = prefix + key;
var ttl = this.maxAge/1000;
client.get(key, function(err, data){
var result;
if (err || !data) {
return cb(err);
}
data = data.toString();
try {
result = JSON.parse(data);
} catch (e) {
return cb(e);
}
client.expire(key,ttl);
return cb(null, result);
});
},

get : function(key, cb){
key = this.prefix + key;
var client = this.client;
var ttl = this.maxAge/1000;
client.get(key, function(err, data){
var result;
if (err || !data) {
return cb(err);
}
data = data.toString();
try {
result = JSON.parse(data);
} catch (e) {
return cb(e);
}
client.expire(key,ttl);
return cb(null, result);
});
},
set : function(key, val, cb){
key = prefix + key;
try {
var ttl = this.maxAge/1000;
var obj = JSON.stringify(val);

set : function(key, val, cb){
key = this.prefix + key;
try {
var ttl = this.maxAge/1000;
var obj = JSON.stringify(val);
client.setex(key, ttl, obj, function(err){
if (cb) {
cb.apply(this, arguments);
}
});
} catch (err) {
if (cb) {
cb(err);
}
}
},

this.client.setex(key, ttl, obj, function(err){
if (cb) {
cb.apply(this, arguments);
}
});
} catch (err) {
if (cb) {
cb(err);
}
}
},
reset: function(key,val,cb) {

reset: function(key,val,cb) {
},

},
size: function() {
return 0;
},

size: function() {
return 0;
},
keycount: function() {
return 0;
}
};
return rcache;

keycount: function() {
return 0;
}

};

module.exports = redisStore;
5 changes: 5 additions & 0 deletions test/benchmark.js
Expand Up @@ -6,6 +6,7 @@

var obcache = require('../index');
var cache = new obcache.Create();
var rcache = new obcache.Create({ redis: { port: 6379 } });
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite();

Expand All @@ -16,13 +17,17 @@ var suite = new Benchmark.Suite();
});
};
var wrapped = cache.wrap(original);
var rwrapped = rcache.wrap(original);

suite.add('uncached', function() {
original(5,function(){});
})
.add('cached', function() {
wrapped(5,function() {});
})
.add('redis', function() {
wrapped(5,function() {});
})
.on('cycle', function(event) {
console.log(String(event.target));
})
Expand Down
36 changes: 30 additions & 6 deletions test/redis.js
@@ -1,8 +1,32 @@
var redis = require('../redis');
"use strict";

redis.create({ maxAge: 60000 });
redis.set('hello', { world: 1, universe: 20 });
var obcache = require('../index');
var debug = require('../debug');

setTimeout(function() {
redis.get('hello',console.log);
},1000);
var cache = debug.register(new obcache.Create({ reset: { interval: 2000, firstReset: new Date(Date.now() + 1000) }, redis: { port: 6379 } }),'redis');

(function() {
var original = function (id,cb) {
process.nextTick(function() {
cb(null,id);
});
};
var wrapped = cache.wrap(original);

original(5,console.log);
wrapped(5,console.log);

// this should find it in cache
process.nextTick(function() {
wrapped(5,console.log)
debug.log();
});

setTimeout(function() {
wrapped(5,console.log);
},5000);

setTimeout(function() {
debug.log();
},15000);
}());

0 comments on commit 9b6a3c9

Please sign in to comment.