Browse files

Added memcached support

  • Loading branch information...
1 parent 6e4c087 commit 483fce891d210462727f76e01de0f11c2824d600 @seejohnrun seejohnrun committed Jan 17, 2012
Showing with 67 additions and 2 deletions.
  1. +23 −2 README.md
  2. +44 −0 lib/memcached_document_store.js
View
25 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 <john.crepezzi@gmail.com>
View
44 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;

0 comments on commit 483fce8

Please sign in to comment.