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.*
?