-
Notifications
You must be signed in to change notification settings - Fork 48
fix: Course screen requests optimization #473
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
Open
PavloNetrebchuk
wants to merge
4
commits into
openedx:develop
Choose a base branch
from
raccoongang:fix/course_requests_optimization
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
course/src/main/java/org/openedx/course/data/repository/CoalescingCache.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package org.openedx.course.data.repository | ||
|
|
||
| import kotlinx.coroutines.CompletableDeferred | ||
| import java.util.concurrent.ConcurrentHashMap | ||
|
|
||
| /** | ||
| * A cache with request coalescing support. | ||
| * | ||
| * When multiple callers request the same data simultaneously, | ||
| * only one fetch operation is performed and all callers receive the same result. | ||
| * | ||
| * @param K the type of cache keys | ||
| * @param V the type of cached values | ||
| * @param fetch the suspend function to fetch data for a given key | ||
| * @param persist optional callback invoked after successful fetch (e.g., to save to database) | ||
| */ | ||
| class CoalescingCache<K, V>( | ||
| private val fetch: suspend (K) -> V, | ||
| private val persist: (suspend (K, V) -> Unit)? = null | ||
| ) { | ||
| private val cache = ConcurrentHashMap<K, V>() | ||
| private val pending = ConcurrentHashMap<K, CompletableDeferred<V>>() | ||
|
|
||
| /** | ||
| * Returns cached value for the key, or null if not cached. | ||
| */ | ||
| fun getCached(key: K): V? = cache[key] | ||
|
|
||
| /** | ||
| * Manually sets a cached value. | ||
| */ | ||
| fun setCached(key: K, value: V) { | ||
| cache[key] = value | ||
| } | ||
|
|
||
| /** | ||
| * Removes all cached values. | ||
| */ | ||
| fun clear() { | ||
| cache.clear() | ||
| } | ||
|
|
||
| /** | ||
| * Gets the value from cache or fetches it. | ||
| * | ||
| * If [forceRefresh] is false and a cached value exists, returns it immediately. | ||
| * Otherwise, fetches the value. If another fetch for the same key is already | ||
| * in progress, waits for that result instead of making a duplicate request. | ||
| */ | ||
| suspend fun getOrFetch(key: K, forceRefresh: Boolean = false): V { | ||
| if (!forceRefresh) { | ||
| cache[key]?.let { return it } | ||
| } | ||
|
|
||
| val (deferred, isOwner) = getOrCreateDeferred(key) | ||
| return if (isOwner) { | ||
| try { | ||
| val result = fetch(key) | ||
| cache[key] = result | ||
| persist?.invoke(key, result) | ||
| deferred.complete(result) | ||
| result | ||
| } catch (e: Exception) { | ||
| deferred.completeExceptionally(e) | ||
| throw e | ||
| } finally { | ||
| pending.remove(key) | ||
| } | ||
| } else { | ||
| deferred.await() | ||
| } | ||
| } | ||
|
|
||
| private fun getOrCreateDeferred(key: K): Pair<CompletableDeferred<V>, Boolean> { | ||
| pending[key]?.let { return it to false } | ||
| val deferred = CompletableDeferred<V>() | ||
| val existing = pending.putIfAbsent(key, deferred) | ||
| return if (existing != null) existing to false else deferred to true | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are get and set functions, I think we need to add clear cache func. also
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done