@@ -5,48 +5,58 @@ import java.nio.file.attribute.BasicFileAttributes
55import java .nio .file .attribute .FileTime
66
77import dotty .tools .dotc .core .Contexts .Context
8+ import dotty .tools .dotc .core .Decorators .em
9+ import dotty .tools .dotc .report
810import dotty .tools .io .{AbstractFile , ClassPath }
911
12+ trait GlobalCache :
13+ def getOrCreateClassPath (key : AbstractFile , createValue : => ClassPath )(using Context ): ClassPath
14+
1015/** Global cache that can be shared across [[Driver ]] instances.
1116 *
1217 * This class is thread-safe.
1318 */
14- final class GlobalCache ():
15- import GlobalCache .FileBasedCache
16-
19+ private final class GlobalCacheImpl () extends GlobalCache :
1720 private val classPathCache = FileBasedCache [ClassPath ]()
1821
1922 def getOrCreateClassPath (key : AbstractFile , createValue : => ClassPath )(using Context ): ClassPath =
2023 classPathCache.getOrCreate(key.file.nn.toPath, () => createValue)
2124
2225object GlobalCache :
26+ def apply (): GlobalCache =
27+ GlobalCacheImpl ()
2328
24- /** A cache for values associated with files on disk, that invalidates
25- * the cached value when the file is modified.
26- *
27- * See https://github.com/scala/bug/issues/10295 for some context on the
28- * invalidation strategy.
29- *
30- * Moved from [[ZipAndJarFileLookupFactory ]] in December 2025.
31- *
32- * @author @allanrenucci
33- */
34- private class FileBasedCache [T ]:
35- private case class Stamp (lastModified : FileTime , fileKey : Object )
36- private val cache = collection.mutable.Map .empty[java.nio.file.Path , (Stamp , T )]
37-
38- def getOrCreate (path : java.nio.file.Path , create : () => T ): T =
39- cache.synchronized :
40- val attrs = Files .readAttributes(path, classOf [BasicFileAttributes ])
41- val lastModified = attrs.lastModifiedTime()
42- // null on some platforms, but that's okay, we just use the last
43- // modified timestamp as our stamp in that case
44- val fileKey = attrs.fileKey()
45- val stamp = Stamp (lastModified, fileKey)
46- cache.get(path) match
47- case Some ((cachedStamp, cached)) if cachedStamp == stamp =>
48- cached
49- case _ =>
50- val value = create()
51- cache.put(path, (stamp, value))
52- value
29+ object NoGlobalCache extends GlobalCache :
30+ def getOrCreateClassPath (key : AbstractFile , createValue : => ClassPath )(using Context ): ClassPath =
31+ report.configurationWarning(em " Not GlobalCache set " )
32+ createValue
33+
34+ /** A cache for values associated with files on disk, that invalidates
35+ * the cached value when the file is modified.
36+ *
37+ * See https://github.com/scala/bug/issues/10295 for some context on the
38+ * invalidation strategy.
39+ *
40+ * Moved from [[ZipAndJarFileLookupFactory ]] in December 2025.
41+ *
42+ * @author @allanrenucci
43+ */
44+ private class FileBasedCache [T ]:
45+ private case class Stamp (lastModified : FileTime , fileKey : Object )
46+ private val cache = collection.mutable.Map .empty[java.nio.file.Path , (Stamp , T )]
47+
48+ def getOrCreate (path : java.nio.file.Path , create : () => T ): T =
49+ cache.synchronized :
50+ val attrs = Files .readAttributes(path, classOf [BasicFileAttributes ])
51+ val lastModified = attrs.lastModifiedTime()
52+ // null on some platforms, but that's okay, we just use the last
53+ // modified timestamp as our stamp in that case
54+ val fileKey = attrs.fileKey()
55+ val stamp = Stamp (lastModified, fileKey)
56+ cache.get(path) match
57+ case Some ((cachedStamp, cached)) if cachedStamp == stamp =>
58+ cached
59+ case _ =>
60+ val value = create()
61+ cache.put(path, (stamp, value))
62+ value
0 commit comments