Skip to content

feat(backends)!: make delete() return whether a key was removed - #427

Merged
allen0099 merged 1 commit into
masterfrom
feat/71-delete-returns-bool
Oct 2, 2026
Merged

allen0099 merged 1 commit into
masterfrom
feat/71-delete-returns-bool

Conversation

@allen0099

Copy link
Copy Markdown
Owner

Closes #71.

BaseCacheBackend.delete() returns whether a key was removed, instead of None.

Changes

  • Abstract signature: async def delete(self, key: str) -> bool.
  • Memory: True when the key held an entry that had not expired. An expired entry is still dropped but counts as absent, matching _evict (delete_many, clear_pattern, clear_path) and Redis DEL.
  • Redis: the DEL count, as a bool.
  • Memcached: delete(noreply=False)'s answer. clear_path() now reuses delete() instead of duplicating it.
  • Base fallbacks for third-party backends:
    • delete_many() counts only the keys that existed, not the ones attempted.
    • get_and_delete() and delete_if_equals() use the result, so a caller whose delete found the key already gone no longer also wins. They are still non-atomic, as documented.
  • Changelog fragment changelog.d/71.changed.md. The migration section MIGRATING_0_4.md#backend-delete (EN and zh-TW) already describes this change and is unchanged.

Tests

  • Each backend's own test file asserts True on the first delete and False on the second. Memory also checks that deleting an expired entry returns False.
  • A stubbed Memcached test pins noreply=False and the namespaced key, so it runs without a server.
  • test_base.py: the delete_many fallback count, and the fallbacks losing a race between their get and delete.
  • Ran the full suite against live Redis and Memcached as well as without them.
  • Mutation-checked: making each backend return True unconditionally, restoring the old fallbacks, or dropping noreply=False each fails the tests that guard it.

An independent review agent reviewed the change before this PR. It found nothing blocking; its optional suggestions (stubbed Memcached test, fallbacks using the result, clear_path reuse) are included.

BaseCacheBackend.delete() now returns bool on memory, Redis and
Memcached. The base delete_many() fallback counts only keys that
existed, and the get_and_delete() / delete_if_equals() fallbacks no
longer let a caller win after its delete found the key gone.
@allen0099 allen0099 added this to the 0.4.0 milestone Oct 2, 2026
@allen0099 allen0099 added enhancement New feature or request backends Cache backends and their atomic primitives breaking-change Changes public behaviour or API; needs a minor/major release labels Oct 2, 2026
@allen0099
allen0099 merged commit 4239688 into master Oct 2, 2026
15 checks passed
@allen0099
allen0099 deleted the feat/71-delete-returns-bool branch October 2, 2026 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backends Cache backends and their atomic primitives breaking-change Changes public behaviour or API; needs a minor/major release enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0.4.0: make BaseCacheBackend.delete() return whether a key was removed

1 participant