title: How to implement immutable cache in Scala?
body: <p>Suppose I have a server, which calls a time consuming function <code>slow: Int =&gt; String</code> upon a client request. If <code>slow</code> does not return within the <code>timeout</code> the server returns an error to the client.</p>
  
  <pre><code>def trySlow(timeout: Duration)(id: Int): Try[String] = {
    val fut = Future(slow(id))
    try {
      Await.ready(fut, timeout).value match {
        case Some(r) =&gt; r
        case None =&gt; Failure(new TimeoutException()) // should not happen
      }
    } catch {
       case e: TimeoutException =&gt; Failure(e)
    } 
  }
  </code></pre>
  
  <p>Now I'd like to cache the futures so that multiple threads calling <code>trySlow</code> with the same <code>id</code> will wait for the same future.</p>
  
  <p>I am going to use a <em>mutable</em> concurrent <a href="http://www.scala-lang.org/api/current/#scala.collection.concurrent.TrieMap" rel="nofollow">TrieMap</a> to implement a <em>singleton</em> cache.</p>
  
  <pre><code>case class CacheEntry (
    future: Future[String], 
    start: Long = System.currentTimeMillis() // need it to clean the cache
  ) 
  
  type Cache = TrieMap[Int, CacheEntry]
  
  def trySlow(timeout: Duration, cache: Cache)(id: Int): Try[String] = {
  
    val fut = cache.getOrElseUpdate(id,  CacheEntry(Future(slow(id))))
  
    ... // as in above
  }
  </code></pre>
  
  <p>Does it make sense ? How to do it with <em>immutable</em> <em>non-singleton</em> cache ?</p>