Last active
May 4, 2016 12:21
-
-
Save jillro/f3adf0685e85cffddc1ed53df58878ec to your computer and use it in GitHub Desktop.
A simple Javascript in memory cache with timestamp.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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