Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for latest #3

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# connect-memcached

Memcached session store, using [node-memcached](http://github.com/3rd-Eden/node-memcached) for communication with cache server.
Expand All @@ -9,10 +8,10 @@

$ npm install connect-memcached

## Example
## Example in Connect

var connect = require('connect');
var MemcachedStore = require('connect-memcached');
var MemcachedStore = require('connect-memcached')(connect);

connect.createServer(
connect.cookieParser(),
Expand All @@ -27,6 +26,25 @@
secret: 'thisissosick'
})
);

## Example in Express JS


var express = require('express');
var MemcachedStore = require('connect-memcached')(express);

app.configure(function(){
app.use(express.session({
store: new MemcachedStore({
hosts: [
'192.168.1.65:11213',
'192.168.1.66:11213',
'192.168.1.67:11213'
]
}),
secret: 'thisissosick'
}));
});

## Options

Expand Down
241 changes: 125 additions & 116 deletions lib/connect-memcached.js
Original file line number Diff line number Diff line change
@@ -1,131 +1,140 @@

/*!
* connect-memcached
* Copyright(c) 2011 Michał Thoma <michal@balor.pl>
* MIT Licensed
*/
* connect-memcached
* Copyright(c) 2011 Michał Thoma <michal@balor.pl>
* MIT Licensed
*/

/**
* Library version.
*/
* Library version.
*/

exports.version = '0.0.2';


/**
* Module dependencies.
*/

var Store = require('connect').session.Store;
* Module dependencies.
*/
var Memcached = require('memcached');

/**
* One day in seconds.
*/
* One day in seconds.
*/

var oneDay = 86400;

/**
* Initialize MemcachedStore with the given `options`.
*
* @param {Object} options
* @api public
*/

var MemcachedStore = module.exports = function MemcachedStore(options) {
options = options || {};
Store.call(this, options);
if (!options.hosts) {
options.hosts = '127.0.0.1:11211';
}
this.client = new Memcached(options.hosts, options);
console.log("MemcachedStore initialized for servers: " + options.hosts);

this.client.on("issue", function(issue) {
console.log("MemcachedStore::Issue @ " + issue.server + ": " +
issue.messages + ", " + issue.retries + " attempts left");
});
};

/**
* Inherit from `Store`.
*/

MemcachedStore.prototype.__proto__ = Store.prototype;

/**
* Attempt to fetch session by the given `sid`.
*
* @param {String} sid
* @param {Function} fn
* @api public
*/

MemcachedStore.prototype.get = function(sid, fn) {
this.client.get(sid, function(err, data) {
try {
if (!data) {
return fn();
}
fn(null, JSON.parse(data.toString()));
} catch (err) {
fn(err);
}
});
};

/**
* Commit the given `sess` object associated with the given `sid`.
*
* @param {String} sid
* @param {Session} sess
* @param {Function} fn
* @api public
*/

MemcachedStore.prototype.set = function(sid, sess, fn) {
try {
var maxAge = sess.cookie.maxAge
var ttl = 'number' == typeof maxAge ? maxAge / 1000 | 0 : oneDay
var sess = JSON.stringify(sess);

this.client.set(sid, sess, ttl, function() {
fn && fn.apply(this, arguments);
});
} catch (err) {
fn && fn(err);
}
};

/**
* Destroy the session associated with the given `sid`.
*
* @param {String} sid
* @api public
*/

MemcachedStore.prototype.destroy = function(sid, fn) {
this.client.del(sid, fn);
};

/**
* Fetch number of sessions.
*
* @param {Function} fn
* @api public
*/

MemcachedStore.prototype.length = function(fn) {
this.client.items(fn);
};

/**
* Clear all sessions.
*
* @param {Function} fn
* @api public
*/

MemcachedStore.prototype.clear = function(fn) {
this.client.flush(fn);
};
* Initialize MemcachedStore with the given `options`.
*
* @param {Object} options
* @api public
*/

module.exports = function(connect) {
var Store = connect.session.Store;

function MemcachedStore(options) {
options = options || {};
Store.call(this, options);
this.prefix = null == options.prefix
? 'sess:'
: options.prefix;
if (!options.hosts) {
options.hosts = '127.0.0.1:11211';
}
this.client = new Memcached(options.hosts, options);
console.log("MemcachedStore initialized for servers: " + options.hosts);

this.client.on("issue", function(issue) {
console.log("MemcachedStore::Issue @ " + issue.server + ": " +
issue.messages + ", " + issue.retries + " attempts left");
});
};

/**
* Inherit from `Store`.
*/

MemcachedStore.prototype.__proto__ = Store.prototype;

/**
* Attempt to fetch session by the given `sid`.
*
* @param {String} sid
* @param {Function} fn
* @api public
*/

MemcachedStore.prototype.get = function(sid, fn) {
sid = this.prefix + sid;
this.client.get(sid, function(err, data) {
try {
if (!data) {
return fn();
}
fn(null, JSON.parse(data.toString()));
} catch (err) {
fn(err);
}
});
};

/**
* Commit the given `sess` object associated with the given `sid`.
*
* @param {String} sid
* @param {Session} sess
* @param {Function} fn
* @api public
*/

MemcachedStore.prototype.set = function(sid, sess, fn) {
sid = this.prefix + sid;
try {
var maxAge = sess.cookie.maxAge
var ttl = 'number' == typeof maxAge ? maxAge / 1000 | 0 : oneDay
var sess = JSON.stringify(sess);

this.client.set(sid, sess, ttl, function() {
fn && fn.apply(this, arguments);
});
} catch (err) {
fn && fn(err);
}
};

/**
* Destroy the session associated with the given `sid`.
*
* @param {String} sid
* @api public
*/

MemcachedStore.prototype.destroy = function(sid, fn) {
sid = this.prefix.sid
this.client.del(sid, fn);
};

/**
* Fetch number of sessions.
*
* @param {Function} fn
* @api public
*/

MemcachedStore.prototype.length = function(fn) {
this.client.items(fn);
};

/**
* Clear all sessions.
*
* @param {Function} fn
* @api public
*/

MemcachedStore.prototype.clear = function(fn) {
this.client.flush(fn);
};

return MemcachedStore;
};