From 176541d804b5cc41194a1372d5f94eb2e3a178b8 Mon Sep 17 00:00:00 2001 From: tcs-de Date: Tue, 15 May 2012 00:25:31 +0200 Subject: [PATCH] - added test to package.json - added ttl-method to redefine the ttl of a key --- lib/node_cache.coffee | 48 ++++++++++++++++ lib/node_cache.js | 32 ++++++++++- package.json | 8 ++- test/node_cache-test.coffee | 112 +++++++++++++++++++++++++++++++++++- test/node_cache-test.js | 89 +++++++++++++++++++++++++++- 5 files changed, 283 insertions(+), 6 deletions(-) diff --git a/lib/node_cache.coffee b/lib/node_cache.coffee index 7065c5d..c1592b2 100644 --- a/lib/node_cache.coffee +++ b/lib/node_cache.coffee @@ -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 diff --git a/lib/node_cache.js b/lib/node_cache.js index 3443754..0d61ef9 100644 --- a/lib/node_cache.js +++ b/lib/node_cache.js @@ -1,6 +1,6 @@ (function() { var NodeCache, _; - var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; + var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __slice = Array.prototype.slice; _ = require("underscore"); module.exports = NodeCache = (function() { function NodeCache(options) { @@ -14,6 +14,7 @@ this._checkData = __bind(this._checkData, this); this.flushAll = __bind(this.flushAll, this); this.getStats = __bind(this.getStats, this); + this.ttl = __bind(this.ttl, this); this.del = __bind(this.del, this); this.set = __bind(this.set, this); this.get = __bind(this.get, this); @@ -99,6 +100,35 @@ } cb(null, delCount); }; + NodeCache.prototype.ttl = function() { + var arg, args, cb, key, ttl, _i, _len; + key = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; + for (_i = 0, _len = args.length; _i < _len; _i++) { + arg = args[_i]; + switch (typeof arg) { + case "number": + ttl = arg; + break; + case "function": + cb = arg; + } + } + cb || (cb = function() {}); + ttl || (ttl = this.options.stdTTL); + if (!key) { + cb(null, false); + } + if ((this.data[key] != null) && this._check(key, this.data[key])) { + if (ttl > 0) { + this.data[key] = this._wrap(this.data[key].v, ttl); + } else { + this.del(key); + } + cb(null, true); + } else { + cb(null, false); + } + }; NodeCache.prototype.getStats = function() { return this.stats; }; diff --git a/package.json b/package.json index f1e7b7e..29540a7 100644 --- a/package.json +++ b/package.json @@ -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" } } \ No newline at end of file diff --git a/test/node_cache-test.coffee b/test/node_cache-test.coffee index ac0da05..e5058cf 100644 --- a/test/node_cache-test.coffee +++ b/test/node_cache-test.coffee @@ -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 ) + + + + diff --git a/test/node_cache-test.js b/test/node_cache-test.js index f7e977d..06ec9b0 100644 --- a/test/node_cache-test.js +++ b/test/node_cache-test.js @@ -1,11 +1,15 @@ (function() { - var VCache, ks, localCache, randomString, vs, _; + var VCache, ks, localCache, localCacheTTL, randomString, vs, _; _ = require("underscore"); VCache = require("../lib/node_cache"); localCache = new VCache({ stdTTL: 0 }); + localCacheTTL = new VCache({ + stdTTL: 0.3 + }); localCache._killCheckPeriod(); + localCacheTTL._killCheckPeriod(); randomString = function(length, withnumbers) { var chars, i, randomstring, rnum, string_length; if (withnumbers == null) { @@ -272,11 +276,14 @@ }); }, "ttl": function(beforeExit, assert) { - var key, key2, n, val; + var key, key2, key3, key4, key5, n, val; 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; localCache.set(key, val, 0.5, function(err, res) { assert.isNull(err, err); @@ -328,7 +335,7 @@ return assert.eql(pred, res); }); }, 600); - return setTimeout(function() { + setTimeout(function() { var startKeys; startKeys = localCache.getStats().keys; key = "autotest"; @@ -348,6 +355,82 @@ }); }); }, 1000); + localCache.set(key3, val, 100, function(err, res) { + assert.isNull(err, err); + assert.ok(res); + return localCache.get(key3, function(err, res) { + var pred; + assert.isNull(err, err); + pred = {}; + pred[key3] = val; + assert.eql(pred, res); + localCache.ttl(key3 + "false", 0.3, function(err, setted) { + assert.isNull(err, err); + return assert.equal(false, setted); + }); + localCache.ttl(key3, 0.3, function(err, setted) { + assert.isNull(err, err); + return assert.ok(setted); + }); + localCache.get(key3, function(err, res) { + pred = {}; + pred[key3] = val; + return assert.eql(pred, res); + }); + return setTimeout(function() { + localCache._checkData(false); + return assert.isUndefined(localCache.data[key3]); + }, 500); + }); + }); + localCache.set(key4, val, 100, function(err, res) { + assert.isNull(err, err); + assert.ok(res); + return localCache.get(key4, function(err, res) { + var pred; + assert.isNull(err, err); + pred = {}; + pred[key4] = val; + assert.eql(pred, res); + localCache.ttl(key4 + "false", function(err, setted) { + assert.isNull(err, err); + return assert.equal(false, setted); + }); + return localCache.ttl(key4, function(err, setted) { + assert.isNull(err, err); + assert.ok(setted); + return assert.isUndefined(localCache.data[key4]); + }); + }); + }); + return localCacheTTL.set(key5, val, 100, function(err, res) { + assert.isNull(err, err); + assert.ok(res); + return localCacheTTL.get(key5, function(err, res) { + var pred; + assert.isNull(err, err); + pred = {}; + pred[key5] = val; + assert.eql(pred, res); + localCacheTTL.ttl(key5 + "false", function(err, setted) { + assert.isNull(err, err); + return assert.equal(false, setted); + }); + localCacheTTL.ttl(key5, function(err, setted) { + assert.isNull(err, err); + return assert.ok(setted); + }); + localCacheTTL.get(key5, function(err, res) { + pred = {}; + pred[key5] = val; + return assert.eql(pred, res); + }); + return setTimeout(function() { + localCacheTTL._checkData(false); + return assert.isUndefined(localCacheTTL.data[key5]); + }, 500); + }); + }); } }; }).call(this);