Permalink
Browse files

- added test to package.json

- added ttl-method to redefine the ttl of a key
  • Loading branch information...
1 parent aa3d91c commit 176541d804b5cc41194a1372d5f94eb2e3a178b8 @tcs-de tcs-de committed May 14, 2012
Showing with 283 additions and 6 deletions.
  1. +48 −0 lib/node_cache.coffee
  2. +31 −1 lib/node_cache.js
  3. +7 −1 package.json
  4. +111 −1 test/node_cache-test.coffee
  5. +86 −3 test/node_cache-test.js
View
@@ -159,6 +159,54 @@ module.exports = class NodeCache
cb( null, delCount )
return
+ # ## ttl
+ #
+ # reset or redefine the ttl of a key. If `ttl` is not passed or set to 0 it's similar to `.del()`
+ #
+ # **Parameters:**
+ #
+ # * `key` ( String ): cache key to reset the ttl value
+ # * `ttl` ( Number ): ( optional -> options.stdTTL || 0 ) The time to live in seconds
+ # * `cb` ( Function ): Callback function
+ #
+ # **Return**
+ #
+ # ( Boolen ): key found and ttl set
+ #
+ # **Example:**
+ #
+ # myCache.ttl( "myKey" ) // will set ttl to default ttl
+ #
+ # myCache.ttl( "myKey", 1000, ( err, keyFound )->
+ # console.log( err, success )
+ #
+ ttl: =>
+ # change args if only key and callback are passed
+ [ key, args... ] = arguments
+ for arg in args
+ switch typeof arg
+ when "number" then ttl = arg
+ when "function" then cb = arg
+
+ cb or= ->
+ ttl or= @options.stdTTL
+ if not key
+ cb( null, false )
+
+ # check for existend data and update the ttl value
+ if @data[ key ]? and @_check( key, @data[ key ] )
+ # on ttl = 0 delete the key. otherwise reset the value
+ if ttl > 0
+ @data[ key ] = @_wrap( @data[ key ].v, ttl )
+ else
+ @del( key )
+ cb( null, true )
+ else
+ # return false if key has not been found
+ cb( null, false )
+
+ return
+
# ## getStats
#
# get the stats
View

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

Oops, something went wrong.
View
@@ -13,7 +13,13 @@
"engines": {
"node": ">= 0.4.6"
},
+ "scripts":{
+ "test": "expresso test/node_cache-test.js"
+ },
"dependencies": {
- "underscore": ">= 1.1.7"
+ "underscore": ">= 1.3.3"
+ },
+ "devDependencies": {
+ "expresso": "0.9.2"
}
}
View
@@ -3,8 +3,10 @@ _ = require( "underscore" )
VCache = require "../lib/node_cache"
localCache = new VCache( stdTTL: 0 )
+localCacheTTL = new VCache( stdTTL: 0.3 )
# just for testing disable the check period
localCache._killCheckPeriod()
+localCacheTTL._killCheckPeriod()
# test helper
randomString = ( length, withnumbers = true ) ->
@@ -299,9 +301,13 @@ module.exports =
"ttl": (beforeExit, assert) ->
console.log "START TTL TEST"
+
val = randomString( 20 )
key = randomString( 7 )
key2 = randomString( 7 )
+ key3 = randomString( 7 )
+ key4 = randomString( 7 )
+ key5 = randomString( 7 )
n = 0
# set a key with ttl
@@ -327,7 +333,7 @@ module.exports =
pred = {}
pred[ key2 ] = val
assert.eql( pred, res )
-
+
# check key before lifetime end
setTimeout( ->
++n;
@@ -385,5 +391,109 @@ module.exports =
, 700 )
, 1000 )
+
+ # set a key with ttl
+ localCache.set key3, val, 100, ( err, res )->
+ assert.isNull( err, err )
+ assert.ok( res )
+
+ # check the key3 immediately
+ localCache.get key3, ( err, res )->
+ assert.isNull( err, err )
+ pred = {}
+ pred[ key3 ] = val
+ assert.eql( pred, res )
+
+ # check ttl with false key
+ localCache.ttl ( key3 + "false" ), 0.3, ( err, setted )->
+ assert.isNull( err, err )
+ assert.equal(false, setted)
+
+ # check ttl with false key
+ localCache.ttl key3, 0.3, ( err, setted )->
+ assert.isNull( err, err )
+ assert.ok(setted)
+
+ # check existens
+ localCache.get key3, ( err, res )->
+ pred = {}
+ pred[ key3 ] = val
+ assert.eql( pred, res )
+
+ # run general checkdata after ttl
+ setTimeout( ->
+ localCache._checkData( false )
+
+ # deep dirty check if key is deleted
+ assert.isUndefined( localCache.data[ key3 ] )
+
+ , 500 )
+
+
+ # set a key with default ttl = 0
+ localCache.set key4, val, 100, ( err, res )->
+ assert.isNull( err, err )
+ assert.ok( res )
+
+ # check the key4 immediately
+ localCache.get key4, ( err, res )->
+ assert.isNull( err, err )
+ pred = {}
+ pred[ key4 ] = val
+ assert.eql( pred, res )
+
+ # check ttl with false key
+ localCache.ttl ( key4 + "false" ), ( err, setted )->
+ assert.isNull( err, err )
+ assert.equal(false, setted)
+
+ # check ttl with false key
+ localCache.ttl key4, ( err, setted )->
+ assert.isNull( err, err )
+ assert.ok(setted)
+
+ # deep dirty check if key is deleted
+ assert.isUndefined( localCache.data[ key4 ] )
+
+ # set a key with default ttl
+ localCacheTTL.set key5, val, 100, ( err, res )->
+ assert.isNull( err, err )
+ assert.ok( res )
+
+ # check the key5 immediately
+ localCacheTTL.get key5, ( err, res )->
+ assert.isNull( err, err )
+ pred = {}
+ pred[ key5 ] = val
+ assert.eql( pred, res )
+
+ # check ttl with false key
+ localCacheTTL.ttl ( key5 + "false" ), ( err, setted )->
+ assert.isNull( err, err )
+ assert.equal(false, setted)
+
+ # check ttl with false key
+ localCacheTTL.ttl key5, ( err, setted )->
+ assert.isNull( err, err )
+ assert.ok(setted)
+
+ # check existens
+ localCacheTTL.get key5, ( err, res )->
+ pred = {}
+ pred[ key5 ] = val
+ assert.eql( pred, res )
+
+ # run general checkdata after ttl
+ setTimeout( ->
+ localCacheTTL._checkData( false )
+
+ # deep dirty check if key is deleted
+ assert.isUndefined( localCacheTTL.data[ key5 ] )
+
+ , 500 )
+
+
+
+
View

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

Oops, something went wrong.

0 comments on commit 176541d

Please sign in to comment.