forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cached.scala
61 lines (46 loc) · 1.79 KB
/
Cached.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package lila.game
import scala.concurrent.duration._
import org.joda.time.DateTime
import play.api.libs.json.JsObject
import lila.db.api.$count
import lila.db.BSON._
import lila.memo.{ AsyncCache, MongoCache, ExpireSetMemo, Builder }
import lila.user.{ User, UidNb }
import tube.gameTube
import UidNb.UidNbBSONHandler
final class Cached(
mongoCache: MongoCache.Builder,
defaultTtl: FiniteDuration) {
def nbImportedBy(userId: String): Fu[Int] = count(Query imported userId)
def clearNbImportedByCache(userId: String) = count.remove(Query imported userId)
def nbPlaying(userId: String): Fu[Int] = countShortTtl(Query nowPlaying userId)
private implicit val userHandler = User.userBSONHandler
val rematch960 = new ExpireSetMemo(3.hours)
val isRematch = new ExpireSetMemo(3.hours)
// very expensive
val activePlayerUidsDay = mongoCache[Int, List[UidNb]](
prefix = "player:active:day",
(nb: Int) => GameRepo.activePlayersSince(DateTime.now minusDays 1, nb),
timeToLive = 1 hour)
private val countShortTtl = AsyncCache[JsObject, Int](
f = (o: JsObject) => $count(o),
timeToLive = 5.seconds)
private val count = mongoCache(
prefix = "game:count",
f = (o: JsObject) => $count(o),
timeToLive = defaultTtl)
object Divider {
private val cache = Builder.size[String, chess.Division](5000)
def apply(game: Game, initialFen: Option[String]): chess.Division =
if (!Game.divisionSensiblevariants.contains(game.variant)) chess.Division.empty
else Option(cache getIfPresent game.id) | {
val div = chess.Replay.boards(
moveStrs = game.pgnMoves,
initialFen = initialFen,
variant = game.variant
).toOption.fold(chess.Division.empty)(chess.Divider.apply)
cache.put(game.id, div)
div
}
}
}