Added memcached support

This commit is contained in:
John Crepezzi 2012-01-17 14:43:33 -05:00
parent 6e4c087319
commit 483fce891d
2 changed files with 67 additions and 2 deletions

View File

@ -82,9 +82,9 @@ Where `path` represents where you want the files stored
### Redis
To use redis storage you must install the redis package in npm globall using
To use redis storage you must install the redis package in npm
`npm install redis --global`
`npm install redis`
Once you've done that, your config section should look like:
@ -101,6 +101,27 @@ You can also set an `expire` option to the number of seconds to expire keys in.
All of which are optional except `type` with very logical default values.
### Memcached
To use memcached storage you must install the `memcache` package via npm
`npm install memcache`
Once you've done that, your config section should look like:
``` json
{
"type": "memcached",
"host": "127.0.0.1",
"port": 11211
}
```
You can also set an `expire` option to the number of seconds to expire keys in. This behaves just like the redis expirations, but does not push expirations forward on GETs.
All of which are optional except `type` with very logical default values.
## Author
John Crepezzi <john.crepezzi@gmail.com>

View File

@ -0,0 +1,44 @@
var memcached = require('memcache');
var winston = require('winston');
// Create a new store with options
var MemcachedDocumentStore = function(options) {
this.expire = options.expire;
if (!MemcachedDocumentStore.client) {
MemcachedDocumentStore.connect(options);
}
};
// Create a connection
MemcachedDocumentStore.connect = function(options) {
var host = options.host || '127.0.0.1';
var port = options.port || 11211;
this.client = new memcached.Client(port, host);
this.client.connect();
this.client.on('connect', function() {
winston.info('connected to memcached on ' + host + ':' + port);
});
this.client.on('error', function(e) {
winston.info('error connecting to memcached', { error: e });
});
};
// Save file in a key
MemcachedDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
MemcachedDocumentStore.client.set(key, data, function(err, reply) {
err ? callback(false) : callback(true);
}, skipExpire ? 0 : this.expire);
};
// Get a file from a key
MemcachedDocumentStore.prototype.get = function(key, callback, skipExpire) {
var _this = this;
MemcachedDocumentStore.client.get(key, function(err, reply) {
callback(err ? false : reply);
if (_this.expire && !skipExpire) {
winston.warn('memcache store does not currently push forward expirations on GET');
}
});
};
module.exports = MemcachedDocumentStore;