Skip to content

Instantly share code, notes, and snippets.

@jillro
Last active May 4, 2016 12:21
Show Gist options
  • Select an option

  • Save jillro/f3adf0685e85cffddc1ed53df58878ec to your computer and use it in GitHub Desktop.

Select an option

Save jillro/f3adf0685e85cffddc1ed53df58878ec to your computer and use it in GitHub Desktop.
A simple Javascript in memory cache with timestamp.
// Cache object
var cache = {};
var getInCache = (name, date) => {
var noDate = (typeof date === 'undefined');
if (cache[name] && (noDate || cache[name].date.getTime() > date.getTime())) {
//debug('Hit ' + name + ' in cache.');
return cache[name].data;
}
//debug('Misses ' + name + ' in cache.');
};
var putInCache = (name, data) => {
cache[name] = {
data: data,
date: new Date()
};
//debug('Stored ' + name + ' in cache.');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment