Ron Badur
Ron Badur

Reputation: 1973

set time to live to keys in data loader

Hey there is any way to set time to live to keys in data-loader (library for caching and batching created by facebook developers)?

In their docs there is only clearAll() function but I want to clear each key separately after X seconds from the moment I inserted it to the cache

Upvotes: 0

Views: 1005

Answers (1)

Daniel Rearden
Daniel Rearden

Reputation: 84807

DataLoader's cache only lives for the length of the request and is specific to that individual request. When used with GraphQL, each DataLoader instance is recreated with the request's context. From the documentation:

DataLoader caching does not replace Redis, Memcache, or any other shared application-level cache. DataLoader is first and foremost a data loading mechanism, and its cache only serves the purpose of not repeatedly loading the same data in the context of a single request to your Application. To do this, it maintains a simple in-memory memoization cache (more accurately: .load() is a memoized function).

While there are clear and clearAll methods available on the DataLoader instance, these methods are only meant to be used in some edge cases where you're running multiple mutations in the same request and a mutation might cause a cached value to be stale within the same request.

While you could foreseeably use timeouts to achieve what you're asking, there should be no need to invalidate or clear the cache after a set amount of time -- it'll be gone as soon as your request completes.

Upvotes: 1

Related Questions