Skip to content

Instantly share code, notes, and snippets.

@maxpert
Created August 1, 2018 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maxpert/96f26f3a480cf8811e3581d48bf60ab1 to your computer and use it in GitHub Desktop.
Save maxpert/96f26f3a480cf8811e3581d48bf60ab1 to your computer and use it in GitHub Desktop.
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