From 483fce891d210462727f76e01de0f11c2824d600 Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Tue, 17 Jan 2012 14:43:33 -0500 Subject: [PATCH] Added memcached support --- README.md | 25 +++++++++++++++++-- lib/memcached_document_store.js | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 lib/memcached_document_store.js diff --git a/README.md b/README.md index 6f604e6..3f10a26 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/memcached_document_store.js b/lib/memcached_document_store.js new file mode 100644 index 0000000..e0ce405 --- /dev/null +++ b/lib/memcached_document_store.js @@ -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;