Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose cache as a map of filtered seed #775

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,33 @@ abstract class CachedTool<Input, Output>(
return cache(CachedToolKey(input, seed)) { onCacheMissed(input) }
}

/**
* Exposes the cache as a [Map] of [Input] to [Output] filtered by instance [seed] and
* [timeCachePolicy]. Removes expired cache entries.
*/
suspend fun getCache(): Map<Input, Output> {
val lastTimeInCache = timeInMillis() - timeCachePolicy.inWholeMilliseconds
val withoutExpired =
cache.modify { cachedToolInfo ->
// Filter entries belonging to the current seed and have not expired
val validEntries =
cachedToolInfo
.filter { (key, value) ->
if (key.seed == seed) lastTimeInCache <= value.timestamp else true
}
.toMutableMap()
// Remove expired entries for the current seed only
cachedToolInfo.keys.removeAll { key -> key.seed == seed && !validEntries.containsKey(key) }
// Modifies state A, and returns state B
Pair(cachedToolInfo, validEntries)
}
return withoutExpired.map { it.key.value to it.value.value }.toMap()
}

abstract suspend fun onCacheMissed(input: Input): Output

private suspend fun cache(input: CachedToolKey<Input>, block: suspend () -> Output): Output {
val cachedToolInfo = cache.get().get(input)
val cachedToolInfo = cache.get()[input]
if (cachedToolInfo != null) {
val lastTimeInCache = timeInMillis() - timeCachePolicy.inWholeMilliseconds
if (lastTimeInCache > cachedToolInfo.timestamp) {
Expand All @@ -32,7 +55,7 @@ abstract class CachedTool<Input, Output>(
}
}
val response = block()
cache.get().put(input, CachedToolValue(response, timeInMillis()))
cache.get()[input] = CachedToolValue(response, timeInMillis())
return response
}
}
Loading