update all syncaches to expire after access or write

This commit is contained in:
Thibault Duplessis 2017-01-26 18:31:07 +01:00
parent b134b481c5
commit 9e8280102e
6 changed files with 20 additions and 13 deletions

View file

@ -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)
}

View file

@ -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)

View file

@ -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
}

View file

@ -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 _

View file

@ -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

View file

@ -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")
}