@@ -25,18 +25,23 @@ object Blacklist {
2525 }
2626}
2727
28- final case class CacheBasedBlacklist (cache : Cache [BlacklistId , String ])
29- extends Blacklist with Logger {
28+ final case class CacheBasedBlacklist (cache : Cache [BlacklistId , String ]) extends Blacklist with Logger {
3029
3130 override def isBlacklisted (id : BlacklistId ): Boolean = cache.getIfPresent(id).isDefined
3231
3332 override def add (id : BlacklistId , duration : FiniteDuration , reason : String ): Unit =
34- cache.policy().expireVariably().toScala.fold {
35- log.warn(s " Unexpected error while adding peer [ ${id.value}] to blacklist using custom expiration time. Falling back to default expiration. " )
36- cache.put(id, reason)
37- } { varExpirationPolicy =>
38- varExpirationPolicy.put(id, reason, duration.toJava)
39- }
33+ cache
34+ .policy()
35+ .expireVariably()
36+ .toScala
37+ .fold {
38+ log.warn(
39+ s " Unexpected error while adding peer [ ${id.value}] to blacklist using custom expiration time. Falling back to default expiration. "
40+ )
41+ cache.put(id, reason)
42+ } { varExpirationPolicy =>
43+ varExpirationPolicy.put(id, reason, duration.toJava)
44+ }
4045 override def remove (id : BlacklistId ): Unit = cache.invalidate(id)
4146
4247 override def keys : Set [BlacklistId ] = cache.underlying.asMap().keySet().asScala.toSet
@@ -47,10 +52,14 @@ object CacheBasedBlacklist {
4752 def empty (maxSize : Int ): CacheBasedBlacklist = {
4853 val cache =
4954 Scaffeine ()
50- .expireAfter[BlacklistId , String ](create = (_, _) => 60 .minutes,
55+ .expireAfter[BlacklistId , String ](
56+ create = (_, _) => 60 .minutes,
5157 update = (_, _, _) => 60 .minutes,
52- read = (_, _, _) => 60 .minutes) // required to enable VarExpiration policy (i.e. set custom expiration time per element)
53- .maximumSize(maxSize) // uses Window TinyLfu eviction policy, see https://github.com/ben-manes/caffeine/wiki/Efficiency
58+ read = (_, _, _) => 60 .minutes
59+ ) // required to enable VarExpiration policy (i.e. set custom expiration time per element)
60+ .maximumSize(
61+ maxSize
62+ ) // uses Window TinyLfu eviction policy, see https://github.com/ben-manes/caffeine/wiki/Efficiency
5463 .build[BlacklistId , String ]()
5564 CacheBasedBlacklist (cache)
5665 }
0 commit comments