-
-
Save vegemite4me/3c12c398c9ffac2d4e822a3814116b91 to your computer and use it in GitHub Desktop.
Memoize a function using local storage as it's cache
This file contains 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
/** | |
* memoizes a function using localStorage as the cache | |
* @param {function|Promise} fn function or promise you wish to memoize | |
* @param {Object} [options] additional configuration | |
* @param {number} [options.ttl=0] time to live in seconds | |
* @param {Boolean} [options.backgroundRefresh=false] whether the cache should be refreshed asynchronously, | |
* Note: new results won't resolve until next execution | |
* @return {Promise} Promise object represents memoized result | |
*/ | |
function memoizeLocalStorage( | |
fn, | |
options = { ttl: 0, backgroundRefresh: false }, | |
) { | |
if (!fn.name) | |
throw new Error('memoizeLocalStorage only accepts non-anonymous functions'); | |
// Fetch localstorage or init new object | |
const cache = JSON.parse(localStorage.getItem(fn.name) || '{}'); | |
//executes and caches result | |
async function executeAndCacheFn(fn, args, argsKey) { | |
const result = fn.apply(null, args); | |
// reset the cache value | |
cache[fn.name] = { | |
...cache[fn.name], | |
[argsKey]: { expiration: Date.now() + options.ttl, result }, | |
}; | |
localStorage.setItem(fn.name, JSON.stringify(cache)); | |
} | |
return async function() { | |
// Note: JSON.stringify is non-deterministic, | |
// consider something like json-stable-stringify to avoid extra cache misses | |
const argsKey = JSON.stringify(arguments); | |
if ( | |
!cache[fn.name] || | |
!cache[fn.name][argsKey] || | |
cache[fn.name][argsKey].expiration <= Date.now() | |
) { | |
await executeAndCacheFn(fn, arguments, argsKey); | |
} else if (options.backgroundRefresh) { | |
executeAndCacheFn(fn, arguments, argsKey); | |
} | |
return cache[fn.name][argsKey].result; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment