Skip to content

Instantly share code, notes, and snippets.

@maxpert

maxpert/DebouncerUsage.kt Secret

Created Aug 1, 2018
Embed
What would you like to do?
Coroutine Debouncer Usage
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
You can’t perform that action at this time.