forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserLagCache.scala
More file actions
34 lines (27 loc) · 748 Bytes
/
UserLagCache.scala
File metadata and controls
34 lines (27 loc) · 748 Bytes
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
package lila.socket
import chess.Centis
import com.github.blemale.scaffeine.Cache
import lila.core.socket.userLag.*
final class UserLagCache(using Executor):
private val cache: Cache[UserId, Centis] = lila.memo.CacheApi.scaffeineNoScheduler
.expireAfterWrite(15.minutes)
.build[UserId, Centis]()
val put: Put = (userId, lag) =>
if lag.centis >= 0
then
cache.put(
userId,
cache
.getIfPresent(userId)
.fold(lag):
_.avg(lag)
)
export cache.getIfPresent as get
val getLagRating: GetLagRating = userId =>
Centis
.raw(get(userId))
.map: c =>
if c <= 15 then 4
else if c <= 30 then 3
else if c <= (50) then 2
else 1