Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cache results in scala?

Tags:

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

like image 298
Jeriho Avatar asked Sep 06 '10 12:09

Jeriho


People also ask

How do I use cache in spark?

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.

What is Memoization in Scala?

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.

What is ehcache used for?

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.


1 Answers

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

like image 131
oluies Avatar answered Sep 22 '22 23:09

oluies