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

Expose cache age #37

Closed
sqlalchemy-bot opened this issue Jul 1, 2013 · 7 comments
Closed

Expose cache age #37

sqlalchemy-bot opened this issue Jul 1, 2013 · 7 comments

Comments

@sqlalchemy-bot
Copy link

Migrated issue, originally created by s (sontek)

Currently dogpile.cache has the created time stored in metadata in the cache, inside value.metadata["ct"] but doesn't expose this anywhere.

We have some very heavy operations and even if the cache gets invalidated, we want to let it stay stale for up to 5 minutes afterwards during high traffic but we need to pull be able to decide when to regenerate the cache based on the size of the survey.

For example,

If we have a survey that has only 10 respondents, we want to invalidate the cache every time a new respondent comes in.

If a survey has 500 respondents or more, even while new respondents are filling out the survey, we would like to leave the cache in place for up to 5 minutes before we regenerate it.

I would be happy with a get_with_metadata and get_or_create_with_metadata, that way after the call I could inspect the age of the cache from metadata['ct'] and decide if I should invalidate it.

@sqlalchemy-bot
Copy link
Author

lxyu (lxyu) wrote:

I have got similar problems as well, that I want to make a cache expire but still want the cache to be there while I'm updating it.

So I suggest you to take a look at the refresh meth, it can update the cache while others still using the old cache. If you don't have heavy concurrent issue, the current refresh implementation works very well.

@sqlalchemy-bot
Copy link
Author

lxyu (lxyu) wrote:

FYI, in my case, it's a server-client model which forbid me to use async_creator, but maybe you can use it, it also works.

@sqlalchemy-bot
Copy link
Author

s (sontek) wrote:

My problem is I don't want to regenerate the cache even if its stale. The operation is extremely heavy and the amount of traffic we get could be regenerating it all the time, so we want to allow the cache to be stale for up to 5 minutes but we need to be able to check how long it has been in cache.

@sqlalchemy-bot
Copy link
Author

Michael Bayer (zzzeek) wrote:

I think a simple flag "raw=True" on Region.get()/get_or_create(), and then we just make sure to return "value" instead of "value.payload" for both, should be simple.

@gregoiredx
Copy link
Contributor

gregoiredx commented Dec 11, 2023

Here is a draft of a PR adding a get_age(key) to CacheRegion, WDYT?
#247

I have the same need to get the age of a value in the cache, in order to be able to fill an Age header.
I find it cleaner to expose a method to get the age of a value rather than exposing the "private" metadata.

@sqla-tester
Copy link
Collaborator

Grégoire Deveaux has proposed a fix for this issue in the main branch:

add method to get the CachedValue / cached time directly https://gerrit.sqlalchemy.org/c/sqlalchemy/dogpile.cache/+/5029

@zzzeek
Copy link
Member

zzzeek commented Dec 19, 2023

I made a change to the PR to just use CachedValue for the API. This is already public.

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

No branches or pull requests

4 participants