4

我正在为 scala 2.10.1 中的一些实体编写通用缓存。目前,我正在使用 google Guava 的 CacheBuilder,因为 scala 生态系统中没有太多选择。

代码:

trait CachedEntity[E <: KeyedEntity[K],K] {

  def lookup(id:K):E

  def getElem(id:K):Option[E] = Try(elemCache.get(id)).toOption

  val elemCache = CacheBuilder.newBuilder().maximumSize(10).expireAfterWrite(1,TimeUnit.MINUTES).build(
    new CacheLoader[K,E] {
      def load(key:K) = {
        println("Looking Up key:" + key + "in Class:" + this.getClass.getName)
        lookup(key)
      }
    }
  )
}
trait LongKeyed[E<: KeyedEntity[Long],Long] extends CachedEntity[E,Long]

但是,sbt 抛出错误:

[error] KEHCaching.scala:16: type mismatch;
[error]  found   : id.type (with underlying type K)
[error]  required: Object with K
[error]   def getElem(id:K):Option[E] = Try(elemCache.get(id)).toOption
[error]                                                   ^
[error] one error found

有任何想法吗?即使我像这样添加 K<:Object :

trait CachedEntity[E <: KeyedEntity[K],K <:Object] {

我收到这个错误

[error] KEHCaching.scala:27: type arguments [E,Long] do not conform to trait CachedEntity's type parameter bounds [E <: org.squeryl.KeyedEntity[K],K <: Object]
[error] trait LongKeyed[E<: KeyedEntity[Long],Long] extends CachedEntity[E,Long]
[error]                                                     ^
[error] one error found
4

2 回答 2

3

CacheBuilder 需要一个 Object/AnyRef。您可以使用 java.lang.Long 代替 scala.Long 如下;Scala 将根据需要自动装箱/拆箱。

import scala.util.Try
import java.util.concurrent.TimeUnit
import java.lang.{Long => JLong}

trait KeyedEntity[K]

trait CachedEntity[E <: KeyedEntity[K], K <: AnyRef] {

  def lookup(id:K):E

  def getElem(id:K):Option[E] = Try(elemCache.get(id)).toOption

  val elemCache = CacheBuilder.newBuilder().maximumSize(10).expireAfterWrite(1,TimeUnit.MINUTES).build(
    new CacheLoader[K,E] {
      def load(key:K) = {
        println("Looking Up key:" + key + "in Class:" + this.getClass.getName)
        lookup(key)
      }
    }
  )
}

trait LongKeyed[E <: KeyedEntity[JLong]] extends CachedEntity[E,JLong]
于 2013-08-02T01:37:55.617 回答
2

如果你不介意有点丑陋的演员表,你可以让它工作。主要问题是buildon 函数CacheBuilder返回与 types 相关的缓存[Object,Object]。在 Scala 中,AnyVal不是从 Object 派生的,所以它不起作用。但是我模拟了以下代码示例,以展示如何通过一些难看的转换来解决这个限制:

trait CachedEntity[E <: KeyedEntity[K], K] {

  def lookup(id:K):E

  def getElem(id:K):Option[E] = Try(elemCache.get(id)).toOption

  val elemCache = CacheBuilder.newBuilder().build(
    new CacheLoader[K,E] {
      def load(key:K) = {
        println("Looking Up key:" + key + "in Class:" + this.getClass.getName)
        lookup(key)
     }
    }
  ).asInstanceOf[LoadingCache[K,E]]
}

trait LongKeyed[E<: KeyedEntity[Long]] extends CachedEntity[E,Long]

case class MyEntity(id:Long, value:String) extends KeyedEntity[Long]

class MyEntityCache extends LongKeyed[MyEntity]{
  def lookup(id:Long) = MyEntity(id, "foo") 
}

object CachedEntityTest{
  def main(args: Array[String]) {
    val cache = new MyEntityCache
    val entity = cache.getElem(1)
    println(entity)
  }
}

//Faking this for purposes of code sample...
trait KeyedEntity[K]
于 2013-04-25T00:41:00.173 回答