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