-
-
Save maxpert/96f26f3a480cf8811e3581d48bf60ab1 to your computer and use it in GitHub Desktop.
Coroutine Debouncer Usage
This file contains 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
val debouncer = CoroutineDebouncer(ConcurrentHashMap()) | |
... | |
/** | |
* Gets list of menus for given restaurant ID | |
* | |
* @param id of the restaurant | |
* @return list of menu items | |
*/ | |
suspend fun getRestaurantMenus(id: Long) : List<Menu> { | |
val cached = cacheL1.get(id) | |
if (cached != null) { | |
return cached.toList(); | |
} | |
val deferredFetch = debouncer.debounce("restaurant:$id") { | |
fetchMenuFromRemoteCacheOrDatabase(id) | |
} | |
return deferredFetch.await() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment