Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[minor] adding a length variable

[minor] use one options argument instead of 2 args..
[api] starting on lru implementation
  • Loading branch information...
commit 0cfa23da839016d0dbc18758c0d84317e21145db 1 parent cce7c7c
@3rd-Eden authored
Showing with 37 additions and 4 deletions.
  1. +22 −3 index.js
  2. +15 −1 tests/expirable.test.js
View
25 index.js
@@ -3,17 +3,29 @@
/**
* Simple automatic expiring cache.
*
+ * Options:
+ *
+ * - expire {String} how long should the objects stay alive
+ * - interval {String} when should the cleaning operation start.
+ *
* @constructor
* @param {Number} expire amount of miliseconds we should cache the data
* @param {Object} options options
* @api public
*/
-function Expire(expire, options) {
+function Expire(options) {
+ // Legacy formatting
+ if (typeof options === 'string') {
+ options = { expire: options };
+ }
+
options = options || {};
this.cache = {};
- this.expiree = Expire.parse(expire || '5 minutes');
+ this.length = 0;
+ this.expiree = Expire.parse(options.expire || '5 minutes');
this.interval = Expire.parse(options.interval || '2 minutes');
+ this.lru = options.lru || 0;
// Start watching for expired items.
if (!options.manually) this.start();
@@ -33,11 +45,12 @@ Expire.prototype.get = function get(key, dontUpdate) {
var now = Date.now();
- // We are still streaming in data, so return nothing
+ // We are still streaming in data, so return nothing.
if (result.streaming) return undefined;
// We found a match, make sure that it's not expired.
if (now - result.last >= result.expires) {
+ this.length--;
delete this.cache[key];
return undefined;
}
@@ -59,6 +72,8 @@ Expire.prototype.get = function get(key, dontUpdate) {
* @api public
*/
Expire.prototype.set = function set(key, value, expires) {
+ if (!(key in this.cache)) this.length++;
+
this.cache[key] = {
value: value
, expires: expires ? Expire.parse(expires) : this.expiree
@@ -136,6 +151,8 @@ Expire.prototype.expire = function expires(key, expire) {
* @api public
*/
Expire.prototype.remove = function remove(key) {
+ if (key in this.cache) this.length--;
+
delete this.cache[key];
};
@@ -154,6 +171,7 @@ Expire.prototype.scan = function scan() {
if (result.streaming) continue;
if (now - result.last >= result.expires) {
+ this.length--;
delete this.cache[key];
}
}
@@ -188,6 +206,7 @@ Expire.prototype.start = function start() {
Expire.prototype.destroy = function destroy() {
this.stop();
this.cache = {};
+ this.length = 0;
};
/**
View
16 tests/expirable.test.js
@@ -26,8 +26,10 @@ describe('Expirable', function () {
cache.set('foo', 'bar');
expect(cache.get('foo')).to.equal('bar');
+ expect(cache.length).to.equal(1);
cache.destroy();
+ expect(cache.length).to.equal(0);
});
it('should have the key after setting', function () {
@@ -37,16 +39,20 @@ describe('Expirable', function () {
expect(cache.has('foo')).to.equal(true);
cache.destroy();
+ expect(cache.length).to.equal(0);
});
it('should expire the cache', function (done) {
var cache = new Expirable('5ms');
+ expect(cache.length).to.equal(0);
cache.set('foo', 'bar');
+ expect(cache.length).to.equal(1);
setTimeout(function () {
expect(cache.has('foo')).to.equal(false);
expect(cache.get('foo')).to.equal(undefined);
+ expect(cache.length).to.equal(0);
cache.destroy();
done();
@@ -56,10 +62,14 @@ describe('Expirable', function () {
it('should remove keys', function () {
var cache = new Expirable('10000 ms');
+ expect(cache.length).to.equal(0);
cache.set('foo', 'bar');
+ expect(cache.length).to.equal(1);
+
cache.remove('foo');
- expect(cache.get('foo')).to.equal(undefined);
+ expect(cache.length).to.equal(0);
+ expect(cache.get('foo')).to.equal(undefined);
cache.destroy();
});
@@ -70,6 +80,7 @@ describe('Expirable', function () {
setTimeout(function () {
expect(cache.get('foo')).to.equal('bar');
+ expect(cache.length).to.equal(1);
cache.destroy();
done();
@@ -83,8 +94,11 @@ describe('Expirable', function () {
cache.stop();
setTimeout(function () {
+ expect(cache.length).to.equal(1);
expect(cache.cache.foo.value).to.equal('trololol');
+
expect(cache.get('foo')).to.equal(undefined);
+ expect(cache.length).to.equal(0);
cache.destroy();
done();
Please sign in to comment.
Something went wrong with that request. Please try again.