Skip to content

Fix insert_new not unwrapping %ConCache.Item{} - #86

Open
hikmetba-bit wants to merge 1 commit into
sasa1977:masterfrom
hikmetba-bit:fix/insert-new-concache-item
Open

hikmetba-bit wants to merge 1 commit into
sasa1977:masterfrom
hikmetba-bit:fix/insert-new-concache-item

Conversation

@hikmetba-bit

Copy link
Copy Markdown

Summary

Fixes #84.

dirty_put has two clauses: one that matches %ConCache.Item{ttl:, value:} and unwraps it into perform_put(cache, key, value, ttl), and a fallback for a plain value that uses the cache's default ttl. dirty_insert_new had no such split — it stored whatever was passed straight into ETS, so an %ConCache.Item{} ended up cached as the raw struct instead of its .value, and its .ttl was silently ignored in favor of the cache's default ttl.

ConCache.put(cache, 1, %ConCache.Item{value: %{a: 1}, ttl: :timer.seconds(60)})
ConCache.get(cache, 1)  # => %{a: 1}                          (correct)

ConCache.insert_new(cache, 2, %ConCache.Item{value: %{a: 1}, ttl: :timer.seconds(60)})
ConCache.get(cache, 2)  # => %ConCache.Item{value: %{a: 1}, ...}   (wrong)

Fix

Mirrors dirty_put's two-clause pattern for dirty_insert_new via a new perform_insert_new/4, so ConCache.Item behaves the same way for both functions.

Testing

  • Added a regression test asserting insert_new with a %ConCache.Item{} unwraps to the plain value.
  • Verified the new test fails against the code without this fix (gets the raw struct back), and passes with it.
  • mix test test/con_cache_test.exs: 54/60 passing both before and after this change — the 6 remaining failures are pre-existing on unmodified master too (timing-sensitive ttl-expiry tests using ~10-40ms sleeps, flaky in this sandboxed environment; confirmed identical failure set on master).
  • mix compile --warnings-as-errors: no new warnings from the changed file (the one pre-existing warning, unused require Logger in lib/con_cache.ex, is unrelated and untouched by this change).

🤖 Generated with Claude Code

dirty_put has two clauses: one that matches %ConCache.Item{ttl:, value:}
and unwraps it into perform_put(cache, key, value, ttl), and a fallback
for a plain value that uses the cache's default ttl. dirty_insert_new
had no such split -- it stored whatever was passed straight into ETS,
so an %ConCache.Item{} ended up as the raw struct instead of its
.value, and its .ttl was silently ignored in favor of the cache
default.

Mirror dirty_put's two-clause pattern for dirty_insert_new via a new
perform_insert_new/4, so ConCache.Item behaves the same way for both
functions.

Fixes sasa1977#84.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.

insert_new not compatible ConCache.Item

1 participant