Skip to content

Commit

Permalink
Merge pull request #10 from loris/patch-1
Browse files Browse the repository at this point in the history
Add support for callback event on key expiration
  • Loading branch information
ptarjan committed Feb 5, 2013
2 parents 31c7ce0 + 4d5d7cd commit 18cc1e5
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var debug = false;
var hitCount = 0; var hitCount = 0;
var missCount = 0; var missCount = 0;


exports.put = function(key, value, time) { exports.put = function(key, value, time, timeoutCallback) {
if (debug) console.log('caching: '+key+' = '+value+' (@'+time+')'); if (debug) console.log('caching: '+key+' = '+value+' (@'+time+')');
var oldRecord = cache[key]; var oldRecord = cache[key];
if (oldRecord) { if (oldRecord) {
Expand All @@ -17,6 +17,9 @@ exports.put = function(key, value, time) {
if (!isNaN(expire)) { if (!isNaN(expire)) {
var timeout = setTimeout(function() { var timeout = setTimeout(function() {
exports.del(key); exports.del(key);
if (typeof timeoutCallback === 'function') {
timeoutCallback(key);
}
}, time); }, time);
record.timeout = timeout; record.timeout = timeout;
} }
Expand Down

0 comments on commit 18cc1e5

Please sign in to comment.