Skip to content

Commit

Permalink
periodic cache cleanup to free server memory when running uninterrupt…
Browse files Browse the repository at this point in the history
…ed for long period
  • Loading branch information
yohny committed Aug 11, 2015
1 parent 1c24718 commit e87b1b9
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var config = require('./config');

module.exports = new (function() { // jshint ignore:line
var cache = {};
setInterval(cleanUp, config.cacheExpiration);

this.set = function(key, value) {
if(typeof value !== "object")
Expand All @@ -20,7 +21,11 @@ module.exports = new (function() { // jshint ignore:line
return !!enterTime && (enterTime + config.cacheExpiration > Date.now());
}

this.clear = function() {
cache = {};
};
function cleanUp() {
for(var key in cache){
if(cache.hasOwnProperty(key) && !isValid(key)){
delete cache[key];
}
}
}
})();

0 comments on commit e87b1b9

Please sign in to comment.