Skip to content

Commit

Permalink
Use ConcurrentHashMap for better thread safety
Browse files Browse the repository at this point in the history
  • Loading branch information
lolgab committed Sep 30, 2024
1 parent 7d57735 commit f959deb
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import com.goyeau.mill.scalafix.CoursierUtils
import coursier.core.Repository
import scalafix.interfaces.Scalafix

import scala.collection.mutable
import scala.ref.SoftReference
import java.util.concurrent.ConcurrentHashMap
import scala.jdk.CollectionConverters._
import scala.ref.SoftReference

private[scalafix] object ScalafixCache {

private val cache = mutable.Map.empty[(String, Seq[Repository]), SoftReference[Scalafix]]
private val cache = new ConcurrentHashMap[(String, Seq[Repository]), SoftReference[Scalafix]]

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

0 comments on commit f959deb

Please sign in to comment.