Comparing version 0.0.5 to 0.1.0
@@ -16,18 +16,14 @@ /** | ||
opts.prefix = opts.prefix || 'basiccache_'; | ||
opts.expires = opts.expires || opts.expire || opts.ttl; | ||
var self = this; | ||
this.opts = opts; | ||
this.cache = {}; | ||
self.opts = opts; | ||
self.cache = {}; | ||
if (opts.purgeInterval) { | ||
self._purge_timer = setInterval(function() { | ||
self.purge(); | ||
}, opts.purgeInterval); | ||
this.startTimer(opts.purgeInterval) | ||
} | ||
if ('function' === typeof opts.debug) { | ||
self.debug = opts.debug; | ||
if ('function' == typeof opts.debug) { | ||
this.debug = opts.debug; | ||
} else { | ||
self.debug = function noop() { }; | ||
this.debug = function noop() { }; | ||
} | ||
@@ -45,14 +41,14 @@ } | ||
BasicCache.prototype.get = function(key) { | ||
var d = Date.now(); | ||
key = this.opts.prefix + key; | ||
var obj = this.cache[key]; | ||
if (this.cache[key] === undefined) { | ||
this.debug('failed to pull "%s" from cache', key); | ||
this.debug('miss cache for "%s"', key); | ||
return; | ||
} | ||
if (d > this.cache[key].expires_at) { | ||
this.debug('key "%s" expired at: %s', key, this.cache[key].expires_at); | ||
if (obj.expires_at && Date.now() > obj.expires_at) { | ||
this.debug('cache for "%s" expired at: %s', key, this.cache[key].expires_at); | ||
delete this.cache[key]; | ||
return; | ||
} | ||
this.debug('key "%s" pulled from cache', key); | ||
this.debug('hit cache for "%s"', key); | ||
return this.cache[key].value; | ||
@@ -62,11 +58,11 @@ }; | ||
/** | ||
* set an item in the cache | ||
* | ||
* expires defaults to 5 minutes | ||
* Set an item in the cache | ||
*/ | ||
BasicCache.prototype.set = function(key, value, expires) { | ||
key = this.opts.prefix + key; | ||
expires = expires || this.opts.expires; | ||
this.cache[key] = { | ||
value: value, | ||
expires_at: Date.now() + (expires || 5*60*1000) | ||
// use `undefined`, so comparison to any Date will return `false` | ||
expires_at: expires ? Date.now() + expires : undefined | ||
}; | ||
@@ -77,3 +73,3 @@ return this.cache[key].expires_at; | ||
/** | ||
* remove an item from the cache | ||
* Remove an item from the cache | ||
*/ | ||
@@ -112,6 +108,20 @@ BasicCache.prototype.remove = function(key) { | ||
/** | ||
* Start a purge timer | ||
*/ | ||
BasicCache.prototype.startTimer = function(interval) { | ||
var self = this; | ||
this.cancelTimer(); | ||
this._purge_timer = setInterval(function() { | ||
self.purge(); | ||
}, interval); | ||
}; | ||
/** | ||
* Clear purge interval | ||
*/ | ||
BasicCache.prototype.sleep = function() { | ||
clearInterval(this._purge_timer); | ||
BasicCache.prototype.cancelTimer = function() { | ||
if (this.hasOwnProperty('_purge_timer')) { | ||
clearInterval(this._purge_timer); | ||
delete this._purge_timer; | ||
} | ||
}; |
@@ -1,1 +0,1 @@ | ||
function BasicCache(e){if(!(this instanceof BasicCache))return new BasicCache(e);e=e||{},e.prefix=e.prefix||"basiccache_";var t=this;t.opts=e,t.cache={},e.purgeInterval&&(t._purge_timer=setInterval(function(){t.purge()},e.purgeInterval)),t.debug="function"==typeof e.debug?e.debug:function(){}}"undefined"!=typeof exports&&(module.exports=BasicCache),BasicCache.prototype.get=function(e){var t=Date.now();return e=this.opts.prefix+e,void 0===this.cache[e]?(this.debug('failed to pull "%s" from cache',e),void 0):t>this.cache[e].expires_at?(this.debug('key "%s" expired at: %s',e,this.cache[e].expires_at),delete this.cache[e],void 0):(this.debug('key "%s" pulled from cache',e),this.cache[e].value)},BasicCache.prototype.set=function(e,t,c){return e=this.opts.prefix+e,this.cache[e]={value:t,expires_at:Date.now()+(c||3e5)},this.cache[e].expires_at},BasicCache.prototype.remove=function(e){e=this.opts.prefix+e,delete this.cache[e]},BasicCache.prototype.clear=function(){for(var e in this.cache)this.cache.hasOwnProperty(e)&&delete this.cache[e]},BasicCache.prototype.purge=function(){var e=Date.now(),t=0;for(var c in this.cache)this.cache[c]&&e>this.cache[c].expires_at&&(delete this.cache[c],t+=1);this.debug("Purged %s expired item(s).",t)},BasicCache.prototype.sleep=function(){clearInterval(this._purge_timer)}; | ||
function BasicCache(e){return this instanceof BasicCache?(e=e||{},e.prefix=e.prefix||"basiccache_",e.expires=e.expires||e.expire||e.ttl,this.opts=e,this.cache={},e.purgeInterval&&this.startTimer(e.purgeInterval),this.debug="function"==typeof e.debug?e.debug:function(){},void 0):new BasicCache(e)}"undefined"!=typeof exports&&(module.exports=BasicCache),BasicCache.prototype.get=function(e){e=this.opts.prefix+e;var t=this.cache[e];return void 0===this.cache[e]?(this.debug('miss cache for "%s"',e),void 0):t.expires_at&&Date.now()>t.expires_at?(this.debug('cache for "%s" expired at: %s',e,this.cache[e].expires_at),delete this.cache[e],void 0):(this.debug('hit cache for "%s"',e),this.cache[e].value)},BasicCache.prototype.set=function(e,t,i){return e=this.opts.prefix+e,i=i||this.opts.expires,this.cache[e]={value:t,expires_at:i?Date.now()+i:void 0},this.cache[e].expires_at},BasicCache.prototype.remove=function(e){e=this.opts.prefix+e,delete this.cache[e]},BasicCache.prototype.clear=function(){for(var e in this.cache)this.cache.hasOwnProperty(e)&&delete this.cache[e]},BasicCache.prototype.purge=function(){var e=Date.now(),t=0;for(var i in this.cache)this.cache[i]&&e>this.cache[i].expires_at&&(delete this.cache[i],t+=1);this.debug("Purged %s expired item(s).",t)},BasicCache.prototype.startTimer=function(e){var t=this;this.cancelTimer(),this._purge_timer=setInterval(function(){t.purge()},e)},BasicCache.prototype.cancelTimer=function(){this.hasOwnProperty("_purge_timer")&&(clearInterval(this._purge_timer),delete this._purge_timer)}; |
{ | ||
"name": "basiccache", | ||
"description": "An extremely basic cache with a simple expiry system", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"author": "Dave Eddy <dave@daveeddy.com> (http://www.daveeddy.com)", | ||
@@ -6,0 +6,0 @@ "main": "./basiccache.js", |
@@ -34,2 +34,5 @@ basiccache.js | ||
cache.set('foo', 'bar'); | ||
// 'foo' will never expire | ||
cache.set('key', 'value', 5 * 1000); | ||
@@ -47,4 +50,4 @@ // key will expire in 5 seconds, regardless of access | ||
setTimeout(function() { | ||
cache.get('key'); | ||
// => undefined | ||
cache.get('key'); // => undefined | ||
cache.get('foo'); // => 'bar' | ||
}, 6 * 1000); | ||
@@ -54,4 +57,9 @@ | ||
expiry is set per individual key | ||
expiry can be set per individual key, or pass to constructor: | ||
```js | ||
// default all keys to expire in one hour | ||
var cache = new BasicCache({ expires: 3600 * 1000 }) | ||
``` | ||
Function | ||
@@ -63,32 +71,39 @@ -------- | ||
- `opts.debug`: a function to use to print debug messages, defaults to a `noop` | ||
- `opts.expires`: default expire time in ms | ||
- `opts.prefix`: string to prefix the cache keys with for the internal cache object, | ||
defaults to `basiccache_` | ||
- `opts.purgeInterval`: a time, in ms, to purge the cache of expired items, defaults to no timer` | ||
- `opts.purgeInterval`: a time, in ms, to purge the cache of expired items, defaults to no timer | ||
### cache.get(key) | ||
returns the cached item if it exists and hasn't expired. If the item doesn't | ||
exist, or has been invalidated, this function will return `undefined` | ||
Returns the cached item if it exists and hasn't expired. If the item doesn't | ||
exist, or has been invalidated, this function will return `undefined`. | ||
### cache.set(key, value, [expires]) | ||
set a key to a value, `expires` is the number of milliseconds from now when | ||
this specific cache entry expires, defaults to 5 * 60 * 1000 (5 minutes) | ||
Set a key to a value, `expires` is the number of milliseconds from now when | ||
this specific cache entry expires. If not configured, and no `opts.expires` was set, | ||
the cache will never expire. | ||
### cache.remove(key) | ||
remove an entry from the cache, no errors are thrown if the key doesn't exist or is already invalidated | ||
Remove an entry from the cache, no errors are thrown if the key doesn't exist or is already invalidated. | ||
### cache.clear() | ||
remove all entries from the cache | ||
Remove all entries from the cache. | ||
### cache.purge() | ||
remove expired items from the cache | ||
Remove expired items from the cache. | ||
### cache.sleep() | ||
### cache.startTimer() | ||
clear the purgeInterval if it was set | ||
Start the timer for purging expired items. | ||
### cache.cancelTimer() | ||
Stop the purge timer. | ||
License | ||
@@ -95,0 +110,0 @@ ------- |
var BasicCache = require('../'); | ||
var cache = new BasicCache({debug: console.log}); | ||
// set a key for 5 seconds | ||
cache.set('key', new Date(), 5 * 1000); | ||
// set a key for 500ms | ||
cache.set('key', new Date(), 500); | ||
@@ -12,9 +12,9 @@ // request the key and print it | ||
setTimeout(function() { | ||
console.log('3 seconds:\t%s', cache.get('key')); | ||
}, 3 * 1000); | ||
console.log('300ms:\t%s', cache.get('key')); | ||
}, 300); | ||
// print the key 6 seconds later so it's invalidated | ||
setTimeout(function() { | ||
console.log('6 seconds:\t%s', cache.get('key')); | ||
}, 6 * 1000); | ||
console.log('600ms:\t%s', cache.get('key')); | ||
}, 600); | ||
@@ -14,3 +14,3 @@ var BasicCache = require('../'); | ||
console.log('1s later, Remains %s item', Object.keys(cache.cache).length); | ||
cache.sleep(); | ||
cache.cancelTimer(); | ||
}, 1000) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
9297
180
109
0