Create a memoized future with TTL.
Create a memoized future with TTL.
From Scala: Refresh.every(1.hour) { ... }
From Java: Refresh.every(Duration.fromSeconds(3600), new Function0<Future<T>>() { \@Override public Future<T> apply() { ... } });
The amount of time between refreshes
A provider function that returns a future to refresh
A memoized version of operation
that will refresh on invocation
if more than ttl
time has passed since operation
was last called.
A single-value asynchronous cache with TTL. The provider supplies the future value when invoked and this class ensures that the provider is not invoked more frequently than the TTL. If the future fails, that failure is not cached. If more than one value is required, a FutureCache backed by a
Caffeine
cache with TTL may be more appropriate.This is useful in situations where a call to an external service returns a value that changes infrequently and we need to access that value often, for example asking a service for a list of features that it supports.
A non-memoized function like this: def getData(): Future[T] = { ... }
can be memoized with a TTL of 1 hour as follows: import com.twitter.conversions.DurationOps._ import com.twitter.cache.Refresh val getData: () => Future[T] = Refresh.every(1.hour) { ... }