cache user team IDs as a single string

pull/2642/head
Thibault Duplessis 2017-02-05 12:11:03 +01:00
parent f26e8620bd
commit a5a9092d5e
7 changed files with 32 additions and 10 deletions

View File

@ -40,7 +40,7 @@ final class Env(
getRanks = Env.user.cached.ranking.getAll,
isHostingSimul = Env.simul.isHosting,
fetchIsStreamer = Env.tv.isStreamer.apply,
fetchTeamIds = Env.team.api.teamIds,
fetchTeamIds = Env.team.api.teamIdsSet,
fetchIsCoach = Env.coach.api.isListedCoach,
insightShare = Env.insight.share,
getPlayTime = Env.game.playTime.apply,

View File

@ -8,7 +8,7 @@ object ForumCateg extends LilaController with ForumController {
def index = Open { implicit ctx =>
NotForKids {
for {
teamIds <- ctx.userId ?? teamCache.teamIds
teamIds <- ctx.userId ?? teamCache.teamIdsSet
categs <- categApi.list(teamIds, ctx.troll)
_ <- Env.user.lightUserApi preloadMany categs.flatMap(_.lastPostUserId)
} yield html.forum.categ.index(categs)

View File

@ -24,7 +24,7 @@ object ForumPost extends LilaController with ForumController {
def recent = Open { implicit ctx =>
NotForKids {
Env.forum.recent(ctx.me, teamCache.teamIds) map { posts =>
Env.forum.recent(ctx.me, teamCache.teamIdsSet) map { posts =>
html.forum.post.recent(posts)
}
}

View File

@ -70,7 +70,7 @@ object Lobby extends LilaController {
expireAfter = _.ExpireAfterWrite(1 second))
private def renderCtx(implicit ctx: Context): Fu[Html] = Env.current.preloader(
posts = Env.forum.recent(ctx.me, Env.team.cached.teamIds).nevermind,
posts = Env.forum.recent(ctx.me, Env.team.cached.teamIdsSet).nevermind,
tours = Env.tournament.cached.promotable.get.nevermind,
events = Env.event.api.promotable.get.nevermind,
simuls = Env.simul.allCreatedFeaturable.get.nevermind

View File

@ -17,16 +17,18 @@ private[team] final class Cached(
def name(id: String) = nameCache sync id
private val teamIdsCache = new Syncache[String, Set[String]](
// ~ 30k entries as of 04/02/17
private val teamIdsCache = new Syncache[lila.user.User.ID, Team.IdsStr](
name = "team.ids",
compute = MemberRepo.teamIdsByUser,
default = _ => Set.empty,
compute = u => MemberRepo.teamIdsByUser(u).dmap(Team.IdsStr.apply),
default = _ => Team.IdsStr.empty,
strategy = Syncache.WaitAfterUptime(20 millis),
expireAfter = Syncache.ExpireAfterAccess(1 hour),
logger = logger)
def syncTeamIds = teamIdsCache sync _
def teamIds = teamIdsCache async _
def teamIdsSet(userId: lila.user.User.ID) = teamIdsCache async userId dmap (_.toSet)
def invalidateTeamIds = teamIdsCache invalidate _

View File

@ -29,6 +29,26 @@ object Team {
type ID = String
case class IdsStr(value: String) extends AnyVal {
def contains(teamId: ID) = value contains teamId
def toArray: Array[ID] = value.split(IdsStr.separator)
def toList = toArray.toList
def toSet = toArray.toSet
}
object IdsStr {
private val separator = ' '
val empty = IdsStr("")
def apply(ids: Iterable[ID]): IdsStr = IdsStr(ids mkString separator.toString)
}
def make(
name: String,
location: Option[String],

View File

@ -54,9 +54,9 @@ final class TeamApi(
}
def mine(me: User): Fu[List[Team]] =
cached teamIds me.id flatMap coll.team.byIds[Team]
cached teamIds me.id dmap (_.toList) flatMap coll.team.byIds[Team]
def hasTeams(me: User): Fu[Boolean] = cached.teamIds(me.id).map(_.nonEmpty)
def hasTeams(me: User): Fu[Boolean] = cached.teamIds(me.id).map(_.value.nonEmpty)
def hasCreatedRecently(me: User): Fu[Boolean] =
TeamRepo.userHasCreatedSince(me.id, creationPeriod)
@ -163,7 +163,7 @@ final class TeamApi(
def owns(teamId: String, userId: String): Fu[Boolean] =
TeamRepo ownerOf teamId map (Some(userId) ==)
def teamIds(userId: String) = cached teamIds userId
def teamIdsSet(userId: String) = cached teamIdsSet userId
def teamName(teamId: String) = cached name teamId