Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Last active August 15, 2022 21:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save james2doyle/2a7428e6e740279f8cc7fbd2dd7b4f75 to your computer and use it in GitHub Desktop.
Save james2doyle/2a7428e6e740279f8cc7fbd2dd7b4f75 to your computer and use it in GitHub Desktop.
Use lodash memoize with a TTL. Allows calls to be cached by time as well as argument values
import { memoize, partialRight } from 'lodash';
/**
* Custom memoize that uses a 1 minute TTL
* @see https://lodash.com/docs/4.17.15#memoize
*/
const memo = partialRight(memoize, function memoResolver(...args) {
// or for one hour: (new Date()).getHours();
const time = (new Date()).getMinutes();
args.push({ time });
const cacheKey = JSON.stringify(args);
return cacheKey;
});
// because we make a new memoize, lets just pretend it’s the original
export default memo as typeof memoize;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment