From e1f32fa38bdae618975b65149cb7eea1603b505d Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Wed, 18 Sep 2013 02:11:17 +0200 Subject: [PATCH] cache confrontation count --- modules/game/src/main/Cached.scala | 7 +++++++ modules/game/src/main/GameRepo.scala | 10 +++++----- modules/user/src/main/Confrontation.scala | 7 +------ todo | 2 +- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/modules/game/src/main/Cached.scala b/modules/game/src/main/Cached.scala index dbabb96eff..1bfb233d8e 100644 --- a/modules/game/src/main/Cached.scala +++ b/modules/game/src/main/Cached.scala @@ -6,6 +6,7 @@ import play.api.libs.json.JsObject import lila.db.api.$count import lila.memo.AsyncCache +import lila.user.{ User, Confrontation } import tube.gameTube private[game] final class Cached(ttl: Duration) { @@ -15,5 +16,11 @@ private[game] final class Cached(ttl: Duration) { def nbPopular: Fu[Int] = count(Query.popular) def nbImported: Fu[Int] = count(Query.imported) + def confrontation(user1: User, user2: User): Fu[Confrontation] = + confrontationCache(List(user1, user2).sortBy(_.count.game).map(_.id)) + + private val confrontationCache = + AsyncCache(GameRepo.confrontation, timeToLive = 1.minute) + private val count = AsyncCache((o: JsObject) ⇒ $count(o), timeToLive = ttl) } diff --git a/modules/game/src/main/GameRepo.scala b/modules/game/src/main/GameRepo.scala index d0bf8152de..2550a369a6 100644 --- a/modules/game/src/main/GameRepo.scala +++ b/modules/game/src/main/GameRepo.scala @@ -227,10 +227,11 @@ object GameRepo { ) // user1 wins, draws, losses - def confrontation(user1: User, user2: User): Fu[Confrontation] = { + // the 2 userIds SHOULD be sorted by game count desc + // this method is cached in lila.game.Cached + private[game] def confrontation(userIds: List[String]): Fu[Confrontation] = { import reactivemongo.bson._ import reactivemongo.core.commands._ - val userIds = List(user1, user2).sortBy(_.count.game).map(_.id) val command = Aggregate(gameTube.coll.name, Seq( Match(BSONDocument( "uids" -> BSONDocument("$all" -> userIds), @@ -247,10 +248,9 @@ object GameRepo { } }).flatten.toMap Confrontation( - user1, user2, - ~(res get user1.id), + ~(res get ~userIds.lift(0)), ~(res get ""), - ~(res get user2.id) + ~(res get ~userIds.lift(1)) ) } } diff --git a/modules/user/src/main/Confrontation.scala b/modules/user/src/main/Confrontation.scala index 6c991eb4f7..9701d126ac 100644 --- a/modules/user/src/main/Confrontation.scala +++ b/modules/user/src/main/Confrontation.scala @@ -1,11 +1,6 @@ package lila.user -case class Confrontation( - user1: User, - user2: User, - wins: Int, - draws: Int, - losses: Int) { +case class Confrontation(wins: Int, draws: Int, losses: Int) { def games = wins + draws + losses diff --git a/todo b/todo index 93bc774620..8f498c88c9 100644 --- a/todo +++ b/todo @@ -61,5 +61,5 @@ real board editor save clickable mistakes list in analysis FEN/editor castle/enpassant https://github.com/ornicar/lila/issues/42 white/black to play user notes, shared among friends -autopairing http://en.lichess.org/forum/general-chess-discussion/pairing#1 detect cheat using lichess AI +fix AI protocol