Skip to content

Commit d3bcb80

Browse files
committed
Updated docs for listeners
1 parent dcd0edf commit d3bcb80

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

README.md

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ val value2 = cache.get("foo") {
4545
}
4646
```
4747

48-
The `asLoadingCache` method supports a generic compute function which is used if no specific compute function is provided.
48+
The `asLoadingCache` method supports a generic compute function which is used if no specific compute function is
49+
provided.
4950

5051
```kotlin
5152
val cache = Caffeine.newBuilder().asLoadingCache<String, String>() {
@@ -66,10 +67,10 @@ For example:
6667

6768
```kotlin
6869
val cache = Caffeine
69-
.newBuilder()
70-
.expireAfterWrite(1.hours) // supports kotlin.time.Duration
71-
.maximumSize(100) // standard Caffeine option
72-
.asCache<String, String>()
70+
.newBuilder()
71+
.expireAfterWrite(1.hours) // supports kotlin.time.Duration
72+
.maximumSize(100) // standard Caffeine option
73+
.asCache<String, String>()
7374
```
7475

7576
## Evictions
@@ -89,22 +90,36 @@ Caffeine provides different approaches to eviction:
8990
* invalidate / invalidateAll: Programatically remove entries based on their key(s) or remove all entries. In the case of
9091
a loading cache, any currently loading values may not be removed.
9192

92-
You can specify a suspendable function to listen to evictions:
93+
You can specify a suspendable function to listen to evictions using the `withEvictionListener` method.
9394

9495
```kotlin
9596
val cache = Caffeine
9697
.newBuilder()
9798
.expireAfterWrite(1.hours) // supports kotlin.time.Duration
9899
.maximumSize(100) // standard Caffeine option
99100
.asCache<String, String>()
100-
.evictionListener { key, value, cause ->
101+
.withEvictionListener { key, value, cause ->
101102
when (cause) {
102103
RemovalCause.SIZE -> println("Removed due to size constraints")
103104
else -> delay(100) // suspendable for no real reason, but just to show you can!!
104105
}
105106
}.asCache<String, String>()
106107
```
107108

109+
## Removals
110+
111+
Similar to evictions, you can specify a suspendable function to listen to removals using the `withRemovalListener`
112+
method.
113+
114+
```kotlin
115+
val cache = Caffeine
116+
.newBuilder()
117+
.asCache<String, String>()
118+
.withRemovalListener { key, value, cause ->
119+
...
120+
}.asCache<String, String>()
121+
```
122+
108123
## Coroutine Context
109124

110125
Aedile will use the context from the calling function for executing the compute functions. You can
@@ -113,11 +128,11 @@ specify your own context by just switching the context like with any suspendable
113128
```kotlin
114129
val cache = Caffeine.newBuilder().asCache<String, String>()
115130
val value = cache.get("foo") {
116-
withContext(Dispatchers.IO) {
117-
// blocking database call
118-
}
131+
withContext(Dispatchers.IO) {
132+
// blocking database call
119133
}
120134
}
135+
}
121136
```
122137

123138
## Metrics

0 commit comments

Comments
 (0)