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

Feature Request: Add `cacheExpireTime` to invalidation data store

Open jwarykowski opened this issue 3 years ago • 4 comments
trafficstars

Hi, before I raise a PR I would love to add the following possible improvements:

  1. Add an isExpired helper to return a boolean as to whether TTL has passed for record.
cache.client.isExpired(cache.client.identity(record)) => true | false
  1. Add cacheExpireTime to the invalidation data store so we can see this next to the cacheTime of the record as defined below:

Screen Shot 2022-06-25 at 2 13 08 pm

Do you see any implications with adding these improvements?

jwarykowski avatar Jun 25 '22 04:06 jwarykowski

Hey there! Thanks for the suggestions. Let me follow up:

  1. I think this is interesting, I'm wondering what a use case could be. Maybe you're looking to expire an entity on reading it that is unrelated to a TTL per-se, rather it's related to maybe some external state or the state of other entities? Is that what you're thinking of? Also if a TTL is specified for the entity type too, I assume the idea would be that this takes precedence of a conflicting TTL expiry rule?

  2. This one I think could be solved with a utility function like cache.readEntityExpiration(ref) that would take the current cacheTime on the ref and then add its type's TTL to generate the expiration time lazily when queried vs storing that redundant info in the cache itself where it's just adding to the memory footprint. Thoughts on that?

Thanks for these proposals!

danReynolds avatar Jun 25 '22 05:06 danReynolds

Hey @danReynolds,

  1. This first proposal was more around just understanding whether the ref that is in the cache has had its TTL expired. To do this currently I'm just doing something like the following:
  const expiredEntities = client.cache.expiredEntities();

  const isExpiredTTL = expiredEntities.find(
    (id: string) => id === client.cache.identify(item)
  );

This is pretty simple to be honest, perhaps we don't need a function for this but I though it could be nice to make this in one call. Note the reason I'm doing this is simply for debugging at the moment, I just want to show whether an entity has an active or expired TTL.

https://user-images.githubusercontent.com/406799/175767708-22cab62a-462e-4bb1-99d9-4577bced808c.mov

  1. The proposal you've suggested would solve issue 1 above, I understand your concern about the memory footprint as a large cache could store data that is never/rarely used etc, so I agree this could be a waste. However if I wanted to find out the exact time the TTL would expire this could be quite useful for debugging.

Perhaps we could have a function which returns a boolean and a function which which tell me the time it will expire?

  • cache.readEntityExpired(ref) would return true or false
  • cache.readEntityExpirationTime(ref) would return a timestamp value e.g 1519211809934

Note getting its cache time would also be quite useful:

cache.readEntityCacheTIme(ref) would return a timestamp value e.g 1519211809934

jwarykowski avatar Jun 25 '22 09:06 jwarykowski

I agree! All three of those APIs sound useful. Feel free to look into that, I'd be happy to add them as well next week so whatever works.

danReynolds avatar Jun 25 '22 13:06 danReynolds

Hey @danReynolds, sounds great, I'll try and get around to this in the week 🙇

jwarykowski avatar Jun 26 '22 03:06 jwarykowski