Skip to content

Commit

Permalink
use java collections for cache implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
github-brice-jaglin committed Jul 7, 2020
1 parent 89cfd04 commit e822f41
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/main/scala/scalafix/internal/sbt/BlockingCache.scala
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, _ => value)
}

0 comments on commit e822f41

Please sign in to comment.