Skip to content

feat: add CacheManager.add() for atomic store-if-absent - #83

Closed
HarshRajSinghania wants to merge 1 commit into
allen0099:masterfrom
HarshRajSinghania:feat/cache-manager-add
Closed

HarshRajSinghania wants to merge 1 commit into
allen0099:masterfrom
HarshRajSinghania:feat/cache-manager-add

Conversation

@HarshRajSinghania

Copy link
Copy Markdown

Summary

Add CacheManager.add(key, value, ttl=None) -> bool so application code can store a JSON-serializable value only when the key is still free.

Motivation

Requested in #65. CacheManager already exposes get / set / delete / has / get_or_set, and backends already implement atomic set_if_absent (#62). This is the application-level wrapper for once-only writes (webhook/email dedup, first-occurrence recording, idempotency keys).

Implementation

  • Same key prefix, JSON encoding, fingerprinting, and default_ttl handling as set().
  • Delegates to backend.set_if_absent (atomic on Memory, Redis, and Memcached).
  • Non-JSON-serializable values raise TypeError, matching set().
  • README example and Unreleased changelog entry.

Testing

Added tests in tests/test_cache_manager.py:

  • store when absent
  • do not overwrite an existing key
  • honor default_ttl
  • raise TypeError for non-JSON values

The environment used for this change could not install FastAPI from the configured PyPI mirror (502s), so the full pytest suite was not run here. add() was checked against MemoryBackend with a stubbed FastAPI import; all four behaviors above passed.

Closes #65

Implements allen0099#65. add() uses the same prefix, JSON encoding and default_ttl as set(), and delegates to backend.set_if_absent.

Signed-off-by: Harsh Raj Singhania <harshrajsinghania@proton.me>
@allen0099

Copy link
Copy Markdown
Owner

Thanks for the PR. #65 has been implemented in #93, together with a few follow-ups (a concurrency test, the shared encoding helper, and the docs that moved to docs/APP_CACHE.md since this branch was cut), so I'm closing this one.

@allen0099 allen0099 closed this Sep 25, 2026
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.

Add CacheManager.add() for atomic store-if-absent of application values

2 participants