Created
January 10, 2018 13:36
-
-
Save pcejrowski/54f5c388bf5f02481479af2f994f8e2e to your computer and use it in GitHub Desktop.
Simple in-memory cache in Scala
This file contains hidden or 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 java.util.concurrent.ConcurrentHashMap | |
class EternalInMemoryCache[A, B]{ | |
private val underlying = new ConcurrentHashMap[A,B]() | |
def cached(key: A)(value: => B): B = { | |
Option(underlying.get(key)) match { | |
case Some(v) => v | |
case None => | |
underlying.put(key, value) | |
value | |
} | |
} | |
} | |
val cache = new EternalInMemoryCache[String, String] | |
cache.cached("foo"){"bar"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment