Last active
August 15, 2022 21:20
-
-
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
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
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