Skip to content

fix: flush/1 evicts both per-record and per-type key sets#6

Closed
BobbieBarker wants to merge 1 commit into
mainfrom
fix/flush-evicts-both-key-sets
Closed

fix: flush/1 evicts both per-record and per-type key sets#6
BobbieBarker wants to merge 1 commit into
mainfrom
fix/flush-evicts-both-key-sets

Conversation

@BobbieBarker

Copy link
Copy Markdown
Owner

Summary

  • flush/1 now evicts cache keys tracked under both the per-record set (__set:Schema:<pk>) and the per-type set (__set:Schema). Previously it only walked the per-record set, leaving collection caches stale.
  • get_set_value/3 now registers singular {:ok, value} results under the per-type key set (via associate_key_reference_with_schema_type), making eviction symmetric — flush(struct, :new_schema) can now find and evict singular cache entries too.
  • Added regression tests proving both behaviors: flush/1 on a struct evicts collection caches, and singular lookups are tracked under the per-type set for flush(_, :new_schema) eviction.

Downstream ticket: QRK-346

Test plan

  • Regression test: flush/1 on a struct evicts collection caches tracked under per-type key set
  • Regression test: singular cache entries are tracked under per-type key set and evicted by flush(_, :new_schema)
  • Updated lifecycle and concurrent tests to reflect new create/1 behavior (singular entries now evicted alongside collections)
  • All non-Redis tests pass (100/100)
  • mix format clean
  • mix credo --strict clean (0 issues)

🤖 Generated with Claude Code

Previously, flush(struct) only evicted cache keys tracked under the
per-record reference set (__set:Schema:<pk>), missing entries tracked
under the per-type set (__set:Schema). This meant collection queries
survived per-record flush, and single-record lookups survived per-type
flush. Callers had to know the internal tracking split and call both
flush(struct) and flush(struct, :new_schema) to fully invalidate.

Two changes:
1. get_set_value/3 now calls associate_key_reference_with_schema_type
   for single-record ({:ok, value}) results, not just lists. This
   ensures per-type flush can find single-record cache entries.
2. flush/1 (without :new_schema) now evicts both the per-record AND
   per-type key sets, so a single call fully invalidates all cache
   entries referencing the schema.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

@codecov codecov Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code has been improved for flushing and associating different types of cache keys. The changes follow the guidelines of DRY (Don't Repeat Yourself) which is great but there are still some improvements and clarifications needed regarding the implementation of each key type.

Comment thread lib/schema_cache.ex
{:ok, value} ->
Adapter.put(adapter(), cache_key, value, ttl)
associate_key_reference_with_schema(cache_key, value)
associate_key_reference_with_schema_type(cache_key, value)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addition of 'associate_key_reference_with_schema_type' here seems redundant. The same operation of associating a key reference with a schema type is also performed below. You might want to clarify its necessity here or consider removing this line.

Comment thread lib/schema_cache.ex
@@ -458,10 +461,12 @@ defmodule SchemaCache do
evict_reference_keys("#{schema_type}")
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes in the function parameters may cause bugs when existing code calls this method. Could you check whether %{}=schema has the same functionality as the previous version of this line (%schema_type{} = schema)?

Comment thread lib/schema_cache.ex
@@ -534,6 +539,19 @@ defmodule SchemaCache do
)
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This private function looks fine, but can we improve the performance and readability of this function by avoiding the use of pipelining?

@@ -658,6 +654,53 @@ defmodule SchemaCacheTest do
assert :ok = SchemaCache.flush(user)
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case is comprehensive, but it seems quite large and could be broken down into smaller pieces. Have you considered this?

@@ -925,13 +968,16 @@ defmodule SchemaCacheTest do
new_user = make_user(3, "charlie")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add further comments in the test case, especially to describe the use of each assertion in this part. This will help another reader to understand the logic better.

Comment thread lib/schema_cache.ex
@@ -534,6 +539,19 @@ defmodule SchemaCache do
)
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The '&Adapter.sadd' method returns some value, but we are currently ignoring this return value. It might be more appropriate to use this value, instead of just returning ':ok' in the next line, especially if it returns error status or extra information that could be helpful for diagnosis.

@codecov

codecov Bot commented Mar 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.10%. Comparing base (5b70a10) to head (2fbe2ae).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main       #6      +/-   ##
==========================================
+ Coverage   94.58%   95.10%   +0.51%     
==========================================
  Files           7        7              
  Lines         240      245       +5     
==========================================
+ Hits          227      233       +6     
+ Misses         13       12       -1     
Files with missing lines Coverage Δ
lib/schema_cache.ex 96.26% <100.00%> (+1.16%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@BobbieBarker BobbieBarker deleted the fix/flush-evicts-both-key-sets branch June 19, 2026 04:32
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