Skip to content

Commit 4c52ee5

Browse files
committed
It's better if it's traverse! (no, really)
By traversing on the entry's TTL we get the opportunity to avoid calculating the time in case there's no TTL. That might give us some extra performance in case of infinitely long-lived keys. The constraint is more powerful, but all the caching implementations will be at least monads, so it's fine. And Clock is only possible to implement in a real scenario for instances of Sync.
1 parent 1b33e95 commit 4c52ee5

File tree

1 file changed

+8
-4
lines changed
  • modules/core/shared/src/main/scala/scalacache

1 file changed

+8
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package scalacache
22

3-
import java.time.{Instant}
3+
import java.time.Instant
44
import cats.effect.Clock
55
import java.util.concurrent.TimeUnit
66
import cats.implicits._
77
import language.higherKinds
8-
import cats.Functor
8+
import cats.Applicative
99

1010
/**
1111
* A cache entry with an optional expiry time
@@ -15,6 +15,10 @@ case class Entry[F[_], +A](value: A, expiresAt: Option[Instant]) {
1515
/**
1616
* Has the entry expired yet?
1717
*/
18-
def isExpired(implicit clock: Clock[F], functor: Functor[F]): F[Boolean] =
19-
clock.monotonic(TimeUnit.MILLISECONDS).map(Instant.ofEpochMilli(_)).map(now => expiresAt.exists(_.isBefore(now)))
18+
def isExpired(implicit clock: Clock[F], applicative: Applicative[F]): F[Boolean] =
19+
expiresAt
20+
.traverse { ttl =>
21+
clock.monotonic(TimeUnit.MILLISECONDS).map(Instant.ofEpochMilli(_)).map(ttl.isBefore(_))
22+
}
23+
.map(_.contains(true))
2024
}

0 commit comments

Comments
 (0)