cache visible & scheduled tournaments

This commit is contained in:
Thibault Duplessis 2017-04-07 09:39:15 +02:00
parent 95898b55c5
commit b8953ba1b7
2 changed files with 30 additions and 22 deletions

View file

@ -2,12 +2,13 @@ package controllers
import play.api.libs.json._ import play.api.libs.json._
import play.api.mvc._ import play.api.mvc._
import scala.concurrent.duration._
import lila.api.Context import lila.api.Context
import lila.app._ import lila.app._
import lila.common.HTTPRequest import lila.common.HTTPRequest
import lila.game.{ Pov, GameRepo } import lila.game.{ Pov, GameRepo }
import lila.tournament.{ System, TournamentRepo, PairingRepo } import lila.tournament.{ System, TournamentRepo, PairingRepo, VisibleTournaments, Tournament => Tour }
import views._ import views._
object Tournament extends LilaController { object Tournament extends LilaController {
@ -17,6 +18,15 @@ object Tournament extends LilaController {
private def tournamentNotFound(implicit ctx: Context) = NotFound(html.tournament.notFound()) private def tournamentNotFound(implicit ctx: Context) = NotFound(html.tournament.notFound())
private[controllers] val upcomingCache = Env.memo.asyncCache.single[(VisibleTournaments, List[Tour])](
name = "tournament.home",
for {
visible <- env.api.fetchVisibleTournaments
scheduled <- repo.scheduledDedup
} yield (visible, scheduled),
expireAfter = _.ExpireAfterWrite(3 seconds)
)
def home(page: Int) = Open { implicit ctx => def home(page: Int) = Open { implicit ctx =>
negotiate( negotiate(
html = Reasonable(page, 20) { html = Reasonable(page, 20) {
@ -26,8 +36,7 @@ object Tournament extends LilaController {
_ <- Env.user.lightUserApi preloadMany pag.currentPageResults.flatMap(_.winnerId) _ <- Env.user.lightUserApi preloadMany pag.currentPageResults.flatMap(_.winnerId)
} yield Ok(html.tournament.finishedPaginator(pag)) } yield Ok(html.tournament.finishedPaginator(pag))
else for { else for {
visible <- env.api.fetchVisibleTournaments (visible, scheduled) <- upcomingCache.get
scheduled <- repo.scheduledDedup
finished <- finishedPaginator finished <- finishedPaginator
winners <- env.winners.all winners <- env.winners.all
_ <- Env.user.lightUserApi preloadMany { _ <- Env.user.lightUserApi preloadMany {
@ -40,7 +49,7 @@ object Tournament extends LilaController {
} }
}, },
api = _ => for { api = _ => for {
visible <- env.api.fetchVisibleTournaments (visible, _) <- upcomingCache.get
scheduleJson <- env scheduleJsonView visible scheduleJson <- env scheduleJsonView visible
} yield Ok(scheduleJson) } yield Ok(scheduleJson)
) )
@ -197,11 +206,11 @@ object Tournament extends LilaController {
} }
def limitedInvitation = Auth { implicit ctx => me => def limitedInvitation = Auth { implicit ctx => me =>
env.api.fetchVisibleTournaments.flatMap { tours => for {
lila.tournament.TournamentInviter.findNextFor(me, tours, env.verify.canEnter(me)) (tours, _) <- upcomingCache.get
} map { res <- lila.tournament.TournamentInviter.findNextFor(me, tours, env.verify.canEnter(me))
case None => Redirect(routes.Tournament.home(1)) } yield res.fold(Redirect(routes.Tournament.home(1))) { t =>
case Some(t) => Redirect(routes.Tournament.show(t.id)) Redirect(routes.Tournament.show(t.id))
} }
} }

View file

@ -75,7 +75,7 @@ object TournamentRepo {
coll.find(startedSelect).sort($doc("createdAt" -> -1)).list[Tournament](None) coll.find(startedSelect).sort($doc("createdAt" -> -1)).list[Tournament](None)
def publicStarted: Fu[List[Tournament]] = def publicStarted: Fu[List[Tournament]] =
coll.find(startedSelect ++ $doc("private" -> $doc("$exists" -> false))) coll.find(startedSelect ++ $doc("private" $exists false))
.sort($doc("createdAt" -> -1)) .sort($doc("createdAt" -> -1))
.list[Tournament]() .list[Tournament]()
@ -87,7 +87,7 @@ object TournamentRepo {
def finishedNotable(limit: Int): Fu[List[Tournament]] = def finishedNotable(limit: Int): Fu[List[Tournament]] =
coll.find(finishedSelect ++ $doc( coll.find(finishedSelect ++ $doc(
"$or" -> $arr( "$or" -> $arr(
$doc("nbPlayers" -> $doc("$gte" -> 15)), $doc("nbPlayers" $gte 15),
scheduledSelect scheduledSelect
) )
)) ))
@ -110,44 +110,43 @@ object TournamentRepo {
def setStatus(tourId: String, status: Status) = coll.update( def setStatus(tourId: String, status: Status) = coll.update(
$id(tourId), $id(tourId),
$doc("$set" -> $doc("status" -> status.id)) $set("status" -> status.id)
).void ).void
def setNbPlayers(tourId: String, nb: Int) = coll.update( def setNbPlayers(tourId: String, nb: Int) = coll.update(
$id(tourId), $id(tourId),
$doc("$set" -> $doc("nbPlayers" -> nb)) $set("nbPlayers" -> nb)
).void ).void
def setWinnerId(tourId: String, userId: String) = coll.update( def setWinnerId(tourId: String, userId: String) = coll.update(
$id(tourId), $id(tourId),
$doc("$set" -> $doc("winner" -> userId)) $set("winner" -> userId)
).void ).void
def setFeaturedGameId(tourId: String, gameId: String) = coll.update( def setFeaturedGameId(tourId: String, gameId: String) = coll.update(
$id(tourId), $id(tourId),
$doc("$set" -> $doc("featured" -> gameId)) $set("featured" -> gameId)
).void ).void
def featuredGameId(tourId: String) = coll.primitiveOne[String]($id(tourId), "featured") def featuredGameId(tourId: String) = coll.primitiveOne[String]($id(tourId), "featured")
private def allCreatedSelect(aheadMinutes: Int) = createdSelect ++ $doc( private def allCreatedSelect(aheadMinutes: Int) = createdSelect ++ $doc(
"$or" -> $arr( "$or" -> $arr(
$doc("schedule" -> $doc("$exists" -> false)), $doc("schedule" $exists false),
$doc("startsAt" -> $doc("$lt" -> (DateTime.now plusMinutes aheadMinutes))) $doc("startsAt" $lt (DateTime.now plusMinutes aheadMinutes))
) )
) )
def publicCreatedSorted(aheadMinutes: Int): Fu[List[Tournament]] = coll.find( def publicCreatedSorted(aheadMinutes: Int): Fu[List[Tournament]] = coll.find(
allCreatedSelect(aheadMinutes) ++ $doc("private" -> $doc("$exists" -> false)) allCreatedSelect(aheadMinutes) ++ $doc("private" $exists false)
).sort($doc("startsAt" -> 1)).list[Tournament](none) ).sort($doc("startsAt" -> 1)).list[Tournament](none)
def allCreated(aheadMinutes: Int): Fu[List[Tournament]] = def allCreated(aheadMinutes: Int): Fu[List[Tournament]] =
coll.find(allCreatedSelect(aheadMinutes)).cursor[Tournament]().gather[List]() coll.find(allCreatedSelect(aheadMinutes)).cursor[Tournament]().gather[List]()
private def stillWorthEntering: Fu[List[Tournament]] = private def stillWorthEntering: Fu[List[Tournament]] = coll.find(
coll.find(startedSelect ++ $doc( startedSelect ++ $doc("private" $exists false)
"private" -> $doc("$exists" -> false) ).sort($doc("startsAt" -> 1)).list[Tournament](none) map {
)).sort($doc("startsAt" -> 1)).list[Tournament](none) map {
_.filter(_.isStillWorthEntering) _.filter(_.isStillWorthEntering)
} }