Skip to content

Commit

Permalink
also cache unstarted lcc games
Browse files Browse the repository at this point in the history
some actually never start
  • Loading branch information
ornicar committed Jun 26, 2024
1 parent 2e998da commit 21b7b86
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions modules/relay/src/main/RelayFetch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,29 @@ final private class RelayFetch(
.flatMap(multiPgnToGames.future)

// cache finished games so they're not requested again for a while
// TODO also cache non-started games. Some actually never start.
private object lccCache:
import DgtJson.GameJson
type LccGameKey = String
private val finishedGames =
cacheApi.notLoadingSync[LccGameKey, GameJson](512, "relay.fetch.finishedLccGames"):
_.expireAfterWrite(5 minutes).build()
private val createdGames = // not started yet (possibly never)
cacheApi.notLoadingSync[LccGameKey, GameJson](512, "relay.fetch.createdLccGames"):
_.expireAfterWrite(1 minute).build()
def apply(lcc: RelayRound.Sync.Lcc, index: Int, roundTags: Tags)(
fetch: () => Fu[GameJson]
): Fu[GameJson] =
val key = s"${lcc.id} ${lcc.round} $index"
finishedGames.getIfPresent(key) match
case Some(game) => fuccess(game)
case None =>
fetch().addEffect: game =>
if game.mergeRoundTags(roundTags).outcome.isDefined then finishedGames.put(key, game)
createdGames.getIfPresent(key) match
case Some(game) => fuccess(game)
case None =>
fetch().addEffect: game =>
if game.moves.isEmpty then createdGames.put(key, game)
else if game.mergeRoundTags(roundTags).outcome.isDefined then finishedGames.put(key, game)

private def fetchFromUpstream(url: URL, max: Max)(using CanProxy): Fu[RelayGames] =
import DgtJson.*
Expand Down

0 comments on commit 21b7b86

Please sign in to comment.