blocking
fun <Identifier, Failure, Value> Cache<Identifier, Failure, Value>.blocking(): BlockingCache<Identifier, Failure, Value>(source)
Converts an asynchronous cache into a blocking cache.
Example
// Create the coroutine context
val cachingJob = SupervisorJob()
val cachingScope = CoroutineScope(cachingJob)
// Instantiate the cache instance
val cache = cache<Int, Int> { it * 2 }
.cachedInMemory(cachingJob)
.expireAfter(2.minutes, cachingScope)
.blocking()
// Access the cache
println(cache[1]) // 2
// Force the cache to accept another value
cache[1] = 3
println(cache[1]) // 3
// Force the cache to forget the value, meaning a new request will be started on next access
cache.expire(1)
println(cache[1]) // 2
// Don't forget to stop the cache workers when you're done using the cache
cachingJob.cancel()
Content copied to clipboard
fun <Identifier, Context, Failure, Value> ContextualCache<Identifier, Context, Failure, Value>.blocking(): BlockingContextualCache<Identifier, Context, Failure, Value>(source)
Converts an asynchronous contextual cache into a blocking cache.
Example
// Create the coroutine context
val cachingJob = SupervisorJob()
val cachingScope = CoroutineScope(cachingJob)
class User(val isAdmin: Boolean)
val admin = User(true)
val user = User(false)
// Instantiate the cache instance
val cache = cache<Int, User, Nothing, Int?> { it, user ->
if (user.isAdmin)
(it * 2).success()
else
null.success()
}
.cachedInMemory(cachingJob)
.expireAfter(2.minutes, cachingScope)
.blocking()
// Access the cache as an admin
println(cache[1, admin]) // 2
// Access the cache as a regular user
println(cache[1, user]) // null
// Force the cache to accept another value
cache[1, user] = 3
println(cache[1, user]) // 3
// Force the cache to forget the value for a specific user, meaning a new request will be started on next access
cache.expire(1, user)
println(cache[1, user]) // null
// Force the cache to forget the value for all users
cache.expire(1)
println(cache[1, user]) // null
// Don't forget to stop the cache workers when you're done using the cache
cachingJob.cancel()
Content copied to clipboard