@@ -35,6 +35,7 @@ sealed trait StatementCache[F[_], V] { outer =>
3535object StatementCache {
3636
3737 def empty [F [_]: Functor : Ref .Make , V ](max : Int , trackEviction : Boolean ): F [StatementCache [F , V ]] =
38+ // State is the cache and a set of evicted values; the evicted set only grows when trackEviction is true
3839 Ref [F ].of((Cache .empty[Statement .CacheKey , V ](max), Set .empty[V ])).map { ref =>
3940 new StatementCache [F , V ] {
4041
@@ -49,7 +50,8 @@ object StatementCache {
4950 def put (k : Statement [_], v : V ): F [Unit ] =
5051 ref.update { case (c, evicted) =>
5152 val (c2, e) = c.put(k.cacheKey, v)
52- val evicted2 = e.filter(_ => trackEviction).fold(evicted) { case (_, v) => evicted + v }
53+ // Remove the value we just inserted from the evicted set and add the newly evicted value, if any
54+ val evicted2 = e.filter(_ => trackEviction).fold(evicted - v) { case (_, ev) => evicted - v + ev }
5355 (c2, evicted2)
5456 }
5557
@@ -67,8 +69,7 @@ object StatementCache {
6769
6870 def clearEvicted : F [List [V ]] =
6971 ref.modify { case (c, evicted) =>
70- val activeValues = c.values.toSet
71- (c, Set .empty[V ]) -> evicted.filterNot(activeValues).toList
72+ (c, Set .empty[V ]) -> evicted.toList
7273 }
7374 }
7475 }
0 commit comments