also name single async caches and mixed cache
parent
6150edf09f
commit
dad62cb5c7
|
@ -18,6 +18,7 @@ object Prismic {
|
|||
}
|
||||
|
||||
private val fetchPrismicApi = AsyncCache.single[PrismicApi](
|
||||
name = "prismic.fetchPrismicApi",
|
||||
f = PrismicApi.get(Env.api.PrismicApiUrl, logger = prismicLogger),
|
||||
timeToLive = 1 minute)
|
||||
|
||||
|
|
|
@ -56,6 +56,7 @@ final class Env(
|
|||
import lila.db.dsl._
|
||||
private val coll = db("flag")
|
||||
private val cache = lila.memo.MixedCache.single[Int](
|
||||
name = "asset.version",
|
||||
f = coll.primitiveOne[BSONNumberLike]($id("asset"), "version").map {
|
||||
_.fold(Net.AssetVersion)(_.toInt max Net.AssetVersion)
|
||||
},
|
||||
|
|
|
@ -39,7 +39,7 @@ final class BlogApi(prismicUrl: String, collection: String) {
|
|||
}
|
||||
|
||||
private val fetchPrismicApi = AsyncCache.single[Api](
|
||||
// name = "blogApi.fetchPrismicApi",
|
||||
name = "blogApi.fetchPrismicApi",
|
||||
f = Api.get(prismicUrl, cache = cache, logger = prismicLogger),
|
||||
timeToLive = 10 seconds)
|
||||
|
||||
|
|
|
@ -5,7 +5,8 @@ import scala.concurrent.duration._
|
|||
final class LastPostCache(api: BlogApi, ttl: Duration, collection: String) {
|
||||
|
||||
private val cache = lila.memo.MixedCache.single[List[MiniPost]](
|
||||
api.prismicApi flatMap { prismic =>
|
||||
name = "blog.lastPost",
|
||||
f = api.prismicApi flatMap { prismic =>
|
||||
api.recent(prismic, none, 3) map {
|
||||
_ ?? {
|
||||
_.results.toList flatMap MiniPost.fromDocument(collection)
|
||||
|
|
|
@ -18,6 +18,7 @@ final class CoachApi(
|
|||
import BsonHandlers._
|
||||
|
||||
private val cache = AsyncCache.single[List[Coach]](
|
||||
name = "coach.list",
|
||||
f = coachColl.find($empty).list[Coach](),
|
||||
timeToLive = 10 minutes)
|
||||
|
||||
|
|
|
@ -10,7 +10,9 @@ final class EventApi(coll: Coll) {
|
|||
|
||||
import BsonHandlers._
|
||||
|
||||
val promotable = AsyncCache.single(fetchPromotable, timeToLive = 5 minutes)
|
||||
val promotable = AsyncCache.single(
|
||||
name = "event.promotable",
|
||||
fetchPromotable, timeToLive = 5 minutes)
|
||||
|
||||
def fetchPromotable: Fu[List[Event]] = coll.find($doc(
|
||||
"enabled" -> true,
|
||||
|
|
|
@ -16,7 +16,9 @@ private[i18n] final class Context(gitUrl: String, gitFile: String, keys: I18nKey
|
|||
|
||||
def get: Fu[Contexts] = cache(true)
|
||||
|
||||
private val cache = AsyncCache.single[Contexts](fetch, timeToLive = 1 hour)
|
||||
private val cache = AsyncCache.single[Contexts](
|
||||
name = "i18n.contexts",
|
||||
fetch, timeToLive = 1 hour)
|
||||
|
||||
private def parse(text: String): Contexts =
|
||||
text.lines.toList.map(_.trim).filter(_.nonEmpty).map(_.split('=')).foldLeft(Map[String, String]()) {
|
||||
|
|
|
@ -34,8 +34,12 @@ object AsyncCache {
|
|||
)
|
||||
|
||||
def single[V](
|
||||
name: String,
|
||||
f: => Fu[V],
|
||||
timeToLive: Duration = Duration.Inf) = new AsyncCache[Boolean, V](
|
||||
timeToLive: Duration = Duration.Inf,
|
||||
resultTimeout: FiniteDuration = 5 seconds) = new AsyncCache[Boolean, V](
|
||||
cache = LruCache(timeToLive = timeToLive),
|
||||
f = _ => f)
|
||||
f = _ => f.withTimeout(
|
||||
duration = resultTimeout,
|
||||
error = lila.common.LilaException(s"AsyncCache $name single timed out after $resultTimeout")))
|
||||
}
|
||||
|
|
|
@ -30,37 +30,39 @@ object MixedCache {
|
|||
async.remove(k) >>- sync.invalidate(k)
|
||||
|
||||
def apply[K, V](
|
||||
name: String,
|
||||
f: K => Fu[V],
|
||||
timeToLive: Duration = Duration.Inf,
|
||||
awaitTime: FiniteDuration = 10.milliseconds,
|
||||
default: K => V,
|
||||
logger: lila.log.Logger): MixedCache[K, V] = {
|
||||
val async = AsyncCache("MixedCache", f, maxCapacity = 10000, timeToLive = 1 minute)
|
||||
val async = AsyncCache(name, f, maxCapacity = 10000, timeToLive = 1 minute)
|
||||
val sync = Builder.cache[K, V](
|
||||
timeToLive,
|
||||
(k: K) => async(k) await makeTimeout(awaitTime))
|
||||
new MixedCache(sync, default, invalidate(async, sync) _, logger branch "MixedCache")
|
||||
}
|
||||
|
||||
def fromAsync[K, V](
|
||||
async: AsyncCache[K,V],
|
||||
timeToLive: Duration = Duration.Inf,
|
||||
awaitTime: FiniteDuration = 10.milliseconds,
|
||||
default: K => V,
|
||||
logger: lila.log.Logger): MixedCache[K, V] = {
|
||||
val sync = Builder.cache[K, V](
|
||||
timeToLive,
|
||||
(k: K) => async(k) await makeTimeout(awaitTime))
|
||||
new MixedCache(sync, default, invalidate(async, sync) _, logger branch "MixedCache")
|
||||
}
|
||||
// def fromAsync[K, V](
|
||||
// async: AsyncCache[K,V],
|
||||
// timeToLive: Duration = Duration.Inf,
|
||||
// awaitTime: FiniteDuration = 10.milliseconds,
|
||||
// default: K => V,
|
||||
// logger: lila.log.Logger): MixedCache[K, V] = {
|
||||
// val sync = Builder.cache[K, V](
|
||||
// timeToLive,
|
||||
// (k: K) => async(k) await makeTimeout(awaitTime))
|
||||
// new MixedCache(sync, default, invalidate(async, sync) _, logger branch "MixedCache")
|
||||
// }
|
||||
|
||||
def single[V](
|
||||
name: String,
|
||||
f: => Fu[V],
|
||||
timeToLive: Duration = Duration.Inf,
|
||||
awaitTime: FiniteDuration = 5.milliseconds,
|
||||
default: V,
|
||||
logger: lila.log.Logger): MixedCache[Boolean, V] = {
|
||||
val async = AsyncCache.single(f, timeToLive = 1 minute)
|
||||
val async = AsyncCache.single(name, f, timeToLive = 1 minute)
|
||||
val sync = Builder.cache[Boolean, V](
|
||||
timeToLive,
|
||||
(_: Boolean) => async(true) await makeTimeout(awaitTime))
|
||||
|
|
|
@ -56,6 +56,7 @@ final class Gamify(
|
|||
def leaderboards = leaderboardsCache(true)
|
||||
|
||||
private val leaderboardsCache = AsyncCache.single[Leaderboards](
|
||||
name = "mod.leaderboards",
|
||||
f = mixedLeaderboard(DateTime.now minusDays 1, none) zip
|
||||
mixedLeaderboard(DateTime.now minusWeeks 1, none) zip
|
||||
mixedLeaderboard(DateTime.now minusMonths 1, none) map {
|
||||
|
|
|
@ -15,6 +15,7 @@ private[puzzle] final class Daily(
|
|||
|
||||
private val cache =
|
||||
lila.memo.AsyncCache.single[Option[DailyPuzzle]](
|
||||
name = "puzzle.daily",
|
||||
f = find,
|
||||
timeToLive = 10 minutes)
|
||||
|
||||
|
|
|
@ -33,7 +33,9 @@ private[puzzle] final class PuzzleApi(
|
|||
|
||||
private def lastId: Fu[Int] = lila.db.Util findNextId puzzleColl map (_ - 1)
|
||||
|
||||
val cachedLastId = lila.memo.AsyncCache.single(lastId, timeToLive = 5 minutes)
|
||||
val cachedLastId = lila.memo.AsyncCache.single(
|
||||
name = "puzzle.lastId",
|
||||
lastId, timeToLive = 5 minutes)
|
||||
|
||||
def importOne(json: JsValue, token: String): Fu[PuzzleId] =
|
||||
if (token != apiToken) fufail("Invalid API token")
|
||||
|
|
|
@ -5,6 +5,7 @@ import scala.concurrent.duration._
|
|||
private[simul] final class Cached(repo: SimulRepo) {
|
||||
|
||||
private val nameCache = lila.memo.MixedCache[Simul.ID, Option[String]](
|
||||
name = "simul.name",
|
||||
((id: Simul.ID) => repo find id map2 { (simul: Simul) => simul.fullName }),
|
||||
timeToLive = 6 hours,
|
||||
default = _ => none,
|
||||
|
|
|
@ -90,9 +90,13 @@ final class Env(
|
|||
|
||||
def isHosting(userId: String): Fu[Boolean] = api.currentHostIds map (_ contains userId)
|
||||
|
||||
val allCreated = lila.memo.AsyncCache.single(repo.allCreated, timeToLive = CreatedCacheTtl)
|
||||
val allCreated = lila.memo.AsyncCache.single(
|
||||
name = "simul.allCreated",
|
||||
repo.allCreated, timeToLive = CreatedCacheTtl)
|
||||
|
||||
val allCreatedFeaturable = lila.memo.AsyncCache.single(repo.allCreatedFeaturable, timeToLive = CreatedCacheTtl)
|
||||
val allCreatedFeaturable = lila.memo.AsyncCache.single(
|
||||
name = "simul.allCreatedFeaturable",
|
||||
repo.allCreatedFeaturable, timeToLive = CreatedCacheTtl)
|
||||
|
||||
def version(tourId: String): Fu[Int] =
|
||||
socketHub ? Ask(tourId, GetVersion) mapTo manifest[Int]
|
||||
|
|
|
@ -33,6 +33,7 @@ private[simul] final class SimulApi(
|
|||
def currentHostIds: Fu[Set[String]] = currentHostIdsCache apply true
|
||||
|
||||
private val currentHostIdsCache = AsyncCache.single[Set[String]](
|
||||
name = "simul.currentHostIds",
|
||||
f = repo.allStarted map (_ map (_.hostId) toSet),
|
||||
timeToLive = 10 minutes)
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import scala.concurrent.duration._
|
|||
private[team] final class Cached {
|
||||
|
||||
private val nameCache = MixedCache[String, Option[String]](
|
||||
name = "team.name",
|
||||
TeamRepo.name,
|
||||
timeToLive = 6 hours,
|
||||
default = _ => none,
|
||||
|
@ -14,6 +15,7 @@ private[team] final class Cached {
|
|||
def name(id: String) = nameCache get id
|
||||
|
||||
private[team] val teamIdsCache = MixedCache[String, Set[String]](
|
||||
name = "team.ids",
|
||||
MemberRepo.teamIdsByUser,
|
||||
timeToLive = 2 hours,
|
||||
default = _ => Set.empty,
|
||||
|
|
|
@ -9,6 +9,7 @@ private[tournament] final class Cached(
|
|||
rankingTtl: FiniteDuration) {
|
||||
|
||||
private val nameCache = MixedCache[String, Option[String]](
|
||||
name = "tournament.name",
|
||||
((id: String) => TournamentRepo byId id map2 { (tour: Tournament) => tour.fullName }),
|
||||
timeToLive = 6 hours,
|
||||
default = _ => none,
|
||||
|
@ -17,6 +18,7 @@ private[tournament] final class Cached(
|
|||
def name(id: String): Option[String] = nameCache get id
|
||||
|
||||
val promotable = AsyncCache.single(
|
||||
name = "tournament.promotable",
|
||||
TournamentRepo.promotable,
|
||||
timeToLive = createdTtl)
|
||||
|
||||
|
|
|
@ -51,6 +51,7 @@ final class Env(
|
|||
|
||||
object isStreamer {
|
||||
private val cache = lila.memo.MixedCache.single[Set[String]](
|
||||
name = "tv.streamers",
|
||||
f = streamerList.lichessIds,
|
||||
timeToLive = 10 seconds,
|
||||
default = Set.empty,
|
||||
|
@ -60,6 +61,7 @@ final class Env(
|
|||
|
||||
object streamsOnAir {
|
||||
private val cache = lila.memo.AsyncCache.single[List[StreamOnAir]](
|
||||
name = "tv.streamsOnAir",
|
||||
f = streaming.onAir,
|
||||
timeToLive = 2 seconds)
|
||||
def all = cache(true)
|
||||
|
|
|
@ -85,6 +85,7 @@ final class Cached(
|
|||
keyToString = _.toString)
|
||||
|
||||
val top50Online = lila.memo.AsyncCache.single[List[User]](
|
||||
name = "user.top50online",
|
||||
f = UserRepo.byIdsSortRating(onlineUserIdMemo.keys, 50),
|
||||
timeToLive = 10 seconds)
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@ final class LightUserApi(coll: Coll) {
|
|||
}
|
||||
|
||||
private val cache = lila.memo.MixedCache[String, Option[LightUser]](
|
||||
name = "user.light",
|
||||
id => coll.find(
|
||||
$id(id),
|
||||
$doc(F.username -> true, F.title -> true, s"${F.plan}.active" -> true)
|
||||
|
|
|
@ -129,6 +129,7 @@ private[video] final class VideoApi(
|
|||
object count {
|
||||
|
||||
private val cache = AsyncCache.single(
|
||||
name = "video.count",
|
||||
f = videoColl.count(none),
|
||||
timeToLive = 1.day)
|
||||
|
||||
|
@ -206,6 +207,7 @@ private[video] final class VideoApi(
|
|||
maxCapacity = 100)
|
||||
|
||||
private val popularCache = AsyncCache.single[List[TagNb]](
|
||||
name = "video.popular",
|
||||
f = videoColl.aggregate(
|
||||
Project($doc("tags" -> true)), List(
|
||||
UnwindField("tags"), GroupField("tags")("nb" -> SumValue(1)),
|
||||
|
|
Loading…
Reference in New Issue