dynamis
v0.3.0
Published
Node.JS cache API for various persistence layers
Downloads
14
Maintainers
Readme
Dynamis
Node.JS cache API for various persistence layers. Dynamis aims to implement a minimal cache API with maximal reusability. If you don't want to bother with specifics Dynamis is made for you. However, if you like full control and features than you should want to use any persistence layer directly.
Installation and usage
npm install dynamis --save
Dynamis does not depend on the supported persistence layers to keep the amount of dependencies small. You'll have to add your desired persistence layer to t
var Dynamis = require('dynamis');
, redis = require('redis').createClient();
//
// Initialize cache layer, by providing the connection object and options.
//
var dynamis = new Dynamis('redis', redis, { database: 1 });
Supported
- Memory: In process memory.
- Memcached: Memcached
- Redis: Node-redis
- CouchDB: Cradle
- LevelDB: LevelUp
Table of Contents
Any persistence layer will have access to the following methods, newly developed layers should implement all methods below.
API
Internal
Events
Dynamis: get
Get a cached value from the persistence layer.
key: String (required) database key done: Function (required) completion callback
dynamis.get('key', function done(error, value) {
console.log(value);
});
Dynamis: set
Store key:value data in the persistence layer, with an optional time to live.
key: String (required) database key value: String (required) value to JSON.stringify ttl: Number (optional) time to live in seconds, defaults to 0 (never) done: Function (required) completion callback
var value = { valid: 'json' };
dynamis.set('key', value, function done(error, result) {
console.log(result);
});
Dynamis: expire
Set or refresh a time to live on the key.
key: String (required) database key ttl: Number (required) time to live in seconds done: Function (required) completion callback
dynamis.expire('key', 10, function done(error, result) {
console.log(result);
});
Dynamis: del
Delete the key and value from the persistence layer
key: String (required) database key done: Function (required) completion callback
dynamis.del('key', function done(error, result) {
console.log(result);
});
Dynamis: flush
Flush all data that is in the persistence layer. This feature is also available by
setting a environment variable, per example CACHE=flush:redis
would flush all
data from the redis database by adding flush
as before hook.
done: Function (required) completion callback
dynamis.flush(function done(error, result) {
console.log(result);
});
Dynamis.execute
All API calls will flow through this function. Execute
will emit before
so that
any registered functions will be executed. before
will only be
run once, thereafter any provided function will executed immediately.
context: Object (required) usually the persistence layer fn: Function (required) persistence layer method to call on context arguments: Mixed (optional) additional arguments to supply to the function
dynamis.execute(redis.database, redis.database.set, key, value, done)
Dynamis.before
Loops over a set of API functions defined in dynamis.pre
. Before will be executed
once, as soon as any API method is called, per example dynamis.create
in cradle.
context: Object (required) usually the persistence layer fn: Function (required) persistence layer method to call on context args: Array (required) arguments to supply to the function
dynamis.before(redis.database, redis.database.set, [ key, value, done ])
Dynamis.on: error
Errors or failures emitted by the persistence layer will be emitted from dynamis. Handling connection or persistence errors from any layer will be done for you. Ignoring these errors is possible, EventEmitter3 will not throw the error when no listener is registered.
var Dynamis = require('dynamis');
, redis = require('redis').createClient();
//
// Initialize cache layer and listen to emitted errors.
//
var dynamis = new Dynamis('redis', redis, {});
dynamis.on('error', function handleError() {
console.log(arguments);
});