apollo-cache-policies icon indicating copy to clipboard operation
apollo-cache-policies copied to clipboard

Different TTL for queries and objects in the cache

Open tolmachev21 opened this issue 1 year ago • 6 comments

Hi guys! Thank you very much for the opportunity to use this library. I would be very grateful if you could help me with the following problem :)

Minimum context:

I want to use the following invalidationPolicy configuration settings:

invalidationPolicies: {
        timeToLive: 15 * 60 * 1000, // 15 minutes in milliseconds
        types: {
            User: {
                timeToLive: 60 * 1000, // 1 minute in milliseconds
            },
        },
    }

It turns out that queries inside ROOT_QUERY will live for 15 minutes. And the cached objects themselves will live for one minute.

Problem description (page and query names are used as an example only):

  1. I go to the /user page, which uses the getUser query. When it is executed, the query itself (i.e. getUser) and the response of this query (i.e. the User object itself) are cached.

  2. Then I wait one minute for the User object in the cache to become invalid (i.e. the TTL has expired). It is worth noting that the getUser query is still valid.

  3. I go to the page /. At this point, the getUser query remains in the cache inside ROOT_QUERY, but the cached User object is not there (it was deleted because it was invalid).

  4. I go back to the page /user within 15 minutes. And now I have no data on the page, because the ROOT_QUERY still contains broken links to objects in the cache, which are no longer in this cache!

Expected behavior:

First, I would like to clarify whether this behavior is expected? If yes, what would you recommend me to avoid this behavior. If not, what option is better to use:

  1. Check object references in ROOT_QUERY for null / undefined? (but as I understand - you need to go to Apollo Client with this question)
  2. Delete query objects (or reference objects themselves) when deleting entities whose TTL has expired?

tolmachev21 avatar Jan 31 '25 07:01 tolmachev21