@@ -35,11 +35,11 @@ object Caches:
3535 s " ${this .getClass.getSimpleName}(stats() = ${stats()}) "
3636
3737 /** Statistics about a cache */
38- final case class CacheStats (total : Long , misses : Long , uncached : Long ):
38+ final case class CacheStats (total : Long , misses : Long , size : Long , uncached : Long ):
3939 val hits : Long = total - misses - uncached
4040
4141 override def toString : String =
42- s " (total = $total, hits = $hits, misses = $misses, uncached = $uncached) "
42+ s " (total = $total, hits = $hits, misses = $misses, size = $size , uncached = $uncached) "
4343
4444 /** A no-op cache implementation that does not cache anything. */
4545 final class NoopCache [K , V ] extends Cache [K , V ]:
@@ -53,7 +53,7 @@ object Caches:
5353 false
5454
5555 def stats (): CacheStats =
56- CacheStats (total, misses = 0 , uncached = total)
56+ CacheStats (total, misses = 0 , size = 0 , uncached = total)
5757
5858 /** Default value for stamp function that indicates no stamping. */
5959 private def noStamp [K ](key : K ): Option [Unit ] = Some (())
@@ -94,11 +94,17 @@ object Caches:
9494 getStamp(key).isDefined
9595
9696 def stats (): CacheStats =
97- CacheStats (total, misses, uncached)
97+ CacheStats (total, misses, map.size, uncached)
9898
9999 /** A thread-safe cache implementation based on a Java [[ConcurrentHashMap ]].
100100 *
101101 * Entries are not evicted.
102+ *
103+ * @param getStamp
104+ * Function to obtain a stamp for a given key. If the function returns
105+ * `None`, no caching is performed for that key. If the function returns
106+ * `Some(stamp)`, the stamp is used to validate cached entries: cache
107+ * values are only reused if the stamp matches the cached stamp.
102108 */
103109 final class SynchronizedMapCache [K , S , V ](getStamp : K => Option [S ] = noStamp) extends Cache [K , V ]:
104110 private val map = ConcurrentHashMap [K , (S , V )]()
@@ -127,7 +133,7 @@ object Caches:
127133 getStamp(key).isDefined
128134
129135 def stats (): CacheStats =
130- CacheStats (total.longValue(), misses.longValue(), uncached.longValue())
136+ CacheStats (total.longValue(), misses.longValue(), map.size(), uncached.longValue())
131137
132138 /** A cache where keys are [[AbstractFile ]]s.
133139 *
@@ -205,6 +211,7 @@ object Caches:
205211 CacheStats (
206212 total = baseStats.total + uncached.longValue(),
207213 misses = baseStats.misses,
214+ size = baseStats.size,
208215 uncached = baseStats.uncached + uncached.longValue()
209216 )
210217
0 commit comments