Cache Handlers

Cache handlers are the backend storage for tag versions. They implement the CacheHandler protocol.

RedisHandler

Primary backend using redis-py. Supports mget for bulk version fetches and setex for TTL.

from redis import Redis
from fragmented_keys import RedisHandler

handler = RedisHandler(Redis(host="127.0.0.1", port=6379))

MemoryHandler

In-memory dict-based handler for testing.

from fragmented_keys import MemoryHandler

handler = MemoryHandler()
handler.clear()  # Reset all cached values

Custom Handlers

Implement the CacheHandler protocol to create a custom backend:

from fragmented_keys.protocols import CacheHandler

class MyHandler:
    def group_name(self) -> str:
        return "MyHandler"

    def get(self, key: str) -> str | None:
        ...

    def set(self, key: str, value: str, ttl: int | None = None) -> None:
        ...

    def get_multi(self, keys: list[str]) -> dict[str, str]:
        ...

CacheHandler Protocol

Method

Description

group_name()

Return a unique identifier for this handler type. Used to group tags for bulk get_multi calls.

get(key)

Fetch a single value by key. Returns None on miss.

set(key, value, ttl=None)

Store a value, optionally with a TTL in seconds.

get_multi(keys)

Fetch multiple values at once. Returns dict of found key-value pairs.