diff --git a/modules/api/src/main/AssetVersionApi.scala b/modules/api/src/main/AssetVersionApi.scala index fc21ebffe9..beb0eb349a 100644 --- a/modules/api/src/main/AssetVersionApi.scala +++ b/modules/api/src/main/AssetVersionApi.scala @@ -26,6 +26,6 @@ final class AssetVersionApi( }, default = _ => lastVersion, strategy = Syncache.NeverWait, - timeToLive = 5.seconds, + expireAfter = Syncache.ExpireAfterWrite(5 seconds), logger = lila.log("assetVersion"))(system) } diff --git a/modules/blog/src/main/LastPostCache.scala b/modules/blog/src/main/LastPostCache.scala index 698d58b567..df9feb8b70 100644 --- a/modules/blog/src/main/LastPostCache.scala +++ b/modules/blog/src/main/LastPostCache.scala @@ -13,7 +13,7 @@ final class LastPostCache( name = "blog.lastPost", compute = _ => fetch, default = _ => Nil, - timeToLive = ttl, + expireAfter = Syncache.ExpireAfterWrite(ttl), strategy = Syncache.NeverWait, logger = logger) diff --git a/modules/memo/src/main/Syncache.scala b/modules/memo/src/main/Syncache.scala index fca7d9858f..5f482011ff 100644 --- a/modules/memo/src/main/Syncache.scala +++ b/modules/memo/src/main/Syncache.scala @@ -1,8 +1,8 @@ package lila.memo import com.github.benmanes.caffeine.cache._ -import java.util.concurrent.TimeUnit import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.TimeUnit import scala.concurrent.duration._ /** @@ -16,7 +16,7 @@ final class Syncache[K, V]( compute: K => Fu[V], default: K => V, strategy: Syncache.Strategy, - timeToLive: FiniteDuration, + expireAfter: Syncache.ExpireAfter, logger: lila.log.Logger, resultTimeout: FiniteDuration = 5 seconds)(implicit system: akka.actor.ActorSystem) { @@ -26,10 +26,14 @@ final class Syncache[K, V]( private val chm = new ConcurrentHashMap[K, Fu[V]] // sync cached values - private val cache: Cache[K, V] = Caffeine.newBuilder() - .asInstanceOf[Caffeine[K, V]] - .expireAfterAccess(timeToLive.toMillis, TimeUnit.MILLISECONDS) - .build[K, V]() + private val cache: Cache[K, V] = { + val b1 = Caffeine.newBuilder().asInstanceOf[Caffeine[K, V]] + val b2 = expireAfter match { + case ExpireAfterAccess(duration) => b1.expireAfterAccess(duration.toMillis, TimeUnit.MILLISECONDS) + case ExpireAfterWrite(duration) => b1.expireAfterWrite(duration.toMillis, TimeUnit.MILLISECONDS) + } + b2.build[K, V]() + } // get the value synchronously, might block depending on strategy def sync(k: K): V = { @@ -121,8 +125,11 @@ final class Syncache[K, V]( object Syncache { sealed trait Strategy - case object NeverWait extends Strategy case class AlwaysWait(duration: FiniteDuration) extends Strategy case class WaitAfterUptime(duration: FiniteDuration, uptimeSeconds: Int = 12) extends Strategy + + sealed trait ExpireAfter + case class ExpireAfterAccess(duration: FiniteDuration) extends ExpireAfter + case class ExpireAfterWrite(duration: FiniteDuration) extends ExpireAfter } diff --git a/modules/team/src/main/Cached.scala b/modules/team/src/main/Cached.scala index 7224cd739c..5fda6bf520 100644 --- a/modules/team/src/main/Cached.scala +++ b/modules/team/src/main/Cached.scala @@ -10,7 +10,7 @@ private[team] final class Cached(implicit system: akka.actor.ActorSystem) { compute = TeamRepo.name, default = _ => none, strategy = Syncache.WaitAfterUptime(20 millis), - timeToLive = 12 hours, + expireAfter = Syncache.ExpireAfterAccess(1 hour), logger = logger) def name(id: String) = nameCache sync id @@ -20,7 +20,7 @@ private[team] final class Cached(implicit system: akka.actor.ActorSystem) { compute = MemberRepo.teamIdsByUser, default = _ => Set.empty, strategy = Syncache.WaitAfterUptime(20 millis), - timeToLive = 2 hours, + expireAfter = Syncache.ExpireAfterAccess(1 hour), logger = logger) def syncTeamIds = teamIdsCache sync _ diff --git a/modules/tournament/src/main/Cached.scala b/modules/tournament/src/main/Cached.scala index 9cf772902f..01a565541f 100644 --- a/modules/tournament/src/main/Cached.scala +++ b/modules/tournament/src/main/Cached.scala @@ -13,7 +13,7 @@ private[tournament] final class Cached( compute = id => TournamentRepo byId id map2 { (tour: Tournament) => tour.fullName }, default = _ => none, strategy = Syncache.WaitAfterUptime(20 millis), - timeToLive = 6 hours, + expireAfter = Syncache.ExpireAfterAccess(1 hour), logger = logger) def name(id: String): Option[String] = nameCache sync id diff --git a/modules/user/src/main/LightUserApi.scala b/modules/user/src/main/LightUserApi.scala index b102760918..60889811e5 100644 --- a/modules/user/src/main/LightUserApi.scala +++ b/modules/user/src/main/LightUserApi.scala @@ -30,7 +30,7 @@ final class LightUserApi(coll: Coll)(implicit system: akka.actor.ActorSystem) { compute = id => coll.find($id(id), projection).uno[LightUser], default = id => LightUser(id, id, None, false).some, strategy = Syncache.WaitAfterUptime(10 millis), - timeToLive = 20 minutes, + expireAfter = Syncache.ExpireAfterAccess(20 minutes), logger = logger branch "LightUserApi") }