Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: serby/ttl-lru-cache
base: fdf5cc8aa3
...
head fork: serby/ttl-lru-cache
compare: ee4b73343c
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 3 files changed
  • 0 commit comments
  • 2 contributors
Showing with 55 additions and 5 deletions.
  1. +9 −0 README.md
  2. +22 −5 lib/cache.js
  3. +24 −0 test/cache.test.js
View
9 README.md
@@ -28,6 +28,15 @@ setTimeout(function() {
// b = undefined
}, 2000);
+// Events
+cache.set('c', 'Hello', 1000); // Add TTL in ms
+cache.on('expired', function(key, value){ console.log('Expired', key, 'who had the value:', value); });
+
+setTimeout(function() {
+ var c = cache.get('c');
+ // => Expired c who had the value: Hello
+}, 2000);
+
```
## Benchmark against lru-cache
View
27 lib/cache.js
@@ -1,4 +1,5 @@
-var _ = require('lodash');
+var _ = require('lodash')
+ , EventEmitter = require('events').EventEmitter;
module.exports = function(options) {
@@ -6,6 +7,7 @@ module.exports = function(options) {
, lru
, lruId = 0
, gcHandle
+ , instance
;
options = _.extend({
@@ -35,7 +37,7 @@ module.exports = function(options) {
Object.keys(cache).forEach(function(key) {
var item = cache[key];
if (item.expire <= Date.now()) {
- del(key);
+ expire(key, item);
}
});
lruClean();
@@ -49,14 +51,20 @@ module.exports = function(options) {
for (var i = 0; i < overage; i++) {
cacheId = lru.shift();
+ var item = cache[cacheId];
delete cache[cacheId];
}
}
+ function expire(key, item) {
+ del(key);
+ instance.emit("expired", key, item.value);
+ }
+
clear();
gcHandle = setInterval(garbageCollection, options.gcInterval);
- return {
+ instance = {
set: function(key, value, ttl) {
if (typeof key === 'undefined') {
@@ -77,7 +85,12 @@ module.exports = function(options) {
var response
, item = cache[key];
- if ((item) && ((!item.expire) || (item.expire) && (item.expire >= Date.now()))) {
+ if (item) {
+ if ((item.expire) && (item.expire < Date.now())) {
+ expire(key, item);
+ return undefined;
+ }
+
response = item.value;
delete lru[item.lruId];
@@ -100,4 +113,8 @@ module.exports = function(options) {
clearInterval(gcHandle);
}
};
-};
+
+ instance = _.extend(instance, new EventEmitter());
+
+ return instance;
+};
View
24 test/cache.test.js
@@ -34,6 +34,17 @@ describe('ttl-lru-cache', function() {
done();
}, 15);
});
+ it('should emit when TTL has been exceeded', function(done) {
+ var memory = createCache();
+ var emitted = 0;
+ memory.on('expired', function(){ emitted++; });
+ memory.set('test', 'hello', 10);
+ setTimeout(function() {
+ should.equal(memory.get('test'), undefined);
+ should.equal(emitted, 1);
+ done();
+ }, 15);
+ });
});
describe('#set()', function() {
@@ -137,6 +148,19 @@ describe('ttl-lru-cache', function() {
}, 2);
});
+ it('should emit expired after gc when something has expired', function(done) {
+ var memory = createCache();
+ var emitted = 0;
+ memory.on('expired', function(){ emitted++; });
+ memory.set('test', 'hello', 1);
+ memory.size().should.eql(1);
+ setTimeout(function() {
+ memory.size().should.eql(0); // internally triggers #garbageCollection
+ should.equal(emitted, 1);
+ done();
+ }, 15);
+ });
+
it('should not exceed cache length', function() {
var memory = createCache({ maxLength: 3 });

No commit comments for this range

Something went wrong with that request. Please try again.