Skip to content

Commit

Permalink
optimize and cache simul count - also remove dangling debug
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jun 12, 2023
1 parent 562dade commit 00c5b58
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/mashup/UserInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ object UserInfo:
postApi.nbByUser(user.id).mon(_.user segment "nbForumPosts") zip
(withUblog ?? ublogApi.userBlogPreviewFor(user, 3, ctx.me)) zip
studyRepo.countByOwner(user.id).recoverDefault.mon(_.user segment "nbStudies") zip
simulApi.nbHostedByUser(user.id).mon(_.user segment "nbSimuls").thenPp zip
simulApi.countHostedByUser.get(user.id).mon(_.user segment "nbSimuls") zip
userApi.getTrophiesAndAwards(user).mon(_.user segment "trophies") zip
teamApi.joinedTeamIdsOfUserAsSeenBy(user, ctx.me).mon(_.user segment "teamIds") zip
coachApi.isListedCoach(user).mon(_.user segment "coach") zip
Expand Down
5 changes: 4 additions & 1 deletion modules/simul/src/main/SimulApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ final class SimulApi(
maxPerPage = MaxPerPage(20)
)

def nbHostedByUser(userId: UserId): Fu[Int] = repo.byHostAdapter(userId).nbResults
object countHostedByUser:
private val cache = cacheApi[UserId, Int](1024, "simul.nb.hosted"):
_.expireAfterWrite(10 minutes).buildAsyncFuture(repo.countByHost)
export cache.get

private def makeGame(simul: Simul, host: User)(
pairing: SimulPairing,
Expand Down
4 changes: 3 additions & 1 deletion modules/simul/src/main/SimulRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ final private[simul] class SimulRepo(val coll: Coll)(using Executor):
.listAll()

def byHostAdapter(hostId: UserId) =
new lila.db.paginator.Adapter[Simul](
lila.db.paginator.Adapter[Simul](
collection = coll,
selector = finishedSelect ++ $doc("hostId" -> hostId),
projection = none,
Expand All @@ -84,6 +84,8 @@ final private[simul] class SimulRepo(val coll: Coll)(using Executor):
def hostId(id: SimulId): Fu[Option[UserId]] =
coll.primitiveOne[UserId]($id(id), "hostId")

def countByHost(hostId: UserId) = coll.countSel($doc("hostId" -> hostId))

private val featurableSelect = $doc("featurable" -> true)

def allCreatedFeaturable: Fu[List[Simul]] =
Expand Down

0 comments on commit 00c5b58

Please sign in to comment.