Last active
June 30, 2019 10:04
-
-
Save fabienjuif/1bc26157131f699f39273bdcc5af5b33 to your computer and use it in GitHub Desktop.
Node.js / utils / cache
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
| const crypto = require('crypto') | |
| const toMD5 = object => crypto.createHash('md5').update(JSON.stringify(object)).digest('hex') | |
| module.exports = ({ | |
| timeout = (/* 1 h */ 1 * 60 * 60 * 1000), | |
| maxEntries = 100, | |
| name = 'cache', | |
| log = console.log, | |
| } = {}) => { | |
| const cache = {} | |
| const purge = () => { | |
| // timeout | |
| const keysTimeout = Object | |
| .entries(cache) | |
| .map(([key, store]) => (store.date + timeout) > Date.now() ? undefined : key) | |
| .filter(Boolean) | |
| if (keysTimeout.length > 0) { | |
| log(`[cache](${name}) timeout keys: [${keysTimeout}]`) | |
| keysTimeout.forEach((key) => { delete cache[key] }) | |
| } | |
| // max entries | |
| const entries = Object.entries(cache) | |
| if (entries.length <= maxEntries) return | |
| entries.sort((a, b) => a[1].date < b[1].date) | |
| const keysMaxEntries = entries | |
| .filter((val, index) => index >= maxEntries) | |
| .map(([key]) => key) | |
| if (keysMaxEntries.length > 0) { | |
| log(`[cache](${name}) max entries keys: [${keysMaxEntries}]`) | |
| keysMaxEntries.forEach((key) => { delete cache[key] }) | |
| } | |
| } | |
| const add = (key, value) => { | |
| purge() | |
| cache[toMD5(key)] = { | |
| value, | |
| date: Date.now(), | |
| } | |
| return value | |
| } | |
| const get = (key) => { | |
| purge() | |
| const found = cache[toMD5(key)] | |
| if (!found) return undefined | |
| return found.value | |
| } | |
| return { | |
| get, | |
| add, | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment