-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use java collections for cache implementation
- Loading branch information
1 parent
89cfd04
commit 1701d6c
Showing
1 changed file
with
6 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,17 @@ | ||
package scalafix.internal.sbt | ||
|
||
import scala.collection.mutable | ||
import java.{util => ju} | ||
|
||
/** A basic thread-safe cache without any eviction. */ | ||
class BlockingCache[K, V] { | ||
private val underlying = new mutable.HashMap[K, V] | ||
|
||
// Number of keys is expected to be very small so the global lock should not be a bottleneck | ||
private val underlying = ju.Collections.synchronizedMap(new ju.HashMap[K, V]) | ||
|
||
/** | ||
* @param value By-name parameter evaluated when the key if missing. Value computation is guaranteed | ||
* to be called only once per key across all invocations. | ||
*/ | ||
def getOrElseUpdate(key: K, value: => V): V = { | ||
// ConcurrentHashMap does not guarantee that there is only one evaluation of the value, so | ||
// we use our own (global) locking, which is OK as the number of keys is expected to be | ||
// very small (bound by the number of projects in the sbt build). | ||
underlying.synchronized { | ||
underlying.getOrElseUpdate(key, value) | ||
} | ||
} | ||
def getOrElseUpdate(key: K, value: => V): V = | ||
underlying.computeIfAbsent(key, (_: K) => value) | ||
} |