syncache initial capacity
parent
0cc6ec7866
commit
b97ae44691
|
@ -13,6 +13,7 @@ final class LastPostCache(
|
|||
|
||||
private val cache = new Syncache[Boolean, List[MiniPost]](
|
||||
name = "blog.lastPost",
|
||||
initialCapacity = 1,
|
||||
compute = _ => fetch,
|
||||
default = _ => Nil,
|
||||
expireAfter = Syncache.ExpireAfterWrite(ttl),
|
||||
|
|
|
@ -16,6 +16,7 @@ import lila.common.Uptime
|
|||
*/
|
||||
final class Syncache[K, V](
|
||||
name: String,
|
||||
initialCapacity: Int,
|
||||
compute: K => Fu[V],
|
||||
default: K => V,
|
||||
strategy: Syncache.Strategy,
|
||||
|
@ -27,28 +28,34 @@ final class Syncache[K, V](
|
|||
import Syncache._
|
||||
|
||||
// sync cached values
|
||||
private val cache: LoadingCache[K, Fu[V]] = {
|
||||
val b1 = Caffeine.newBuilder().asInstanceOf[Caffeine[K, Fu[V]]]
|
||||
val b2 = expireAfter match {
|
||||
case ExpireAfterAccess(duration) => b1.expireAfterAccess(duration.toMillis, TimeUnit.MILLISECONDS)
|
||||
case ExpireAfterWrite(duration) => b1.expireAfterWrite(duration.toMillis, TimeUnit.MILLISECONDS)
|
||||
}
|
||||
b2.recordStats.build[K, Fu[V]](new CacheLoader[K, Fu[V]] {
|
||||
def load(k: K) =
|
||||
compute(k)
|
||||
.withTimeout(
|
||||
duration = resultTimeout,
|
||||
error = lila.base.LilaException(s"Syncache $name $k timed out after $resultTimeout")
|
||||
)
|
||||
.mon(_ => recCompute) // monitoring: record async time
|
||||
.recover {
|
||||
case e: Exception =>
|
||||
logger.branch(s"syncache $name").warn(s"key=$k", e)
|
||||
cache invalidate k
|
||||
default(k)
|
||||
}
|
||||
})
|
||||
} tap {
|
||||
private val cache: LoadingCache[K, Fu[V]] =
|
||||
Caffeine
|
||||
.newBuilder()
|
||||
.asInstanceOf[Caffeine[K, Fu[V]]]
|
||||
.initialCapacity(initialCapacity)
|
||||
.pipe { c =>
|
||||
expireAfter match {
|
||||
case ExpireAfterAccess(duration) => c.expireAfterAccess(duration.toMillis, TimeUnit.MILLISECONDS)
|
||||
case ExpireAfterWrite(duration) => c.expireAfterWrite(duration.toMillis, TimeUnit.MILLISECONDS)
|
||||
}
|
||||
}
|
||||
.recordStats
|
||||
.build[K, Fu[V]](new CacheLoader[K, Fu[V]] {
|
||||
def load(k: K) =
|
||||
compute(k)
|
||||
.withTimeout(
|
||||
duration = resultTimeout,
|
||||
error = lila.base.LilaException(s"Syncache $name $k timed out after $resultTimeout")
|
||||
)
|
||||
.mon(_ => recCompute) // monitoring: record async time
|
||||
.recover {
|
||||
case e: Exception =>
|
||||
logger.branch(s"syncache $name").warn(s"key=$k", e)
|
||||
cache invalidate k
|
||||
default(k)
|
||||
}
|
||||
})
|
||||
.tap {
|
||||
lila.memo.AsyncCache.startMonitoring(s"syncache.$name", _)
|
||||
}
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ class MySpec()
|
|||
var computeCount = 0
|
||||
val cache = new Syncache[Int, String](
|
||||
name = "test",
|
||||
initialCapacity = 64,
|
||||
compute = s =>
|
||||
Future {
|
||||
computeCount += 1
|
||||
|
|
|
@ -12,10 +12,11 @@ final class Cached(
|
|||
|
||||
val nameCache = new Syncache[String, Option[String]](
|
||||
name = "team.name",
|
||||
initialCapacity = 4096,
|
||||
compute = teamRepo.name,
|
||||
default = _ => none,
|
||||
strategy = Syncache.WaitAfterUptime(20 millis),
|
||||
expireAfter = Syncache.ExpireAfterAccess(1 hour),
|
||||
expireAfter = Syncache.ExpireAfterAccess(30 minutes),
|
||||
logger = logger
|
||||
)
|
||||
|
||||
|
@ -23,9 +24,10 @@ final class Cached(
|
|||
|
||||
def preloadSet = nameCache preloadSet _
|
||||
|
||||
// ~ 30k entries as of 04/02/17
|
||||
// ~ 50k entries as of 14/12/19
|
||||
private val teamIdsCache = new Syncache[lila.user.User.ID, Team.IdsStr](
|
||||
name = "team.ids",
|
||||
initialCapacity = 32768,
|
||||
compute = u => memberRepo.teamIdsByUser(u).dmap(Team.IdsStr.apply),
|
||||
default = _ => Team.IdsStr.empty,
|
||||
strategy = Syncache.WaitAfterUptime(20 millis),
|
||||
|
|
|
@ -17,6 +17,7 @@ final private[tournament] class Cached(
|
|||
|
||||
val nameCache = new Syncache[Tournament.ID, Option[String]](
|
||||
name = "tournament.name",
|
||||
initialCapacity = 8192,
|
||||
compute = id => tournamentRepo byId id dmap2 { _.fullName },
|
||||
default = _ => none,
|
||||
strategy = Syncache.WaitAfterUptime(20 millis),
|
||||
|
|
|
@ -31,6 +31,7 @@ final class LightUserApi(repo: UserRepo)(
|
|||
|
||||
private val cache = new Syncache[User.ID, Option[LightUser]](
|
||||
name = cacheName,
|
||||
initialCapacity = 65536,
|
||||
compute = id => repo.coll.find($id(id), projection.some).one[LightUser],
|
||||
default = id => LightUser(id, id, None, false).some,
|
||||
strategy = Syncache.WaitAfterUptime(10 millis),
|
||||
|
|
|
@ -16,6 +16,7 @@ final class TrophyApi(
|
|||
|
||||
val kindCache = new Syncache[String, TrophyKind](
|
||||
name = "trophy.kind",
|
||||
initialCapacity = 32,
|
||||
compute = id =>
|
||||
kindColl.byId(id)(trophyKindObjectBSONHandler) map { k =>
|
||||
k.getOrElse(TrophyKind.Unknown)
|
||||
|
|
Loading…
Reference in New Issue