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

Provide a TTL (time to live) cache mechanism #361

Closed
jpvajda opened this issue Nov 10, 2022 · 4 comments
Closed

Provide a TTL (time to live) cache mechanism #361

jpvajda opened this issue Nov 10, 2022 · 4 comments
Labels
📕 cache Feature requests related to the cache duplicate project-apollo-client (legacy) LEGACY TAG DO NOT USE

Comments

@jpvajda
Copy link
Contributor

jpvajda commented Nov 10, 2022

To help provide better support for Apollo Client caching strategy that uses TTLs. We are considering offering a fetchPolicy that would allow a user to set a TTL value or some other cache control mechanism that can use a TTL value for caching.

As a workaround we've recommended to create a read/merge function for our type that enriches the object with a timestamp on merge and then determine the time elapsed on read. If it is greater than the TTL you'd then schedule cache eviction and refetch the data.

This was a clever idea however some users are reporting issues with this approach:

After the first two read calls and merge call neither function is called again. Even when the page is unmounted and remounted the read function would no longer be called after the first couple times. This happens regardless of time elapsed and is reproducible by including a log statement and unconditionally returning the object from the read function.

Relates to: apollographql/apollo-kotlin#3566 (comment)

@jerelmiller
Copy link
Member

We are considering offering a fetchPolicy that would allow a user to set a TTL value.

Could you explain this a bit more? I might be misunderstanding, but I read this as we want to introduce a new fetchPolicy separate from our existing ones that adds TTL functionality.

I'd push back against that a little bit as I think a TTL config would likely be configuration on the cache, or something that would live alongside the existing fetch policies on queries that write to the cache.

@jpvajda
Copy link
Contributor Author

jpvajda commented Nov 10, 2022

@jerelmiller Understood, this was one idea we can consider what the best approach could be given some use cases that are coming up. I'll share some more details with you directly.

@jpvajda jpvajda changed the title Provide a TTL (time to live) fetchPolicy Provide a TTL (time to live) cache mechanism Nov 10, 2022
@gustavoem
Copy link

I have implemented a fetch policy with expiration, if that's of any help.

const cachedKeys = new Map<string, string>();

export const getFetchPolicyWithExpiration = (
  expiration: moment.Duration | undefined,
  key: string
) => {
  let fetchPolicy: FetchPolicy = 'cache-first';

  if (expiration) {
    const lastFetch = cachedKeys.get(key);
    const shouldFetch =
      !lastFetch || moment().diff(moment(lastFetch)) > expiration.asMilliseconds();

    if (shouldFetch) fetchPolicy = 'network-only';
  }

  const onFetchSuccess = () => {
    if (fetchPolicy === 'network-only') cachedKeys.set(key, moment().toISOString());
  };

  return [fetchPolicy, onFetchSuccess] as const;
};

However, I'm finding it hard to make this implementation work with refetchQueries.

@alessbell alessbell transferred this issue from apollographql/apollo-client Apr 28, 2023
@alessbell alessbell added the project-apollo-client (legacy) LEGACY TAG DO NOT USE label Apr 28, 2023
@jerelmiller jerelmiller added the 📕 cache Feature requests related to the cache label Apr 28, 2023
@jerelmiller
Copy link
Member

Closing this as a duplicate of #336. Will track this request in #336 instead.

@jerelmiller jerelmiller closed this as not planned Won't fix, can't repro, duplicate, stale Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📕 cache Feature requests related to the cache duplicate project-apollo-client (legacy) LEGACY TAG DO NOT USE
Projects
None yet
Development

No branches or pull requests

4 participants