Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[minor] Added Expirable#forEach
[dist] 0.0.5
  • Loading branch information
3rd-Eden committed Feb 20, 2013
1 parent 751e4b0 commit 19ce98b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -59,4 +59,13 @@ var stream = cache.stream('key', fs.createReadStream(..), '10 seconds');

// stream is the result of fs.createReadStream
// once the stream fires it's `done` event, we will store the data.

// iterate over the cache
cache.forEach(function (key, value) {
console.log(key, value);
});
```

## License

MIT
24 changes: 24 additions & 0 deletions index.js
Expand Up @@ -192,6 +192,30 @@ Expire.prototype.remove = function remove(key, expired) {
return this;
};

/**
* Iterate over all the keys in our cache.
*
* @param {Function} iterator
* @param {String} context
* @api public
*/
Expire.prototype.forEach = function forEach(iterator, context) {
var now = Date.now();

Object.keys(this.cache).forEach(function iterating(key) {
var data = this.cache[key];

// Make sure that it's not expired.
if (now - data.last >= data.expires) {
return this.remove(key, true);
}

iterator.call(context || this, key, data.value, data.expires);
}, this);

return this;
};

/**
* Scans the cache for potential items that should expire.
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "expirable",
"version": "0.0.4",
"version": "0.0.5",
"description": "Expirable cache",
"main": "index.js",
"scripts": {
Expand Down
13 changes: 13 additions & 0 deletions tests/expirable.test.js
Expand Up @@ -150,4 +150,17 @@ describe('Expirable', function () {
done();
});
});

it('should be able to iterate over the cache', function () {
var cache = new Expirable('10 hours');

cache.set('foo', 'bar');
cache.forEach(function (key, value) {
expect(this).to.equal(cache);
expect(key).to.equal('foo');
expect(value).to.equal('bar');
});

cache.destroy();
});
});

0 comments on commit 19ce98b

Please sign in to comment.