Last active
May 3, 2024 03:58
-
-
Save cpitt/9d3d1a86dc5b21a38602bbd16589e178 to your computer and use it in GitHub Desktop.
Memoize a function using local storage as it's 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
/** | |
* 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 = await fn(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
Very useful function, well written. I had to make a small change from:
const result = await fn(args);
to
const result = fn.apply(null, args);