From f959deb796ffbeae31fdc0380ac54f5d59491f41 Mon Sep 17 00:00:00 2001 From: Lorenzo Gabriele Date: Mon, 30 Sep 2024 17:15:23 +0200 Subject: [PATCH] Use ConcurrentHashMap for better thread safety --- .../src/com/goyeau/mill/scalafix/ScalafixCache.scala | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala b/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala index 2fa3797..8c27e44 100644 --- a/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala +++ b/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala @@ -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 } }