Skip to content

Commit

Permalink
Some more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mpilquist committed Jan 16, 2025
1 parent 85ab52b commit 68ed3ae
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions modules/core/shared/src/main/scala/util/StatementCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ sealed trait StatementCache[F[_], V] { outer =>
object StatementCache {

def empty[F[_]: Functor: Ref.Make, V](max: Int, trackEviction: Boolean): F[StatementCache[F, V]] =
// State is the cache and a set of evicted values; the evicted set only grows when trackEviction is true
Ref[F].of((Cache.empty[Statement.CacheKey, V](max), Set.empty[V])).map { ref =>
new StatementCache[F, V] {

Expand All @@ -49,7 +50,8 @@ object StatementCache {
def put(k: Statement[_], v: V): F[Unit] =
ref.update { case (c, evicted) =>
val (c2, e) = c.put(k.cacheKey, v)
val evicted2 = e.filter(_ => trackEviction).fold(evicted) { case (_, v) => evicted + v }
// Remove the value we just inserted from the evicted set and add the newly evicted value, if any
val evicted2 = e.filter(_ => trackEviction).fold(evicted - v) { case (_, ev) => evicted - v + ev }
(c2, evicted2)
}

Expand All @@ -67,8 +69,7 @@ object StatementCache {

def clearEvicted: F[List[V]] =
ref.modify { case (c, evicted) =>
val activeValues = c.values.toSet
(c, Set.empty[V]) -> evicted.filterNot(activeValues).toList
(c, Set.empty[V]) -> evicted.toList
}
}
}
Expand Down

0 comments on commit 68ed3ae

Please sign in to comment.