preload homepage light users

This commit is contained in:
Thibault Duplessis 2017-01-25 13:08:56 +01:00
parent 282e0dc5f0
commit b00e1078e2
6 changed files with 71 additions and 33 deletions

View file

@ -26,7 +26,7 @@ final class Env(
countRounds = Env.round.count,
lobbyApi = Env.api.lobbyApi,
getPlayban = Env.playban.api.currentBan _,
lightUser = Env.user.lightUserSync)
lightUserApi = Env.user.lightUserApi)
lazy val userInfo = mashup.UserInfo(
bookmarkApi = Env.bookmark.api,

View file

@ -2,7 +2,6 @@ package lila.app
package mashup
import lila.api.Context
import lila.common.LightUser
import lila.event.Event
import lila.forum.MiniForumPost
import lila.game.{ Game, Pov, GameRepo }
@ -11,6 +10,7 @@ import lila.simul.Simul
import lila.timeline.Entry
import lila.tournament.{ Tournament, Winner }
import lila.tv.{ Tv, StreamOnAir }
import lila.user.LightUserApi
import lila.user.User
import play.api.libs.json._
@ -24,7 +24,7 @@ final class Preload(
countRounds: () => Int,
lobbyApi: lila.api.LobbyApi,
getPlayban: String => Fu[Option[TempBan]],
lightUser: LightUser.GetterSync) {
lightUserApi: LightUserApi) {
private type Response = (JsObject, List[Entry], List[MiniForumPost], List[Tournament], List[Event], List[Simul], Option[Game], List[User.LightPerf], List[Winner], Option[lila.puzzle.DailyPuzzle], List[StreamOnAir], List[lila.blog.MiniPost], Option[TempBan], Option[Preload.CurrentGame], Int)
@ -45,9 +45,14 @@ final class Preload(
dailyPuzzle() zip
streamsOnAir() zip
(ctx.userId ?? getPlayban) zip
(ctx.me ?? Preload.currentGame(lightUser)) map {
(ctx.me ?? Preload.currentGame(lightUserApi.sync)) flatMap {
case (((((((((((((data, posts), tours), events), simuls), feat), entries), lead), tWinners), puzzle), streams), playban), currentGame)) =>
(data, entries, posts, tours, events, simuls, feat, lead, tWinners, puzzle, streams, Env.blog.lastPostCache.apply, playban, currentGame, countRounds())
lightUserApi.preloadMany {
tWinners.map(_.userId) :::
posts.flatMap(_.userId) :::
entries.flatMap(_.userIds)
} inject
(data, entries, posts, tours, events, simuls, feat, lead, tWinners, puzzle, streams, Env.blog.lastPostCache.apply, playban, currentGame, countRounds())
}
}
@ -55,7 +60,7 @@ object Preload {
case class CurrentGame(pov: Pov, json: JsObject, opponent: String)
def currentGame(lightUser: LightUser.GetterSync)(user: User) =
def currentGame(lightUser: lila.common.LightUser.GetterSync)(user: User) =
GameRepo.urgentGames(user) map { povs =>
povs.find { p =>
p.game.nonAi && p.game.hasClock && p.isMyTurn

View file

@ -90,22 +90,54 @@ case class Victory(msg: String) extends Event
package timeline {
case class ReloadTimeline(user: String)
sealed abstract class Atom(val channel: String, val okForKid: Boolean)
case class Follow(u1: String, u2: String) extends Atom("follow", true)
case class TeamJoin(userId: String, teamId: String) extends Atom("teamJoin", false)
case class TeamCreate(userId: String, teamId: String) extends Atom("teamCreate", false)
case class ForumPost(userId: String, topicId: Option[String], topicName: String, postId: String) extends Atom(s"forum:${~topicId}", false)
case class NoteCreate(from: String, to: String) extends Atom("note", false)
case class TourJoin(userId: String, tourId: String, tourName: String) extends Atom("tournament", true)
case class QaQuestion(userId: String, id: Int, title: String) extends Atom("qa", true)
case class QaAnswer(userId: String, id: Int, title: String, answerId: Int) extends Atom("qa", true)
case class QaComment(userId: String, id: Int, title: String, commentId: String) extends Atom("qa", true)
case class GameEnd(playerId: String, opponent: Option[String], win: Option[Boolean], perf: String) extends Atom("gameEnd", true)
case class SimulCreate(userId: String, simulId: String, simulName: String) extends Atom("simulCreate", true)
case class SimulJoin(userId: String, simulId: String, simulName: String) extends Atom("simulJoin", true)
case class StudyCreate(userId: String, studyId: String, studyName: String) extends Atom("studyCreate", true)
case class StudyLike(userId: String, studyId: String, studyName: String) extends Atom("studyLike", true)
case class PlanStart(userId: String) extends Atom("planStart", true)
sealed abstract class Atom(val channel: String, val okForKid: Boolean) {
def userIds: List[String]
}
case class Follow(u1: String, u2: String) extends Atom("follow", true) {
def userIds = List(u1, u2)
}
case class TeamJoin(userId: String, teamId: String) extends Atom("teamJoin", false) {
def userIds = List(userId)
}
case class TeamCreate(userId: String, teamId: String) extends Atom("teamCreate", false) {
def userIds = List(userId)
}
case class ForumPost(userId: String, topicId: Option[String], topicName: String, postId: String) extends Atom(s"forum:${~topicId}", false) {
def userIds = List(userId)
}
case class NoteCreate(from: String, to: String) extends Atom("note", false) {
def userIds = List(from, to)
}
case class TourJoin(userId: String, tourId: String, tourName: String) extends Atom("tournament", true) {
def userIds = List(userId)
}
case class QaQuestion(userId: String, id: Int, title: String) extends Atom("qa", true) {
def userIds = List(userId)
}
case class QaAnswer(userId: String, id: Int, title: String, answerId: Int) extends Atom("qa", true) {
def userIds = List(userId)
}
case class QaComment(userId: String, id: Int, title: String, commentId: String) extends Atom("qa", true) {
def userIds = List(userId)
}
case class GameEnd(playerId: String, opponent: Option[String], win: Option[Boolean], perf: String) extends Atom("gameEnd", true) {
def userIds = opponent.toList
}
case class SimulCreate(userId: String, simulId: String, simulName: String) extends Atom("simulCreate", true) {
def userIds = List(userId)
}
case class SimulJoin(userId: String, simulId: String, simulName: String) extends Atom("simulJoin", true) {
def userIds = List(userId)
}
case class StudyCreate(userId: String, studyId: String, studyName: String) extends Atom("studyCreate", true) {
def userIds = List(userId)
}
case class StudyLike(userId: String, studyId: String, studyName: String) extends Atom("studyLike", true) {
def userIds = List(userId)
}
case class PlanStart(userId: String) extends Atom("planStart", true) {
def userIds = List(userId)
}
object propagation {
sealed trait Propagation
@ -153,7 +185,7 @@ case class AutoAnalyse(gameId: String)
}
package user {
case class Note(from: String, to: String, text: String, mod: Boolean)
case class Note(from: String, to: String, text: String, mod: Boolean)
}
package round {

View file

@ -49,15 +49,14 @@ final class Syncache[K, V](
def invalidate(k: K): Unit = cache invalidate k
// def preloadOne(k: K): Funit =
// if (cache.getIfPresent(k) == null) {
// println(s"*** preload $name $k")
// chm.computeIfAbsent(k, loadFunction)
// chm.get(k).void
// }
// else funit
def preloadOne(k: K): Funit =
if (cache.getIfPresent(k) == null) {
chm.computeIfAbsent(k, loadFunction)
chm.get(k).void
}
else funit
// def preloadMany(ks: List[K]): Funit = ks.map(async).sequenceFu.void
def preloadMany(ks: List[K]): Funit = ks.distinct.map(preloadOne).sequenceFu.void
private val loadFunction = new java.util.function.Function[K, Fu[V]] {
def apply(k: K) = {

View file

@ -44,6 +44,8 @@ case class Entry(
none
}
def userIds = decode.??(_.userIds)
def okForKid = decode ?? (_.okForKid)
}

View file

@ -15,8 +15,8 @@ final class LightUserApi(coll: Coll)(implicit system: akka.actor.ActorSystem) {
def invalidate = cache invalidate _
// def preloadOne = cache preloadOne _
// def preloadMany = cache preloadMany _
def preloadOne = cache preloadOne _
def preloadMany = cache preloadMany _
def getList(ids: List[String]): List[LightUser] = ids flatMap sync