• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala CacheLoader类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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
    }
  }
} 
开发者ID:deenar,项目名称:fintest,代码行数:46,代码来源:Server.scala


示例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)
} 
开发者ID:citrum,项目名称:webby,代码行数:39,代码来源:TimedCache.scala


示例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)
  }
} 
开发者ID:lanshuijuntuan,项目名称:Java.util,代码行数:37,代码来源:GuavaCacheTest.scala


示例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)
            }
          }
        )
    )
  }
} 
开发者ID:lanshuijuntuan,项目名称:Java.util,代码行数:33,代码来源:LoadingFutureCacheTest.scala



注:本文中的com.google.common.cache.CacheLoader类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala StringSerializer类代码示例发布时间:2022-05-23
下一篇:
Scala PrivateMethodTester类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap