Skip to content
Browse files

fixing expire time stuff

  • Loading branch information...
1 parent 8f8d3fa commit f4e7cba14c210ba60475a9dd0827349f61a0eba9 @evantahler committed Jun 13, 2012
Showing with 57 additions and 15 deletions.
  1. +17 −12 initializers/initCache.js
  2. +2 −2 readme.markdown
  3. +28 −0 spec/cache.js
  4. +1 −1 tasks/cleanOldCacheObjects.js
  5. +9 −0 versions.markdown
View
29 initializers/initCache.js
@@ -9,7 +9,6 @@ var initCache = function(api, next){
api.cache.data = {};
}
var redisCacheKey = "actionHero::cache";
- var defaultExpireTime = 31622400 // 1 year
api.cache.size = function(api, next){
if(api.redis.enable === true){
@@ -21,16 +20,22 @@ var initCache = function(api, next){
}
}
- api.cache.save = function(api, key, value, expireTimeSeconds, next){
- if(expireTimeSeconds < 0 || expireTimeSeconds == null){ expireTimeSeconds = defaultExpireTime; }
- var expireTimestamp = new Date().getTime();
- expireTimestamp = expireTimestamp + (expireTimeSeconds * 1000);
+ api.cache.save = function(api, key, value, expireTimeMS, next){
+ if(typeof expireTimeMS == "function" && typeof next == "undefined"){
+ next = expireTimeMS;
+ expireTimeMS = null;
+ }
+ if(expireTimeMS != null){
+ var expireTimestamp = new Date().getTime() + expireTimeMS;
+ }else{
+ expireTimestamp = null;
+ }
var cacheObj = {
- value: value,
- expireTimestamp: expireTimestamp,
- createdAt: new Date().getTime(),
- readAt: null
- }
+ value: value,
+ expireTimestamp: expireTimestamp,
+ createdAt: new Date().getTime(),
+ readAt: null
+ }
if(api.redis.enable === true){
api.redis.client.hset(redisCacheKey, key, JSON.stringify(cacheObj), function(){
if(typeof next == "function"){ process.nextTick(function() { next(true); }); }
@@ -50,7 +55,7 @@ var initCache = function(api, next){
if(api.redis.enable === true){
api.redis.client.hget(redisCacheKey, key, function (err, cacheObj){
cacheObj = JSON.parse(cacheObj);
- if(cacheObj != null && cacheObj.expireTimestamp >= (new Date().getTime())){
+ if(cacheObj != null && ( cacheObj.expireTimestamp >= new Date().getTime() || cacheObj.expireTimestamp == null )) {
cacheObj.readAt = new Date().getTime();
api.redis.client.hset(redisCacheKey, key, JSON.stringify(cacheObj), function(){
if(typeof next == "function"){
@@ -68,7 +73,7 @@ var initCache = function(api, next){
if(cacheObj == null){
process.nextTick(function() { next(null, null, null, null); });
}else{
- if(cacheObj.expireTimestamp >= (new Date().getTime())){
+ if(cacheObj.expireTimestamp >= new Date().getTime() || cacheObj.expireTimestamp == null ){
api.cache.data[key].readAt = new Date().getTime();
if(typeof next == "function"){
process.nextTick(function() { next(cacheObj.value, cacheObj.expireTimestamp, cacheObj.createdAt, cacheObj.readAt); });
View
4 readme.markdown
@@ -184,12 +184,12 @@ Connections over socket can also use the file action.
## Cache
actionHero ships with the functions needed for an in-memory key-value cache. Check the cacheTest action to see how to use it. You can cache strings, numbers, arrays and objects (as long as they contain only strings, numbers, and arrays). Cache functions:
-* `api.cache.save(api, key, value, expireTimeSeconds, next)`
+* `api.cache.save(api, key, value, expireTimeMS, next)`
* `api.cache.load(api, key, next)`
* `api.cache.destroy(api, key, next)`
-api.cache.save is used to both create new entires or update existing cache entires. If you don't define an expireTimeSeconds, the default will be used from `api.configData.cache.defaultExpireTimeSeconds`. A task will periodically go though and delete expired cache entries. If you are running a stand-alone version of actionHero, this cache will be in memory of the actionHero process, otherwise this data will be stored in redis.
+api.cache.save is used to both create new entires or update existing cache entires. If you don't define an expireTimeMS, (null) it will not expire. A task will periodically go though and delete expired cache entries. If you are running a stand-alone version of actionHero, this cache will be in memory of the actionHero process, otherwise this data will be stored in redis.
Note: that the keys starting with an "_" should not be used, as they are in use by core parts of the system.
View
28 spec/cache.js
@@ -100,6 +100,34 @@ suite.addBatch({
},
});
+// objects saved with negative expire time will not load
+suite.addBatch({
+ "cache.save expire negative": {
+ topic: function(){ apiObj.cache.save(apiObj,"testKeyInThePast","abc123",-1,this.callback) },
+ saved: function(resp, f){ specHelper.assert.equal(f, true); }
+ },
+});
+suite.addBatch({
+ "cache.load expire time negative should not load": {
+ topic: function(){ apiObj.cache.load(apiObj,"testKeyInThePast",this.callback) },
+ save: function(resp, f){ specHelper.assert.equal(resp, null); }
+ },
+});
+
+// objects can have null expire time
+suite.addBatch({
+ "cache.save expire negative": {
+ topic: function(){ apiObj.cache.save(apiObj,"testKeyForNullExpireTime","abc123",this.callback) },
+ saved: function(resp, f){ specHelper.assert.equal(f, true); }
+ },
+});
+suite.addBatch({
+ "cache.load expire time negative should not load": {
+ topic: function(){ apiObj.cache.load(apiObj,"testKeyForNullExpireTime",this.callback) },
+ save: function(resp, f){ specHelper.assert.equal(resp, "abc123"); }
+ },
+});
+
// mess with object types to save
suite.addBatch({
"cache.save array": {
View
2 tasks/cleanOldCacheObjects.js
@@ -13,7 +13,7 @@ task.run = function(api, params, next){
var deleted = 0;
for (var i in api.cache.data){
var thisEntry = api.cache.data[i];
- if (thisEntry.expireTimestamp < (new Date().getTime())){
+ if ( thisEntry.expireTimestamp != null && thisEntry.expireTimestamp < new Date().getTime() ){
deleted++;
delete api.cache.data[i];
}
View
9 versions.markdown
@@ -2,6 +2,15 @@
## [actionHeroClient](https://github.com/evantahler/actionHeroNodeClient) released! Connect node to node!
+## Version 2.0.3
+
+**Summary:** Bug Fixes
+
+** Notes **
+
+- cache actions now take miliseconds like everything else.
+- you can now have non-expiring cache entires (just pass a null value for expireTimeMS)
+
## Version 2.0.2
**Summary:** Bug Fixes & Examples

0 comments on commit f4e7cba

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