Permalink
Browse files

init

  • Loading branch information...
0 parents commit b85e498af4ef29ee7e1924ba0c9425c15b1e0431 @vadimdemedes committed Feb 18, 2012
Showing with 319 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +4 −0 .npmignore
  3. +66 −0 Readme.md
  4. +2 −0 index.js
  5. +79 −0 lib/memcacher.coffee
  6. +126 −0 lib/memcacher.js
  7. +16 −0 package.json
  8. +25 −0 tests/memcacher.test.coffee
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1,4 @@
+support
+test
+examples
+*.sock
@@ -0,0 +1,66 @@
+# Memcacher
+
+ Adding tags functionality to memcached. Using node-memcached module by 3rd-Eden.
+
+# Installation
+
+```
+npm install memcacher
+```
+
+# Usage
+
+Use it like you've used **memcached** module by 3rd-Eden. Memcacher just adds one more option to method **set** - tags. Take a look at such situation: Let's say we
+have a list of posts, and when we delete one post, we don't want it to appear on our list of all posts, right? That's where tags come handy. Check out this example:
+
+
+```
+Memcacher = require 'memcacher'
+
+Client = new Memcacher ['127.0.0.1:11211']
+
+Client.set 'first-post', 'first title', 2592000, ['first-post-tag'], ->
+ Client.set 'second-post', 'second title', 2592000, ['second-post-tag'], ->
+ Client.set 'posts', JSON.stringify(['first title', 'second title']), 2592000, ['first-post-tag', 'second-post-tag'], ->
+ # all records are saved
+ # now, if you will remove record with key "first-post", all records with tag "first-post-tag" will be removed
+ Client.del 'first-post', ->
+ # if you will try to get record with key "posts", it will be false
+ Client.get 'posts', (err, value) ->
+ # value is not ['first title', 'second title']
+ # it is false
+ # and you should calculate your list of posts again, excluding deleted ones
+```
+
+# Tests
+
+Tests made using **mocha**. Run them by doing this:
+
+```
+cd tests && mocha *
+```
+
+# License
+
+(The MIT License)
+
+Copyright (c) 2011 Vadim Demedes <sbioko@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,2 @@
+
+module.exports = require('./lib/memcacher');
@@ -0,0 +1,79 @@
+memcached = require 'memcached'
+async = require 'async'
+
+class Memcacher
+ constructor: (servers = []) ->
+ @client = new memcached servers
+
+ set: (key, value, expireIn, tags = [], callback) ->
+ for tag in tags
+ @bindTagToKey tag, key
+ @client.set key, value, expireIn, ->
+ process.nextTick ->
+ callback no if callback
+
+ get: (key, callback) ->
+ @client.get key, (err, value) ->
+ process.nextTick ->
+ callback err, value if callback
+
+ del: (key, callback) ->
+ @remove key, callback
+
+ remove: (key, callback) ->
+ that = @
+ @client.get "#{ key }-tags", (err, value) ->
+ return if not value
+
+ tags = JSON.parse value
+ async.forEach tags, (tag, nextTag) ->
+ that.client.get "#{ tag }-keys", (err, value) ->
+ return do nextTag if not value
+
+ keys = JSON.parse value
+ for key in keys
+ that.client.del key, ->
+
+ do nextTag
+ , ->
+ that.client.del key, ->
+ process.nextTick ->
+ callback no if callback
+
+
+ bindTagToKey: (tag, key, callback) ->
+ that = @
+ bindKeys = (done) ->
+ that.client.get "#{ tag }-keys", (err, value) ->
+ if not value # keys, related to that tag, do not exist
+ that.client.set "#{ tag }-keys", JSON.stringify([key]), 2592000, ->
+ process.nextTick ->
+ do done
+ else
+ keys = JSON.parse value
+ keys.push key
+ that.client.set "#{ tag }-keys", JSON.stringify(keys), 2592000, ->
+ process.nextTick ->
+ do done
+
+ bindTags = (done) ->
+ that.client.get "#{ key }-tags", (err, value) ->
+ if not value # tags, related to that key, do not exist
+ that.client.set "#{ key }-tags", JSON.stringify([tag]), 2592000, ->
+ process.nextTick ->
+ do done
+ else
+ tags = JSON.parse value
+ tags.push tag
+ that.client.set "#{ key }-tags", JSON.stringify(tags), 2592000, ->
+ process.nextTick ->
+ do done
+
+ async.parallel [bindKeys, bindTags], ->
+ process.nextTick ->
+ callback no if callback
+
+ close: ->
+ do @client.end
+
+module.exports = Memcacher
@@ -0,0 +1,126 @@
+var Memcacher, async, memcached;
+
+memcached = require('memcached');
+
+async = require('async');
+
+Memcacher = (function() {
+
+ function Memcacher(servers) {
+ if (servers == null) servers = [];
+ this.client = new memcached(servers);
+ }
+
+ Memcacher.prototype.set = function(key, value, expireIn, tags, callback) {
+ var tag, _i, _len;
+ if (tags == null) tags = [];
+ for (_i = 0, _len = tags.length; _i < _len; _i++) {
+ tag = tags[_i];
+ this.bindTagToKey(tag, key);
+ }
+ return this.client.set(key, value, expireIn, function() {
+ return process.nextTick(function() {
+ if (callback) return callback(false);
+ });
+ });
+ };
+
+ Memcacher.prototype.get = function(key, callback) {
+ return this.client.get(key, function(err, value) {
+ return process.nextTick(function() {
+ if (callback) return callback(err, value);
+ });
+ });
+ };
+
+ Memcacher.prototype.del = function(key, callback) {
+ return this.remove(key, callback);
+ };
+
+ Memcacher.prototype.remove = function(key, callback) {
+ var that;
+ that = this;
+ return this.client.get("" + key + "-tags", function(err, value) {
+ var tags;
+ if (!value) return;
+ tags = JSON.parse(value);
+ return async.forEach(tags, function(tag, nextTag) {
+ return that.client.get("" + tag + "-keys", function(err, value) {
+ var key, keys, _i, _len;
+ if (!value) return nextTag();
+ keys = JSON.parse(value);
+ for (_i = 0, _len = keys.length; _i < _len; _i++) {
+ key = keys[_i];
+ that.client.del(key, function() {});
+ }
+ return nextTag();
+ });
+ }, function() {
+ return that.client.del(key, function() {
+ return process.nextTick(function() {
+ if (callback) return callback(false);
+ });
+ });
+ });
+ });
+ };
+
+ Memcacher.prototype.bindTagToKey = function(tag, key, callback) {
+ var bindKeys, bindTags, that;
+ that = this;
+ bindKeys = function(done) {
+ return that.client.get("" + tag + "-keys", function(err, value) {
+ var keys;
+ if (!value) {
+ return that.client.set("" + tag + "-keys", JSON.stringify([key]), 2592000, function() {
+ return process.nextTick(function() {
+ return done();
+ });
+ });
+ } else {
+ keys = JSON.parse(value);
+ keys.push(key);
+ return that.client.set("" + tag + "-keys", JSON.stringify(keys), 2592000, function() {
+ return process.nextTick(function() {
+ return done();
+ });
+ });
+ }
+ });
+ };
+ bindTags = function(done) {
+ return that.client.get("" + key + "-tags", function(err, value) {
+ var tags;
+ if (!value) {
+ return that.client.set("" + key + "-tags", JSON.stringify([tag]), 2592000, function() {
+ return process.nextTick(function() {
+ return done();
+ });
+ });
+ } else {
+ tags = JSON.parse(value);
+ tags.push(tag);
+ return that.client.set("" + key + "-tags", JSON.stringify(tags), 2592000, function() {
+ return process.nextTick(function() {
+ return done();
+ });
+ });
+ }
+ });
+ };
+ return async.parallel([bindKeys, bindTags], function() {
+ return process.nextTick(function() {
+ if (callback) return callback(false);
+ });
+ });
+ };
+
+ Memcacher.prototype.close = function() {
+ return this.client.end();
+ };
+
+ return Memcacher;
+
+})();
+
+module.exports = Memcacher;
@@ -0,0 +1,16 @@
+{
+ "name": "memcacher",
+ "version": "0.1.0",
+ "description": "Adding tags functionality to memcached.",
+ "keywords": ["memcache", "memcached"],
+ "author": "Vadim Demedes <sbioko@gmail.com>",
+ "dependencies": {
+ "memcached": "*",
+ "async": "*"
+ },
+ "devDependencies": {
+ "should": "*"
+ },
+ "main": "index",
+ "engines": { "node": ">= 0.4" }
+}
@@ -0,0 +1,25 @@
+Memcacher = require '../lib/memcacher'
+Client = new Memcacher ['127.0.0.1:11211']
+require 'should'
+
+describe 'Memcacher', ->
+ describe 'setting record', ->
+ it 'should save', (done) ->
+ Client.set 'test-key-first', 'first', 2592000, ['test-first'], ->
+ Client.set 'test-key-second', 'second', 2592000, ['test-second'], ->
+ Client.set 'test-keys', JSON.stringify(['first', 'second']), 2592000, ['test-first', 'test-second'], ->
+ do done
+
+ describe 'getting record', ->
+ it 'should return two keys', (done) ->
+ Client.get 'test-keys', (err, value) ->
+ keys = JSON.parse value
+ keys[0].should.equal('first') and keys[1].should.equal('second')
+ do done
+
+ describe 'removing one of the records', ->
+ it 'should remove record and remove "test-keys" record', (done) ->
+ Client.remove 'test-key-first', ->
+ Client.get 'test-keys', (err, value) ->
+ value.should.equal false
+ do done

0 comments on commit b85e498

Please sign in to comment.