Permalink
Browse files

different arguments for methods

  • Loading branch information...
vadimdemedes committed May 3, 2012
1 parent 325b63e commit 889c3b36b9b63ae5f420491474e2bbd559144e41
Showing with 159 additions and 141 deletions.
  1. +11 −6 Readme.md
  2. +50 −44 lib/memcacher.coffee
  3. +68 −58 lib/memcacher.js
  4. +3 −2 package.json
  5. +27 −0 test/memcacher.test.coffee
  6. +0 −31 tests/memcacher.test.coffee
View
@@ -1,6 +1,6 @@
# Memcacher
- Adding tags functionality to memcached. Using node-memcached module by 3rd-Eden.
+ Adding tags functionality to memcached without modifying its source. Using node-memcached module by 3rd-Eden.
# Installation
@@ -19,9 +19,9 @@ 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'], ->
+Client.set key: 'first-post', value: 'first title', expireIn: 2592000, tags: ['first-post-tag'], ->
+ Client.set key: 'second-post', value: 'second title', expireIn: 2592000, tags: ['second-post-tag'], ->
+ Client.set key: 'posts', value: JSON.stringify(['first title', 'second title']), expireIn: 2592000, tags: ['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', ->
@@ -30,12 +30,17 @@ Client.set 'first-post', 'first title', 2592000, ['first-post-tag'], ->
# value is not ['first title', 'second title']
# it is false
# and you should calculate your list of posts again, excluding deleted ones
+
+ # or, you can remove by tag
+
+ Client.delByTag 'second-post-tag', ->
+ # record with key "second-post" deleted
```
# Chainable methods
```coffee-script
-Client.set('test-key', 'value', 2592000, ['some-tag']).get 'test-key', (err, value) ->
+Client.set(key: 'test-key', value: 'value', expireIn: 2592000, tags: ['some-tag']).get 'test-key', (err, value) ->
value # 'value'
```
@@ -44,7 +49,7 @@ Client.set('test-key', 'value', 2592000, ['some-tag']).get 'test-key', (err, val
Tests made using **mocha**. Run them by doing this:
```
-cd tests && mocha *
+mocha
```
# License
View
@@ -5,84 +5,90 @@ 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
- return @
+ set: (options, callback) ->
+ if options.tags
+ for tag in options.tags
+ @bindTagToKey tag: tag, key: options.key
+
+ @client.set options.key, options.value, options.expireIn or options.expire_in, ->
+ callback no if callback
+
+ @
- get: (key, callback) ->
+ get: (options, callback) ->
+ key = if typeof options is 'object' then options.key else options
+
@client.get key, (err, value) ->
- process.nextTick ->
- callback err, value if callback
- return @
+ callback err, value if callback
+
+ @
- delByTag: (tag, callback) ->
+ delByTag: (options, callback) ->
+ tag = if typeof options is 'object' then options.tag else options
+
that = @
- that.client.get "#{ tag }-keys", (err, value) ->
+
+ @client.get "#{ tag }-keys", (err, value) ->
return callback no if not value
that.client.del "#{ tag }-keys", ->
- keys = JSON.parse value
- async.forEach keys, (key, nextKey) ->
+ async.forEach JSON.parse(value), (key, nextKey) ->
that.client.del key, ->
do nextKey
, ->
- process.nextTick ->
- callback no if callback
- return @
+ callback no if callback
+
+ @
- del: (key, callback) ->
+ del: (options, callback) ->
+ key = if typeof options is 'object' then options.key else options
+
that = @
+
@client.get "#{ key }-tags", (err, value) ->
- return if not value
+ return callback no if not value
- tags = JSON.parse value
- async.forEach tags, (tag, nextTag) ->
+ async.forEach JSON.parse(value), (tag, nextTag) ->
that.delByTag tag, nextTag
, ->
that.client.del "#{ key }-tags", ->
that.client.del key, ->
- process.nextTick ->
- callback no if callback
- return @
+ callback no if callback
+ @
- bindTagToKey: (tag, key, callback) ->
+ bindTagToKey: (options, callback) ->
that = @
+
bindKeys = (done) ->
- that.client.get "#{ tag }-keys", (err, value) ->
+ that.client.get "#{ options.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
+ that.client.set "#{ options.tag }-keys", JSON.stringify([options.key]), 2592000, ->
+ do done
else
keys = JSON.parse value
- keys.push key
- that.client.set "#{ tag }-keys", JSON.stringify(keys), 2592000, ->
- process.nextTick ->
- do done
+ keys.push options.key
+ that.client.set "#{ options.tag }-keys", JSON.stringify(keys), 2592000, ->
+ do done
bindTags = (done) ->
- that.client.get "#{ key }-tags", (err, value) ->
+ that.client.get "#{ options.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
+ that.client.set "#{ options.key }-tags", JSON.stringify([options.tag]), 2592000, ->
+ do done
else
tags = JSON.parse value
- tags.push tag
- that.client.set "#{ key }-tags", JSON.stringify(tags), 2592000, ->
- process.nextTick ->
- do done
+ tags.push options.tag
+ that.client.set "#{ options.key }-tags", JSON.stringify(tags), 2592000, ->
+ do done
async.parallel [bindKeys, bindTags], ->
- process.nextTick ->
- callback no if callback
+ callback no if callback
close: ->
do @client.end
+
+ end: ->
+ do @close
module.exports = Memcacher
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 889c3b3

Please sign in to comment.