Skip to content

Instantly share code, notes, and snippets.

@tobnee
Created September 8, 2012 14:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobnee/3675523 to your computer and use it in GitHub Desktop.
Save tobnee/3675523 to your computer and use it in GitHub Desktop.
Basic wrapping of guava cache in Scala
import com.google.common.cache.CacheBuilder
import com.google.common.cache.CacheLoader
import com.google.common.cache.LoadingCache
import com.google.common.cache.Cache
object CacheUtil {
implicit def functionToCacheLoader[F, T](f: F => T) = {
new CacheLoader[F, T] {
def load(key: F) = f(key)
}
}
implicit def pimpCache[F, T](cache: Cache[F, T]) = {
new PimpedCache(cache)
}
class PimpedCache[F, T](cache: Cache[F, T]) {
def getOption(key: F) = {
val value = cache.getIfPresent(key)
if(value == null) None else Some(value)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment