Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix for latest #3

Closed
wants to merge 8 commits into from

3 participants

@pdufour

I also added tabs instead of spaces, but that is just my personal preference.

pdufour added some commits
@pdufour pdufour This plugin wasn't working. Had to pass connect object so sessions ca…
…n be extended. See https://github.com/visionmedia/connect-redis for more detailed explanation of the necessary update.


Also added prefix support.
68c5a69
@pdufour pdufour Woops. was going to make a totally new library, but then i just decid…
…ed to fork. Hence the copyright change.
26cfacb
@balor
Owner

Thank you for the support. I'll check the changes in real life and pull/fix the library asap (probably tomorrow).

@pdufour

No problem, glad to help. Sorry I changed everything to tabs. But the general changes were: passing express / connect to the function, and adding prefix support. I went off the connect-redis as an example, but it seems to all be working fine.

@magwo

Any update on this pull request? Seems like a much-needed commit.

@balor
Owner

Updated, tested, published.

@balor balor closed this
@magwo

Excellent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 9, 2012
  1. @pdufour

    This plugin wasn't working. Had to pass connect object so sessions ca…

    pdufour authored
    …n be extended. See https://github.com/visionmedia/connect-redis for more detailed explanation of the necessary update.
    
    
    Also added prefix support.
  2. @pdufour

    Woops. was going to make a totally new library, but then i just decid…

    pdufour authored
    …ed to fork. Hence the copyright change.
  3. @pdufour
  4. @pdufour

    Update Readme.md

    pdufour authored
  5. @pdufour

    Update Readme.md

    pdufour authored
  6. @pdufour

    Update Readme.md

    pdufour authored
  7. @pdufour

    Update Readme.md

    pdufour authored
  8. @pdufour

    Update Readme.md

    pdufour authored
This page is out of date. Refresh to see the latest.
Showing with 146 additions and 119 deletions.
  1. +21 −3 Readme.md
  2. +125 −116 lib/connect-memcached.js
View
24 Readme.md
@@ -1,4 +1,3 @@
-
# connect-memcached
Memcached session store, using [node-memcached](http://github.com/3rd-Eden/node-memcached) for communication with cache server.
@@ -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(),
@@ -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
View
241 lib/connect-memcached.js
@@ -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;
+};
Something went wrong with that request. Please try again.