mirror of
https://github.com/digital-asset/daml.git
synced 2024-09-20 01:07:18 +03:00
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:
parent
6142241719
commit
1018e2744b
@ -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()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user