Skip to content

Cache Invalidation & TTL Strategy #186

@mftee

Description

@mftee

Extend the CacheClient with a proper cache invalidation strategy, including support for tagged invalidation groups and configurable TTL policies per data type.

Requirements:

  • Add the following methods to CacheClient:
    • set_with_tags(key: &str, value: &T, ttl: u64, tags: &[&str]) -> Result<()> — stores the value and also adds the key to a Redis set for each tag (tag:<name> → set of keys)
    • invalidate_by_tag(tag: &str) -> Result<usize> — deletes all keys associated with the tag and removes the tag set; returns the count of deleted keys
    • delete(key: &str) -> Result<()> — delete a single key
    • extend_ttl(key: &str, additional_seconds: u64) -> Result<()> — extend expiry of an existing key
    • get_ttl(key: &str) -> Result<Option<u64>> — return remaining TTL in seconds
  • Define a CacheTtl constants module:
    pub mod cache_ttl {
        pub const VERIFICATION_RESULT: u64 = 3600;  // 1 hour
        pub const TRANSFER_HISTORY: u64 = 300;       // 5 minutes
        pub const HASH_HISTORY: u64 = 300;           // 5 minutes
    }
  • Use tag document:<hash> for all cache entries related to a document hash so they can be bulk-invalidated

Acceptance Criteria:

  • invalidate_by_tag deletes all keys tagged with the given tag in a single operation
  • extend_ttl on a non-existent key is a no-op (does not create the key)
  • Unit tests using a mock Redis (or an actual embedded Redis in tests)
  • All existing call sites updated to use the new set_with_tags and the constants

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions