Time based cache expiration
Along with specifying an amount of cached parameter mutations, it would be nice to also be able to specify a time limit, or maximum age, a cached parameter mutation persists.
For example: // memoize the 50 most recent argument combinations of our function // each argument combination will persist for 5000ms = 5 seconds const memoized = memoizerific(50, 5000)(function(arg1, arg2, arg3) { // many long expensive calls here });
memoized(1, 2, 3); // that took long to process memoized(1, 2, 3); // this one was instant! setTimeout(memoized(1, 2, 3), 6000); // takes long to process
Or perhaps provide a callback function to be able to manually clear the cache: memoized.clearCache();
However, I think the former would be more useful and cleaner.
Thank you for the suggestion.
Would you mind sharing the real-world use-case that made you request this? I can think of some for sure, but I would be interested to know yours.
This hasn't been requested before, and the mission of this project is extreme speed and extreme simplicity, so I am hesitant to double the API size willynilly. I would like to leave it open for a while to see if there is other interest.
Thanks for the reply!
Sure, I'd be happy to. My use-case is for an Express back-end endpoint which returns the result of a function which makes a couple of Postgres queries. This endpoint will not be hit often, but when it is hit, it is expected to be hit by a few thousand users within 60-120 seconds. Also, after each user's client hits the endpoint, they will more likely than not take an action which will cause the calculated result of this endpoint to change. So, I would like to cache the result for 5-10 seconds so that it stays fairly up to date with all the changes happening in the DB, but it's not critical that it stay exactly up-to-date.
Currently, without the timed cache, this endpoint would return the same result for all the thousands of requests within the 1-2 minute time-range.
Hey,
I don't think you can do that with this lib, it's synchronous. The cached result would be the result of sending the request, not receiving the data. Do you have it working?
I do have this working, the following has worked for me:
const queryAlias = async (params) => {
return query(QUERY_STRING, params);
}
const memoizedQuery = memoizerific(1)(queryAlias);
const params = [1,2,3];
memoizedQuery(params); //Takes ~1500 ms to return
memoizedQuery(params); //Returns instantly
with query() itself being an async function.
Ha that's great, never considered trying with async (this was built long before the feature was available), I have to think about this!
Hey there, have you put anymore thought into this idea?