Skip to content
Open
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
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ http.createServer(app).listen(9341, function() {
- `ttl` An optional parameter used for setting the default TTL
- `secret` An optional secret can be used to encrypt/decrypt session contents.
- `algorithm` An optional algorithm parameter may be used, but must be valid based on returned `crypto.getCiphers()`. The current default is `aes-256-ctr` and was chosen based on the following [information](http://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html)
- `serverSupportsTouch` Set true if your memcached server version is at least 1.4.14 to enable the more efficient touch protocol.
- ... Rest of given option will be passed directly to the node-memcached constructor.

For details see [node-memcached](http://github.com/3rd-Eden/node-memcached).
Expand Down
36 changes: 31 additions & 5 deletions lib/connect-memcached.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = function(session) {
options = options || {};
Store.call(this, options);

this.serverSupportsTouch = options.serverSupportsTouch;
this.prefix = options.prefix || '';
this.ttl = options.ttl;
if (!options.client) {
Expand Down Expand Up @@ -107,8 +108,7 @@ module.exports = function(session) {
sid = this.getKey(sid);

try {
var maxAge = sess.cookie.maxAge;
var ttl = this.ttl || ('number' == typeof maxAge ? maxAge / 1000 | 0 : oneDay);
var ttl = computeTtl.call(this, sess);
var sess = JSON.stringify((this.secret) ?
encryptData.call(this, JSON.stringify(sess),
this.secret, this.algorithm) :
Expand Down Expand Up @@ -161,10 +161,36 @@ module.exports = function(session) {
* @api public
*/

MemcachedStore.prototype.touch = function (sid, sess, fn) {
this.set(sid, sess, fn);
}
MemcachedStore.prototype.touch = function (sid, sess, fn) {
sid = this.getKey(sid);

try {
var ttl = computeTtl.call(this, sess);
var self = this;

// memcached.touch support introduced in 1.4.8 and buggy until 1.4.14
// when touch not supported implement it using get+set as modelled in MemoryStore
// Memcached.version is not robust so we use options flag to disable work-around

if (this.serverSupportsTouch) {
this.client.touch(sid, ttl, ensureCallback(fn));
} else {
self.client.get(sid, function(err, data) {
if (err) {
return fn && fn(err);
}
self.client.set(sid, data, ttl, ensureCallback(fn));
});
}
} catch (err) {
fn && fn(err);
}
}

function computeTtl(sess) {
var maxAge = sess.cookie.maxAge;
return this.ttl || ('number' == typeof maxAge ? maxAge / 1000 | 0 : oneDay);
}

function encryptData(plaintext){
var pt = encrypt.call(this, this.secret, plaintext, this.algo)
Expand Down