本文整理汇总了Scala中com.google.common.cache.LoadingCache类的典型用法代码示例。如果您正苦于以下问题:Scala LoadingCache类的具体用法?Scala LoadingCache怎么用?Scala LoadingCache使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了LoadingCache类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: TimedCache
//设置package包名称以及导入依赖的类
package webby.commons.cache
import java.util.concurrent.TimeUnit
import java.{lang => jl}
import com.google.common.cache.{CacheBuilder, CacheLoader, LoadingCache}
abstract class TimedCache[K, RealK, V](val cache: LoadingCache[RealK, V]) {
def get(key: K): V = cache.get(transformKey(key))
def invalidate(key: K): Unit = cache.invalidate(transformKey(key))
def refresh(key: K): Unit = cache.refresh(transformKey(key))
protected def transformKey(key: K): RealK
}
object TimedCache {
def default[K <: AnyRef, V <: AnyRef](expireDuration: Long, expireUnit: TimeUnit)(cacheLoader: K => V): TimedCache[K, K, V] =
new TimedCache[K, K, V](
CacheBuilder.newBuilder().expireAfterWrite(expireDuration, expireUnit).build(new CacheLoader[K, V] {
def load(key: K): V = cacheLoader(key)
})) {
protected def transformKey(key: K): K = key
}
def intKey[V <: AnyRef](expireDuration: Long, expireUnit: TimeUnit)(cacheLoader: Int => V): TimedCache[Int, jl.Integer, V] =
new TimedCache[Int, jl.Integer, V](
CacheBuilder.newBuilder().expireAfterWrite(expireDuration, expireUnit).build(new CacheLoader[jl.Integer, V] {
def load(key: jl.Integer): V = cacheLoader(key)
})) {
protected def transformKey(key: Int): Integer = key
}
def stringKey[V <: AnyRef](expireDuration: Long, expireUnit: TimeUnit)(cacheLoader: String => V): TimedCache[String, String, V] =
default[String, V](expireDuration, expireUnit)(cacheLoader)
}