I am writing a generic cache for a few of my entities in scala 2.10.1. For now, I am using google Guava's CacheBuilder since there aren't many options in scala ecosystem.
Code:
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]
However, sbt throws error:
[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
Any ideas? Even if I add K<:Object like this:
trait CachedEntity[E <: KeyedEntity[K],K <:Object] {
I get this error
[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
CacheBuilder requires an Object/AnyRef. You can use java.lang.Long instead of scala.Long as follows; Scala will automatically box/unbox as required.
If you don't mind a little ugly cast, you can get this to work. The main issue is that the
build
function onCacheBuilder
returns a cache tied to types[Object,Object]
. In Scala, theAnyVal
is not derived from Object, so it won't work. But I mocked up the following code sample to show how you can work around this limitation with a little ugly casting: