blob: ca28da790a4c88b6eba404c2e2c6328e4af4f702 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
package xyz.driver.core
import java.time.Instant
import java.util.concurrent.atomic.AtomicReference
import scala.concurrent.{ExecutionContext, Future, Promise}
import scala.concurrent.duration.Duration
/** A single-value asynchronous cache with TTL.
*
* Slightly adapted from Twitter's "util" library
* https://github.com/twitter/util/blob/ae0ab09134414438af9dfaa88a4613cecbff4741/util-cache/src/main/scala/com/twitter/cache/Refresh.scala
*
* Released under the Apache License 2.0.
*/
object Refresh {
/** Creates a function that will provide a cached value for a given time-to-live (TTL).
*
* {{{
* def freshToken(): Future[String] = // expensive network call to get an access token
* val getToken: Future[String] = Refresh.every(1.hour)(freshToken())
*
* getToken() // new token is issued
* getToken() // subsequent calls use the cached token
* // wait 1 hour
* getToken() // new token is issued
* }}} */
def every[A](ttl: Duration)(compute: => Future[A])(implicit ec: ExecutionContext): () => Future[A] = {
val ref = new AtomicReference[(Future[A], Instant)](
(Future.failed(new NoSuchElementException("Cached value was never computed")), Instant.MIN)
)
def refresh(): Future[A] = {
val tuple = ref.get
val (cachedValue, lastRetrieved) = tuple
val now = Instant.now
if (now.getEpochSecond < lastRetrieved.getEpochSecond + ttl.toSeconds) {
cachedValue
} else {
val p = Promise[A]
val nextTuple = (p.future, now)
if (ref.compareAndSet(tuple, nextTuple)) {
compute.onComplete { done =>
if (done.isFailure) {
ref.set((p.future, lastRetrieved)) // don't update retrieval time in case of failure
}
p.complete(done)
}
}
refresh()
}
}
refresh _
}
}
|