From c8798c6d6f987580d221520a9d8db55cad3c9aa6 Mon Sep 17 00:00:00 2001 From: Lorenzo Gabriele Date: Mon, 30 Sep 2024 22:19:12 +0200 Subject: [PATCH] Use `compute` to avoid concurrent initialization of the same key --- .../goyeau/mill/scalafix/ScalafixCache.scala | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala b/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala index 8c27e44..72681a9 100644 --- a/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala +++ b/mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala @@ -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) + ) + } + )() }