Skip to content

Make cache eviction write-back optional per repository#32

Merged
Aerhhh merged 1 commit into
masterfrom
optional-eviction-writeback
Jul 19, 2026
Merged

Make cache eviction write-back optional per repository#32
Aerhhh merged 1 commit into
masterfrom
optional-eviction-writeback

Conversation

@Aerhhh

@Aerhhh Aerhhh commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

The removal listener in Repository writes entries back to Mongo whenever they leave the cache for any reason other than explicit removal or replacement. That's fine for a single process, but when multiple processes share a database it can resurrect documents another process deleted, or overwrite newer data with a stale cached copy (the write-back is a full-document $set upsert).

This adds a saveOnEviction flag through a new constructor overload so a repository can opt out of the write-back. The two existing constructors delegate with true, so nothing changes for current consumers or for RepositoryManager's reflective instantiation.

Also adds tests for the removal listener: eviction saves with write-back enabled (both via the old constructor and an explicit true), doesn't save when disabled, and explicit removal never saves in either mode. mvn clean verify passes.

Evicted cache entries always get written back to Mongo, which causes
problems when multiple processes share a database - a stale cached copy
can resurrect documents another process deleted, or clobber newer writes
since the write-back is a full-document upsert.

Repositories can now pass saveOnEviction=false to opt out. The existing
constructors keep the old behaviour.
@Aerhhh
Aerhhh merged commit 2162807 into master Jul 19, 2026
3 checks passed
@Aerhhh
Aerhhh deleted the optional-eviction-writeback branch July 19, 2026 00:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant