Skip to content

Commit

Permalink
Added evictionListener extension
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Dec 3, 2024
1 parent cb2817b commit 8ee6abf
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions aedile-core/src/main/kotlin/com/sksamuel/aedile/core/config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,30 @@ fun <K, V> Caffeine<K, V>.scheduler(scheduler: Scheduler): Caffeine<K, V> {
}
}

/**
* Specifies a listener that is notified each time an entry is removed.
* See full docs at [Caffeine.removalListener].
*/
fun <K, V> Caffeine<K, V>.removalListener(
scope: CoroutineScope,
listener: suspend (K?, V?, RemovalCause) -> Unit,
): Caffeine<K, V> {
return removalListener { key, value, cause ->
scope.launch {
listener.invoke(key, value, cause)
}
}
}

/**
* Specifies a listener that is notified each time an entry is evicted.
* See full docs at [Caffeine.evictionListener].
*/
fun <K, V> Caffeine<K, V>.removalListener(
fun <K, V> Caffeine<K, V>.evictionListener(
scope: CoroutineScope,
listener: suspend (K?, V?, RemovalCause) -> Unit
listener: suspend (K?, V?, RemovalCause) -> Unit,
): Caffeine<K, V> {
return removalListener<K, V> { key, value, cause ->
return evictionListener { key, value, cause ->
scope.launch {
listener.invoke(key, value, cause)
}
Expand Down

0 comments on commit 8ee6abf

Please sign in to comment.