Skip to content

Commit

Permalink
Add support for eviction listener
Browse files Browse the repository at this point in the history
  • Loading branch information
blemale committed Jun 5, 2021
1 parent 5d172e1 commit b650b04
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/main/scala/com/github/blemale/scaffeine/Scaffeine.scala
Expand Up @@ -258,6 +258,29 @@ case class Scaffeine[K, V](underlying: caffeine.cache.Caffeine[K, V]) {
})
)

/** Specifies a listener instance that caches should notify each time an entry is evicted.
*
* @param evictionListener a listener that caches should notify each time an entry is
* being automatically removed due to eviction
* @tparam K1 the key type of the listener
* @tparam V1 the value type of the listener
* @return this builder instance
* @throws java.lang.IllegalStateException if a removal listener was already set
*/
def evictionListener[K1 <: K, V1 <: V](
evictionListener: (K1, V1, caffeine.cache.RemovalCause) => Unit
): Scaffeine[K1, V1] =
Scaffeine(
underlying.evictionListener(new caffeine.cache.RemovalListener[K1, V1] {

override def onRemoval(
key: K1,
value: V1,
cause: caffeine.cache.RemovalCause
): Unit = evictionListener(key, value, cause)
})
)

/** Specifies a writer instance that caches should notify each time an entry is explicitly created
* or modified, or removed for any [[com.github.benmanes.caffeine.cache.RemovalCause]].
* <p>
Expand Down

0 comments on commit b650b04

Please sign in to comment.