Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time based cache expiration #13

Open
ulsting opened this issue Jul 27, 2019 · 6 comments
Open

Time based cache expiration #13

ulsting opened this issue Jul 27, 2019 · 6 comments

Comments

@ulsting
Copy link

ulsting commented Jul 27, 2019

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.

@thinkloop
Copy link
Owner

thinkloop commented Jul 31, 2019

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.

@ulsting
Copy link
Author

ulsting commented Aug 4, 2019

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.

@thinkloop
Copy link
Owner

thinkloop commented Aug 8, 2019

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?

@ulsting
Copy link
Author

ulsting commented Aug 9, 2019

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.

@thinkloop
Copy link
Owner

thinkloop commented Aug 9, 2019

Ha that's great, never considered trying with async (this was built long before the feature was available), I have to think about this!

@ulsting
Copy link
Author

ulsting commented Sep 29, 2019

Hey there, have you put anymore thought into this idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants