Revert use of WeakReferences for SbtUpdateReport cached values

This commit is contained in:
Matt Dziuban 2025-06-07 23:44:29 -04:00 committed by Eugene Yokota
parent 882a10a89f
commit 8b0bcc6fcf
1 changed files with 5 additions and 6 deletions

View File

@ -1,7 +1,6 @@
package lmcoursier.internal package lmcoursier.internal
import java.io.File import java.io.File
import java.lang.ref.WeakReference
import java.util.{ Collections, GregorianCalendar, WeakHashMap } import java.util.{ Collections, GregorianCalendar, WeakHashMap }
import coursier.cache.CacheUrl import coursier.cache.CacheUrl
import coursier.{ Attributes, Dependency, Module, Project, Resolution } import coursier.{ Attributes, Dependency, Module, Project, Resolution }
@ -18,16 +17,16 @@ private[internal] object SbtUpdateReport {
private def caching[K, V](f: K => V): K => V = { private def caching[K, V](f: K => V): K => V = {
val cache = Collections.synchronizedMap(new WeakHashMap[K, WeakReference[V]]) val cache = Collections.synchronizedMap(new WeakHashMap[K, V])
key => key =>
val previousValueOpt = Option(cache.get(key)) val previousValueOpt = Option(cache.get(key))
previousValueOpt.fold { previousValueOpt.getOrElse {
val value = f(key) val value = f(key)
val concurrentValueOpt = Option(cache.putIfAbsent(key, new WeakReference(value))) val concurrentValueOpt = Option(cache.putIfAbsent(key, value))
concurrentValueOpt.fold(value)(_.get()) concurrentValueOpt.getOrElse(value)
}(_.get()) }
} }
private def infoProperties(project: Project): Seq[(String, String)] = private def infoProperties(project: Project): Seq[(String, String)] =