Create a second cache with the same values as this one.
Create a second cache with the same values as this one.
The two caches will start with the same values, but will be independently updated.
Given a key, either return a cached value, or compute, store, and return a new value.
Given a key, either return a cached value, or compute, store, and return a new value.
This method is what justifies the existence of Cache. Its second
parameter (v
) is by-name: it will only be evaluated in cases
where the key is not cached.
For example:
def greet(i: Int): Int = { println("hi") i + 1 }
val c = Cache.empty[Int, Int] c.getOrElseUpdate(1, greet(1)) // says hi, returns 2 c.getOrElseUpdate(1, greet(1)) // just returns 2
Forget all cached keys and values.
Forget all cached keys and values.
After calling this method, the resulting cache is equivalent to Cache.empty[K, V].
Access the currently-cached keys and values as a map.
This is a useful cache for memoizing function.
The cache is implemented using a mutable pointer to an immutable map value. In the worst-case, race conditions might cause us to lose cache values (i.e. compute some keys twice), but we will never produce incorrect values.