Permalink
Browse files

Merge pull request #1 from mjor/master

Update for uber-cache 0.2.0
  • Loading branch information...
serby committed Jul 9, 2012
2 parents a4425ee + d81c674 commit afb1dcfc5b4baf31aa0cd802906791ebe1f000ca
Showing with 70 additions and 37 deletions.
  1. +2 −2 package.json
  2. +68 −35 redisEngine.js
View
@@ -1,7 +1,7 @@
{ "author": "Paul Serby <paul@serby.net>"
, "name": "uber-cache-redis"
, "description": "Redis Engine for uber-cache"
-, "version": "0.0.1"
+, "version": "0.1.0"
, "tags":
[ "cache"
, "memory"
@@ -23,6 +23,6 @@
, "devDependencies":
{ "mocha": "*"
, "should": "*"
- , "uber-cache": "*"
+ , "uber-cache": ">= 0.2.0"
}
}
View
@@ -1,51 +1,84 @@
var _ = require('underscore')
+ , EventEmitter = require('events').EventEmitter
, redis = require('redis');
module.exports = function(options) {
- var client = redis.createClient();
+ var client = redis.createClient()
+ , handle = new EventEmitter();
- client.on('error', function (err) {
- console.log('Redis Error: ' + err);
+ client.on('error', function(err) {
+ handle.emit('error', err);
});
- options = _.extend({
- size: 1000 // Maximum number of items that can be held in the LRU cache.
- }, options);
+ handle.set = function(key, value, ttl, callback) {
+ var encoded;
- function clear() {
- client.flushdb();
- }
+ if (typeof ttl === 'function') {
+ callback = ttl;
+ ttl = undefined;
+ }
- function del(key, callback) {
- client.del(key, callback);
- }
+ if (typeof key === 'undefined') {
+ var err = new Error('Invalid key undefined');
+ handle.emit('error', err);
+ throw err;
+ }
- return {
- set: function(key, value, ttl, callback) {
- if (typeof ttl === 'function') {
- callback = ttl;
- ttl = undefined;
+ try {
+ encoded = JSON.stringify(value);
+ } catch (err) {
+ if (typeof callback === 'function') {
+ callback(err);
}
- if (typeof key === 'undefined') {
- throw new Error('Invalid key undefined');
+ return;
+ }
+
+ if ((typeof(ttl) === 'number') && (parseInt(ttl, 10) === ttl)) {
+ client.setex(key, ttl, encoded, callback);
+ } else {
+ client.set(key, encoded, callback);
+ }
+ };
+
+ handle.get = function(key, callback) {
+ return client.get(key, function(err, encoded) {
+ var value;
+
+ if (err) {
+ return callback(err);
}
- if ((typeof(ttl) === 'number') && (parseInt(ttl, 10) === ttl)) {
- client.setex(key, ttl, value, callback);
- } else {
- client.set(key, value, callback);
+
+ if (encoded === null) {
+ handle.emit('miss', key);
+ return callback(err, undefined);
}
- },
- get: function(key, callback) {
- return client.get(key, callback);
- },
- del: del,
- clear: clear,
- size: function(callback) {
- client.dbsize(callback);
- },
- dump: function() {
- //?
- }
+
+ try {
+ value = JSON.parse(encoded);
+ } catch (err) {
+ return callback(err);
+ }
+
+ callback(err, value);
+ });
};
+
+ handle.del = function(key, callback) {
+ client.del(key, callback);
+ };
+
+ handle.clear = function() {
+ client.flushdb();
+ };
+
+ handle.size = function(callback) {
+ client.dbsize(callback);
+ };
+
+ handle.dump = function(callback) {
+ // ?
+ };
+
+ return handle;
};

0 comments on commit afb1dcf

Please sign in to comment.