cache current game ID per user

This commit is contained in:
Thibault Duplessis 2020-04-15 10:59:36 -06:00
parent d7b67d0cec
commit 1d9e6d92d9
2 changed files with 17 additions and 5 deletions

View file

@ -179,9 +179,9 @@ final class User(
}
private def currentlyPlaying(user: UserModel): Fu[Option[Pov]] =
env.game.gameRepo
.lastPlayedPlayingId(user.id)
.flatMap(_ ?? { env.round.proxyRepo.pov(_, user) })
env.game.cached.currentlyPlaying(user.id) flatMap {
_ ?? { env.round.proxyRepo.pov(_, user) }
}
private def lastPlayed(user: UserModel): Fu[Option[Pov]] =
env.game.gameRepo

View file

@ -1,14 +1,15 @@
package lila.game
import com.github.blemale.scaffeine.LoadingCache
import scala.concurrent.duration._
import lila.db.dsl._
import lila.memo.MongoCache
import lila.memo.{ CacheApi, MongoCache }
import lila.user.User
final class Cached(
gameRepo: GameRepo,
cacheApi: lila.memo.CacheApi,
cacheApi: CacheApi,
mongoCache: MongoCache.Api
)(implicit ec: scala.concurrent.ExecutionContext) {
@ -19,6 +20,17 @@ final class Cached(
def nbPlaying = nbPlayingCache.get _
def lastPlayedPlayingId(userId: User.ID): Fu[Option[Game.ID]] = lastPlayedPlayingIdCache get userId
private val lastPlayedPlayingIdCache: LoadingCache[User.ID, Fu[Option[Game.ID]]] =
CacheApi.scaffeineNoScheduler
.expireAfterWrite(5 seconds)
.build[User.ID, Fu[Option[Game.ID]]](userId => gameRepo.lastPlayedPlayingId(userId))
lila.common.Bus.subscribeFun("startGame") {
case lila.game.actorApi.StartGame(game) => game.userIds foreach { lastPlayedPlayingIdCache.invalidate(_) }
}
private val nbPlayingCache = cacheApi[User.ID, Int](256, "game.nbPlaying") {
_.expireAfterWrite(15 seconds)
.buildAsyncFuture { userId =>