adapt all mongo caches
This commit is contained in:
parent
8c471ffd30
commit
730254cd98
|
@ -372,7 +372,6 @@ timeline {
|
|||
}
|
||||
}
|
||||
game {
|
||||
cached.nb.ttl = 1 hour
|
||||
paginator.max_per_page = 12
|
||||
collection {
|
||||
game = game5
|
||||
|
|
|
@ -8,11 +8,10 @@ import lila.user.User
|
|||
|
||||
final class Cached(
|
||||
coll: Coll,
|
||||
mongoCache: MongoCache.Builder,
|
||||
defaultTtl: FiniteDuration) {
|
||||
mongoCache: MongoCache.Builder) {
|
||||
|
||||
def nbImportedBy(userId: String): Fu[Int] = countCache(Query imported userId)
|
||||
def clearNbImportedByCache(userId: String) = countCache.remove(Query imported userId)
|
||||
def nbImportedBy(userId: String): Fu[Int] = nbImportedCache(userId)
|
||||
def clearNbImportedByCache = nbImportedCache remove _
|
||||
|
||||
def nbPlaying(userId: String): Fu[Int] = countShortTtl(Query nowPlaying userId)
|
||||
|
||||
|
@ -26,12 +25,19 @@ final class Cached(
|
|||
|
||||
private val countShortTtl = AsyncCache[Bdoc, Int](
|
||||
name = "game.countShortTtl",
|
||||
f = (o: Bdoc) => coll countSel o,
|
||||
f = coll.countSel,
|
||||
timeToLive = 5.seconds)
|
||||
|
||||
private val countCache = mongoCache(
|
||||
private val nbImportedCache = mongoCache[User.ID, Int](
|
||||
prefix = "game:imported",
|
||||
f = userId => coll countSel Query.imported(userId),
|
||||
timeToLive = 1 hour,
|
||||
timeToLiveMongo = 30.days.some,
|
||||
keyToString = identity)
|
||||
|
||||
private val countCache = mongoCache[Bdoc, Int](
|
||||
prefix = "game:count",
|
||||
f = (o: Bdoc) => coll countSel o,
|
||||
timeToLive = defaultTtl,
|
||||
f = coll.countSel,
|
||||
timeToLive = 1 hour,
|
||||
keyToString = lila.db.BSON.hashDoc)
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@ final class Env(
|
|||
scheduler: lila.common.Scheduler) {
|
||||
|
||||
private val settings = new {
|
||||
val CachedNbTtl = config duration "cached.nb.ttl"
|
||||
val PaginatorMaxPerPage = config getInt "paginator.max_per_page"
|
||||
val CaptcherName = config getString "captcher.name"
|
||||
val CaptcherDuration = config duration "captcher.duration"
|
||||
|
@ -40,8 +39,7 @@ final class Env(
|
|||
|
||||
lazy val cached = new Cached(
|
||||
coll = gameColl,
|
||||
mongoCache = mongoCache,
|
||||
defaultTtl = CachedNbTtl)
|
||||
mongoCache = mongoCache)
|
||||
|
||||
lazy val paginator = new PaginatorBuilder(
|
||||
coll = gameColl,
|
||||
|
|
|
@ -23,8 +23,8 @@ final class RatingChartApi(
|
|||
|
||||
private val cache = mongoCache[User, String](
|
||||
prefix = "history:rating",
|
||||
f = (user: User) => build(user) map (~_),
|
||||
maxCapacity = 64,
|
||||
f = user => build(user) map (~_),
|
||||
maxCapacity = 1024,
|
||||
timeToLive = cacheTtl,
|
||||
keyToString = _.id)
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ final class Env(config: Config, db: lila.db.Env) {
|
|||
private val CollectionCache = config getString "collection.cache"
|
||||
private val CollectionConfig = config getString "collection.config"
|
||||
|
||||
lazy val mongoCache: MongoCache.Builder = MongoCache(db(CollectionCache))
|
||||
lazy val mongoCache: MongoCache.Builder = new MongoCache.Builder(db(CollectionCache))
|
||||
|
||||
lazy val configStore: ConfigStore.Builder = ConfigStore(db(CollectionConfig))
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ final class MongoCache[K, V: MongoCache.Handler] private (
|
|||
keyToString: K => String) {
|
||||
|
||||
def apply(k: K): Fu[V] = cache.get(k, k =>
|
||||
coll.find(select(k)).uno[Entry] flatMap {
|
||||
coll.find($id(makeKey(k))).uno[Entry] flatMap {
|
||||
case None => f(k) flatMap { v =>
|
||||
persist(k, v) inject v
|
||||
}
|
||||
|
@ -37,12 +37,16 @@ final class MongoCache[K, V: MongoCache.Handler] private (
|
|||
|
||||
private implicit val entryBSONHandler = Macros.handler[Entry]
|
||||
|
||||
private def persist(k: K, v: V): Funit =
|
||||
coll.insert(Entry(makeKey(k), v, mongoExpiresAt())).void recover lila.db.recoverDuplicateKey(_ => ())
|
||||
|
||||
private def makeKey(k: K) = s"$prefix:${keyToString(k)}"
|
||||
|
||||
private def select(k: K) = $id(makeKey(k))
|
||||
private def persist(k: K, v: V): Funit = {
|
||||
val mongoKey = makeKey(k)
|
||||
coll.update(
|
||||
$id(mongoKey),
|
||||
Entry(mongoKey, v, mongoExpiresAt()),
|
||||
upsert = true
|
||||
).void
|
||||
}
|
||||
}
|
||||
|
||||
object MongoCache {
|
||||
|
@ -64,13 +68,14 @@ object MongoCache {
|
|||
f: K => Fu[V],
|
||||
maxCapacity: Int = 1024,
|
||||
timeToLive: FiniteDuration,
|
||||
timeToLiveMongo: Option[FiniteDuration] = None,
|
||||
keyToString: K => String): MongoCache[K, V] = new MongoCache[K, V](
|
||||
prefix = prefix,
|
||||
cache = Scaffeine()
|
||||
.expireAfterWrite(timeToLive)
|
||||
.maximumSize(maxCapacity)
|
||||
.build[K, Fu[V]],
|
||||
mongoExpiresAt = mongoExpiresAt(timeToLive),
|
||||
mongoExpiresAt = mongoExpiresAt(timeToLiveMongo | timeToLive),
|
||||
coll = coll,
|
||||
f = f,
|
||||
keyToString = keyToString)
|
||||
|
@ -89,6 +94,4 @@ object MongoCache {
|
|||
f = _ => f,
|
||||
keyToString = _.toString)
|
||||
}
|
||||
|
||||
def apply(coll: Coll) = new Builder(coll)
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ import com.typesafe.config.Config
|
|||
final class Env(
|
||||
config: Config,
|
||||
db: lila.db.Env,
|
||||
mongoCache: lila.memo.MongoCache.Builder,
|
||||
shutup: ActorSelection,
|
||||
notifyApi: lila.notify.NotifyApi,
|
||||
blocks: (String, String) => Fu[Boolean],
|
||||
|
@ -50,7 +49,6 @@ object Env {
|
|||
config = lila.common.PlayApp loadConfig "message",
|
||||
db = lila.db.Env.current,
|
||||
shutup = lila.hub.Env.current.actor.shutup,
|
||||
mongoCache = lila.memo.Env.current.mongoCache,
|
||||
notifyApi = lila.notify.Env.current.api,
|
||||
blocks = lila.relation.Env.current.api.fetchBlocks,
|
||||
follows = lila.relation.Env.current.api.fetchFollows,
|
||||
|
|
|
@ -88,7 +88,7 @@ final class QaApi(
|
|||
f = nb => questionColl.find($empty)
|
||||
.sort($doc("vote.score" -> -1))
|
||||
.cursor[Question]().gather[List](nb),
|
||||
timeToLive = 3 hour,
|
||||
timeToLive = 6 hour,
|
||||
keyToString = _.toString)
|
||||
|
||||
def popular(max: Int): Fu[List[Question]] = popularCache(max)
|
||||
|
|
|
@ -17,7 +17,6 @@ final class Env(
|
|||
system: ActorSystem,
|
||||
scheduler: lila.common.Scheduler,
|
||||
db: lila.db.Env,
|
||||
mongoCache: lila.memo.MongoCache.Builder,
|
||||
hub: lila.hub.Env,
|
||||
lightUser: lila.common.LightUser.Getter,
|
||||
onGameStart: String => Unit,
|
||||
|
@ -119,7 +118,6 @@ object Env {
|
|||
system = lila.common.PlayApp.system,
|
||||
scheduler = lila.common.PlayApp.scheduler,
|
||||
db = lila.db.Env.current,
|
||||
mongoCache = lila.memo.Env.current.mongoCache,
|
||||
hub = lila.hub.Env.current,
|
||||
lightUser = lila.user.Env.current.lightUser,
|
||||
onGameStart = lila.game.Env.current.onStart,
|
||||
|
|
|
@ -18,7 +18,8 @@ final class TournamentStatsApi(mongoCache: lila.memo.MongoCache.Builder) {
|
|||
prefix = "tournament:stats",
|
||||
keyToString = identity,
|
||||
f = fetch,
|
||||
timeToLive = 10 minutes)
|
||||
timeToLive = 10 minutes,
|
||||
timeToLiveMongo = 90.days.some)
|
||||
|
||||
private def fetch(tournamentId: Tournament.ID): Fu[TournamentStats] = for {
|
||||
rating <- PlayerRepo.averageRating(tournamentId)
|
||||
|
|
|
@ -21,11 +21,6 @@ final class Cached(
|
|||
private def oneWeekAgo = DateTime.now minusWeeks 1
|
||||
private def oneMonthAgo = DateTime.now minusMonths 1
|
||||
|
||||
private val countCache = mongoCache.single[Int](
|
||||
prefix = "user:nb",
|
||||
f = userColl.count(UserRepo.enabledSelect.some),
|
||||
timeToLive = nbTtl)
|
||||
|
||||
private implicit val LightUserBSONHandler = Macros.handler[LightUser]
|
||||
private implicit val LightPerfBSONHandler = Macros.handler[LightPerf]
|
||||
private implicit val LightCountBSONHandler = Macros.handler[LightCount]
|
||||
|
|
Loading…
Reference in a new issue