Add put and getIfPresent methods to Cache (#6007)

* Add put method to Cache

changelog_begin
changelog_end

* Add getIfPresent method
This commit is contained in:
Stefano Baghino 2020-05-18 13:53:08 +02:00 committed by GitHub
parent 6142241719
commit 1018e2744b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,8 +8,12 @@ import com.github.benmanes.caffeine.{cache => caffeine}
import scala.compat.java8.OptionConverters._
sealed abstract class Cache[Key, Value] {
def put(key: Key, value: Value): Unit
def get(key: Key, acquire: Key => Value): Value
def getIfPresent(key: Key): Option[Value]
def size: Cache.Size
def weight: Cache.Size
@ -37,8 +41,12 @@ object Cache {
}
final class NoCache[Key, Value] private[Cache] extends Cache[Key, Value] {
override def put(key: Key, value: Value): Unit = ()
override def get(key: Key, acquire: Key => Value): Value = acquire(key)
override def getIfPresent(key: Key): Option[Value] = None
override val size: Cache.Size = 0
override val weight: Cache.Size = 0
@ -46,9 +54,14 @@ object Cache {
final class CaffeineCache[Key, Value] private[Cache] (val cache: caffeine.Cache[Key, Value])
extends Cache[Key, Value] {
override def put(key: Key, value: Value): Unit = cache.put(key, value)
override def get(key: Key, acquire: Key => Value): Value =
cache.get(key, key => acquire(key))
override def getIfPresent(key: Key): Option[Value] =
Option(cache.getIfPresent(key))
override def size: Cache.Size =
cache.estimatedSize()