Comparing version 0.2.2 to 0.3.0
module.exports = require('./lib/cashbox')({ | ||
'memory': require('./lib/stores/memory') | ||
'memory': require('./lib/stores/memory'), | ||
'redis': require('./lib/stores/redis') | ||
}); |
@@ -61,4 +61,7 @@ var extend = require('deap').extendShallow, | ||
done(null, loadedValue); | ||
self.set(key, loadedValue, tags, ttl, noop); | ||
self.set(key, loadedValue, tags, ttl, function(err, status) { | ||
if(err) return done(err); | ||
done(null, loadedValue); | ||
}); | ||
}); | ||
@@ -120,4 +123,8 @@ }); | ||
done(null, allthethings); | ||
self.mset(loaded, tagMap, ttl, noop); | ||
// done(null, allthethings); | ||
self.mset(loaded, tagMap, ttl, function(err, status) { | ||
if(err) return done(err); | ||
done(null, allthethings); | ||
}); | ||
}); | ||
@@ -204,3 +211,2 @@ }); | ||
load(missingKeys, function(err, values, tags) { | ||
if(!err && !(values instanceof Array)) err = new Error('Invalid values passed into load function: "values" must be an array'); | ||
@@ -207,0 +213,0 @@ if(err) return done(err); |
{ | ||
"name": "cashbox", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "javascript cache library with configurable storage", | ||
"main": "index.js", | ||
"browser": "browser.js", | ||
"scripts": { | ||
@@ -17,3 +18,4 @@ "test": "./node_modules/.bin/mocha test/*.test.js" | ||
"cache", | ||
"memory" | ||
"memory", | ||
"redis" | ||
], | ||
@@ -32,7 +34,8 @@ "author": "Brad Harris <bmharris@gmail.com> (http://selfcontained.us)", | ||
], | ||
"license": "BSD", | ||
"license": "MIT", | ||
"readmeFilename": "README.md", | ||
"dependencies": { | ||
"deap": "~0.2.0", | ||
"timestr": "~0.1.0" | ||
"timestr": "~0.1.0", | ||
"redis": "~0.8.5" | ||
}, | ||
@@ -39,0 +42,0 @@ "devDependencies": { |
@@ -10,1 +10,89 @@ [](http://ci.testling.com/selfcontained/cashbox) | ||
javascript cache library with configurable storage | ||
--- | ||
```bash | ||
npm install cashbox | ||
``` | ||
Cashbox provides a common caching api on top of pluggable backend stores. **memory** and **redis** are currently the two supported stores, with **memory** being the default. `Cashbox` fully supports custom stores as long as they implement the correct api (see [the memory store](https://github.com/selfcontained/cashbox/blob/master/lib/stores/memory.js) for an example of what to implement). | ||
Using cashbox is pretty simple: | ||
```javascript | ||
var Cashbox = require('cashbox'); | ||
// creates an in-memory cache by defaut | ||
var cache = new Cashbox(); | ||
cache.set('myKey', 'myValue', function(err, wasSet) { | ||
cache.get('myKey', function(err, value) { | ||
console.log(value); //myValue | ||
}); | ||
}); | ||
// you can also set a ttl in seconds or a time string | ||
cache.set('myKey', 'myValue', 10, function(err, wasSet) { | ||
}); | ||
cache.set('myKey', 'myValue', '1 hour', function(err, wasSet) { | ||
}); | ||
``` | ||
## Cashbox API | ||
--- | ||
### Cashbox(config) constructor | ||
The `Cashbox` constructor accepts an optional config object | ||
+ **type** can be set to specify cache store type. `memory` is the default, `redis` is also supported | ||
+ **store** can be set to either an instance, or a constructor for a backend cache store. The constructor will be passed the config object should it need any special configuration. | ||
```javascript | ||
// a custom store instance, should implement the same api as the memory/redis store | ||
var myCustomStore = new MyCustomStore(); | ||
// you can pass in the instance | ||
var cache = new Cashbox({ store: myCustomStore }); | ||
// or the constructor - it will receive this same config object when instantiated | ||
cache = new Cashbox({ store: MyCustomStore, foo: 'bar' }); | ||
``` | ||
+ **host** can be set for `redis` store. Defaults to `localhost` | ||
+ **port** can be set for `redis` store. Defaults to `6379` | ||
+ **options** can be set for `redis` store. These are connection options passed into the [`redis.createClient(host, port, options)`](https://github.com/mranney/node_redis#rediscreateclientport-host-options) call. | ||
### .get(key, load, ttl, callback) | ||
+ **key** is a string value used as the cache key | ||
+ **load** - Optionally pass a `load` function that will be called upon a cache miss. | ||
```javascript | ||
// provide a function to load a missing value | ||
function load(key, cb) { | ||
//load value from db or w/e | ||
doSomethingAsync(key, function(err, value) { | ||
if(err) return cb(err); | ||
// cb() expects error first, then value. Optionally an array of tags can be passed in third | ||
cb(null, value); | ||
}); | ||
} | ||
cache.get(key, load, function(err, v) { | ||
// load() will have been called upon a cache miss | ||
console.log(v); // value returned from load() | ||
}); | ||
``` | ||
+ **ttl** is also optional, and may be specified w/ or w/o a load function. Supported formats for ttl are either a value in seconds, or a time string parseable by [timestr](https://github.com/nbroslawsky/timestr) (i.e. *"1 hour"*) | ||
+ **callback** is called upon completion of fetching the value from the cache store. It is passed an error first, and the value. `undefined` is returned on cache misses | ||
var assert = require('chai').assert, | ||
Cache = require('../index'), | ||
async = require('async'), | ||
Memory = require('../lib/stores/memory'); | ||
async = require('async'); | ||
@@ -102,2 +101,2 @@ describe('Cache', function() { | ||
}); | ||
}); | ||
}); |
@@ -129,8 +129,6 @@ var assert = require('chai').assert, | ||
setTimeout(function() { | ||
cache.getKeys('test=1', function(err, keys) { | ||
assert.equal(keys[0], key); | ||
done(); | ||
}); | ||
}, 0); | ||
cache.getKeys('test=1', function(err, keys) { | ||
assert.equal(keys[0], key); | ||
done(); | ||
}); | ||
} | ||
@@ -214,12 +212,8 @@ }); | ||
// check existence after event loop is done | ||
setTimeout(function() { | ||
cache.get(key, function(err, v) { | ||
assert.isNull(err); | ||
assert.equal(v, value); | ||
cache.get(key, function(err, v) { | ||
assert.isNull(err); | ||
assert.equal(v, value); | ||
done(); | ||
}); | ||
}, 0); | ||
done(); | ||
}); | ||
}); | ||
@@ -327,8 +321,6 @@ | ||
setTimeout(function() { | ||
cache.getKeys('test=2', function(err, keys) { | ||
assert.equal(keys[0], key2); | ||
done(); | ||
}); | ||
}, 0); | ||
cache.getKeys('test=2', function(err, keys) { | ||
assert.equal(keys[0], key2); | ||
done(); | ||
}); | ||
} | ||
@@ -338,31 +330,2 @@ }); | ||
it('should accept an object with tagging capabilities (as an array)', function(done) { | ||
var cache = new Cache(); | ||
cache.mget({ | ||
keys: [key1, key2], | ||
ttl: 1, | ||
load: function(keys, cb) { | ||
cb( | ||
null, | ||
[value1, value2], | ||
['test_1', 'test_2'] | ||
); | ||
}, | ||
done: function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.equal(results[0], value1); | ||
assert.equal(results[1], value2); | ||
setTimeout(function() { | ||
cache.getKeys('test_2', function(err, keys) { | ||
assert.equal(keys[0], key2); | ||
done(); | ||
}); | ||
}, 0); | ||
} | ||
}); | ||
}); | ||
it('should accept an object with a string ttl', function(done) { | ||
@@ -369,0 +332,0 @@ var cache = new Cache(); |
Sorry, the diff of this file is not supported yet
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
46187
13
0
1483
98
3
+ Addedredis@~0.8.5
+ Addedredis@0.8.6(transitive)