This page has a description of Map's getOrElseUpdate
usage method:
object WithCache{ val cacheFun1 = collection.mutable.Map[Int, Int]() def fun1(i:Int) = i*i def catchedFun1(i:Int) = cacheFun1.getOrElseUpdate(i, fun1(i)) }
So you can use catchedFun1
which will check if cacheFun1
contains key and return value associated with it. Otherwise, it will invoke fun1
, then cache fun1
's result in cacheFun1
, then return fun1
's result.
I can see one potential danger - cacheFun1
can became to large. So cacheFun1
must be cleaned somehow by garbage collector?
P.S. What about scala.collection.mutable.WeakHashMap and java.lang.ref.*
?
Caching methods in SparkDISK_ONLY: Persist data on disk only in serialized format. MEMORY_ONLY: Persist data in memory only in deserialized format. MEMORY_AND_DISK: Persist data in memory and if enough memory is not available evicted blocks will be stored on disk. OFF_HEAP: Data is persisted in off-heap memory.
Memoization is an optimization technique of caching the output of an expensive function for a particular input and then returning the cached result if the function is called again with the same input parameters.
Ehcache is a standards-based caching API that is used by Integration Server. Caching enables an application to fetch frequently used data from memory (or other nearby resource) rather than having to retrieve it from a database or other back-end system each time the data is needed.
See the Memo pattern and the Scalaz implementation of said paper.
Also check out a STM implementation such as Akka.
Not that this is only local caching so you might want to lookinto a distributed cache or STM such as CCSTM, Terracotta or Hazelcast
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With