I'd say yes, there can be a problem if more threads are calling foo
at the same time. Even though I was thinking that if you're only adding to the map and always the same value for a key, there is no guarantee that it must work. If implementation of getOrElseUpdate
method of Map is for whatever reason stateful, it would not work. The contract of mutable Map is by definition not thread-safe.
If you can, add synchronization to your map and you're fine:
private val cache = new HashMap[A, B] with SynchronizedMap[A, B]