@@ -22,17 +22,12 @@ import FileUtils.*
2222 * when there are a lot of projects having a lot of common dependencies.
2323 */
2424sealed trait ZipAndJarFileLookupFactory {
25- private val cache = new FileBasedCache [ClassPath ]
26-
2725 def create (zipFile : AbstractFile )(using Context ): ClassPath =
2826 val release = Option (ctx.settings.javaOutputVersion.value).filter(_.nonEmpty)
2927 if (ctx.settings.YdisableFlatCpCaching .value || zipFile.file == null ) createForZipFile(zipFile, release)
30- else createUsingCache (zipFile, release)
28+ else ctx.globalCache.getOrCreateClassPath (zipFile, createForZipFile(zipFile, release) )
3129
3230 protected def createForZipFile (zipFile : AbstractFile , release : Option [String ]): ClassPath
33-
34- private def createUsingCache (zipFile : AbstractFile , release : Option [String ]): ClassPath =
35- cache.getOrCreate(zipFile.file.toPath, () => createForZipFile(zipFile, release))
3631}
3732
3833/**
@@ -172,29 +167,3 @@ object ZipAndJarSourcePathFactory extends ZipAndJarFileLookupFactory {
172167
173168 override protected def createForZipFile (zipFile : AbstractFile , release : Option [String ]): ClassPath = ZipArchiveSourcePath (zipFile.file)
174169}
175-
176- final class FileBasedCache [T ] {
177- private case class Stamp (lastModified : FileTime , fileKey : Object )
178- private val cache = collection.mutable.Map .empty[java.nio.file.Path , (Stamp , T )]
179-
180- def getOrCreate (path : java.nio.file.Path , create : () => T ): T = cache.synchronized {
181- val attrs = Files .readAttributes(path, classOf [BasicFileAttributes ])
182- val lastModified = attrs.lastModifiedTime()
183- // only null on some platforms, but that's okay, we just use the last modified timestamp as our stamp
184- val fileKey = attrs.fileKey()
185- val stamp = Stamp (lastModified, fileKey)
186- cache.get(path) match {
187- case Some ((cachedStamp, cached)) if cachedStamp == stamp => cached
188- case _ =>
189- val value = create()
190- cache.put(path, (stamp, value))
191- value
192- }
193- }
194-
195- def clear (): Unit = cache.synchronized {
196- // TODO support closing
197- // cache.valuesIterator.foreach(_.close())
198- cache.clear()
199- }
200- }
0 commit comments