Skip to content
Browse files

first version, not tested

  • Loading branch information...
0 parents commit 4444ad548a1be88661956a24827144a77efee331 @pkrumins committed Sep 14, 2010
Showing with 91 additions and 0 deletions.
  1. BIN lib/.cart.js.swp
  2. +60 −0 lib/cart.js
  3. +31 −0 package.json
BIN lib/.cart.js.swp
Binary file not shown.
60 lib/cart.js
@@ -0,0 +1,60 @@
+// inspired by nsession-store
+var sys = require('sys');
+var ConnectStore = require('connect/middleware/session/store'),
+var Store = require('supermarket');
+
+module.exports = function Cart(options) {
+ var self = this;
+
+ options = options || {};
+ options.maxAge = options.maxAge || 3600000; // Expunge after an hour
+ var dbFile = options.dbFile || __dirname + "/sessions.db";
+ ConnectStore.call(this, options);
+ Store(dbFile, function (err, db) {
+ if (err) throw err;
+ self.db = db;
+ db.filter(
+ function (key, item) {
+ item = JSON.parse(item);
+ return item.lastAccess > Date.now() - options.maxAge;
+ },
+ function (key, item) {
+ db.remove(key);
+ }
+ );
+ });
+};
+
+sys.inherits(Cart, ConnectStore);
+
+Cart.prototype.get = function (hash, fn) {
+ this.db.get(hash, fn);
+};
+
+Cart.prototype.set = function (hash, sess, fn) {
+ this.db.set(hash, JSON.stringify(sess), fn);
+};
+
+Cart.prototype.destroy = function (hash, fn) {
+ this.db.remove(hash, fn);
+};
+
+Cart.prototype.length = function (fn) {
+ process.nextTick(function () {
+ this.db.length(function (err, len) {
+ fn(len);
+ });
+ });
+};
+
+Cart.prototype.clear = function (fn) {
+ var self = this;
+ self.db.filter(
+ function () { return true },
+ function (err, key, value) {
+ self.remove(key);
+ },
+ function () { fn() }
+ );
+};
+
31 package.json
@@ -0,0 +1,31 @@
+{
+ "name" : "cart",
+ "version" : "1.0.0",
+ "description" : "Connect session store using supermarket",
+ "modules" : {
+ "index" : "./lib/cart.js"
+ },
+ "keywords" : {
+ "connect",
+ "expresso",
+ "http sessions"
+ },
+ "maintainers": [
+ {
+ "name": "Peteris Krumins",
+ "email": "peteris.krumins@gmail.com",
+ "web": "http://www.catonmat.net",
+ "twitter": "pkrumins"
+ }
+ ],
+ "repository" : {
+ "type" : "git",
+ "url" : "http://github.com/pkrumins/supermarket-cart.git"
+ },
+ "engines": {
+ "node": ">=0.2.0"
+ },
+ "dependencies" : {
+ "supermarket" : ">=1.0.3"
+ }
+}

0 comments on commit 4444ad5

Please sign in to comment.
Something went wrong with that request. Please try again.