Skip to content

Commit

Permalink
Use compute to avoid concurrent initialization of the same key
Browse files Browse the repository at this point in the history
  • Loading branch information
lolgab committed Sep 30, 2024
1 parent f959deb commit c8798c6
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ private[scalafix] object ScalafixCache {
private val cache = new ConcurrentHashMap[(String, Seq[Repository]), SoftReference[Scalafix]]

def getOrElseCreate(scalaVersion: String, repositories: Seq[Repository]) =
cache.get((scalaVersion, repositories)) match {
case SoftReference(value) => value
case _ =>
val newResult =
Scalafix.fetchAndClassloadInstance(scalaVersion, repositories.map(CoursierUtils.toApiRepository).asJava)
cache.put((scalaVersion, repositories), SoftReference(newResult))
newResult
}
cache.compute(
(scalaVersion, repositories),
{
case (_, v @ SoftReference(_)) => v
case _ =>
SoftReference(
Scalafix.fetchAndClassloadInstance(scalaVersion, repositories.map(CoursierUtils.toApiRepository).asJava)
)
}
)()
}

0 comments on commit c8798c6

Please sign in to comment.