本文整理汇总了Scala中com.google.common.cache.CacheLoader类的典型用法代码示例。如果您正苦于以下问题:Scala CacheLoader类的具体用法?Scala CacheLoader怎么用?Scala CacheLoader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CacheLoader类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: Server
//设置package包名称以及导入依赖的类
package com.twitter.finagle.kestrel
import _root_.java.net.SocketAddress
import _root_.java.util.concurrent.{BlockingDeque, LinkedBlockingDeque}
import com.google.common.cache.{CacheBuilder, CacheLoader}
import com.twitter.finagle.builder.{Server => BuiltServer, ServerBuilder}
import com.twitter.finagle.{ServiceFactory, ClientConnection}
import com.twitter.util.{Future, Time}
import org.jboss.netty.buffer.ChannelBuffer
import protocol.{Kestrel, Command, Response}
class Server(address: SocketAddress) {
private[this] val serviceFactory = new ServiceFactory[Command, Response] {
private[this] val queues = CacheBuilder.newBuilder()
.build(new CacheLoader[ChannelBuffer, BlockingDeque[ChannelBuffer]] {
def load(k: ChannelBuffer) = new LinkedBlockingDeque[ChannelBuffer]
})
def apply(conn: ClientConnection) = Future.value(new InterpreterService(new Interpreter(queues)))
def close(deadline: Time) = Future.Done
}
private[this] val serverSpec =
ServerBuilder()
.name("schmestrel")
.codec(Kestrel())
.bindTo(address)
private[this] var server: Option[BuiltServer] = None
def start(): BuiltServer = {
server = Some(serverSpec.build(serviceFactory))
server.get
}
def stop() {
require(server.isDefined, "Server is not open!")
server.foreach { server =>
server.close()
this.server = None
}
}
}
示例2: 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)
}
示例3: GuavaCacheTest
//设置package包名称以及导入依赖的类
package com.twitter.cache.guava
import com.google.common.cache.{CacheLoader, CacheBuilder}
import com.twitter.cache.AbstractFutureCacheTest
import com.twitter.util.{Future, Promise}
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
@RunWith(classOf[JUnitRunner])
class GuavaCacheTest extends AbstractFutureCacheTest {
def name: String = "GuavaCache"
def mkCtx(): Ctx = new Ctx {
val guava = CacheBuilder.newBuilder().build[String, Future[String]]()
val cache = new GuavaCache[String, String](guava)
}
def mkCache() =
CacheBuilder
.newBuilder()
.build(
new CacheLoader[String, Future[Int]] {
override def load(k: String): Future[Int] = new Promise[Int]
}
)
test("GuavaCache#fromLoadingCache is interrupt safe") {
val fCache = GuavaCache.fromLoadingCache(mkCache())
interruptSafe(fCache)
}
test("GuavaCache#fromCache is interrupt safe") {
val fCache = GuavaCache.fromCache((_:String) => new Promise[Int], mkCache())
interruptSafe(fCache)
}
}
示例4: LoadingFutureCacheTest
//设置package包名称以及导入依赖的类
package com.twitter.cache.guava
import com.google.common.cache.{CacheLoader, CacheBuilder}
import com.twitter.cache.AbstractLoadingFutureCacheTest
import com.twitter.util.Future
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
@RunWith(classOf[JUnitRunner])
class LoadingFutureCacheTest extends AbstractLoadingFutureCacheTest {
def name: String = "LoadingFutureCache (guava)"
// NB we can't reuse AbstractFutureCacheTest since
// loading cache semantics are sufficiently unique
// to merit distinct tests.
def mkCtx: Ctx = new Ctx {
val cache = new LoadingFutureCache(
CacheBuilder
.newBuilder()
.build(
new CacheLoader[String,Future[Int]] {
override def load(k: String): Future[Int] = {
cacheLoaderCount += 1
Future.value(k.hashCode)
}
}
)
)
}
}