Skip to content

Instantly share code, notes, and snippets.

@yuikns
Created July 15, 2015 18:40
Show Gist options
  • Save yuikns/3b2206ddbc6421a239e7 to your computer and use it in GitHub Desktop.
Save yuikns/3b2206ddbc6421a239e7 to your computer and use it in GitHub Desktop.
package org.aminer.io.utils
import scala.util.Try
import com.google.common.cache.CacheBuilder
import com.google.common.cache.CacheLoader
import com.google.common.cache.LoadingCache
//import com.google.common.cache.CacheBuilder
//import com.google.common.cache.CacheLoader
//import com.google.common.cache.LoadingCache
//Faking this for purposes of code sample...
trait KeyedEntity[K]
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 BasicCache {
// def main(args: Array[String]) {
// val cache = new MyEntityCache
// val entity = cache.getElem(1)
// println(entity)
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment