Created
September 8, 2012 14:40
-
-
Save tobnee/3675523 to your computer and use it in GitHub Desktop.
Basic wrapping of guava cache in Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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