Skip to content

Expose cache age #37

Description

@sqlalchemy-bot

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.

Activity

  1. sqlalchemy-bot commented on Jul 2, 2013

    @sqlalchemy-bot
    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.

  2. sqlalchemy-bot commented on Jul 2, 2013

    @sqlalchemy-bot
    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.

  3. sqlalchemy-bot commented on Jul 2, 2013

    @sqlalchemy-bot
    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.

  4. sqlalchemy-bot commented on Jul 2, 2013

    @sqlalchemy-bot
    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.

  5. gregoiredx commented on Dec 11, 2023

    @gregoiredx

    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.

  6. sqla-tester commented on Dec 19, 2023

    @sqla-tester
    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

  7. zzzeek commented on Dec 19, 2023

    @zzzeek
    Member

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

  8. added a commit that references this issue on Jun 27, 2024
    074cb77
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions