Skip to main content

Fragmented Key Management and Invalidation Library for use with Redis

Project description

fragmented-keys

Fragmented Key Management and Invalidation Library for use with Redis.

A Python port of noizu-labs/fragmented-keys (PHP), adapted to use Redis as the cache backend.

Overview

Fragmented Keys enable efficient cache invalidation by composing cache keys from multiple independently versioned tags. Instead of deleting cached entries individually, you increment a tag's version — all keys that depend on that tag automatically resolve to a new cache key, leaving the old entries to expire naturally.

Cache Key = md5( base_key + tag1:version1 + tag2:version2 + ... )

When any tag version changes, the resulting key changes, producing a cache miss and triggering a fresh data fetch.

Installation

uv add fragmented-keys

Or with pip:

pip install fragmented-keys

Quick Start

from redis import Redis
from fragmented_keys import Configuration, RedisHandler, StandardTag, StandardKey

# Setup
client = Redis(host="127.0.0.1", port=6379)
Configuration.set_default_cache_handler(RedisHandler(client))
Configuration.set_global_prefix("MyApp")

# Create tags
user_tag = StandardTag("User", "42")
city_tag = StandardTag("City", "chicago")

# Build a composite key
key = StandardKey("UserDashboard", [user_tag, city_tag])
cache_key = key.get_key_str()  # MD5 hex digest

# Use it with Redis
data = client.get(cache_key)
if data is None:
    data = fetch_from_database()
    client.set(cache_key, data)

# Invalidate all keys depending on User:42
user_tag = StandardTag("User", "42")
user_tag.increment()
# Next call to get_key_str() with User:42 will produce a different key

Tag Types

StandardTag

Version is stored in the cache backend and can be incremented. Each increment changes the version by +0.1, invalidating all dependent keys.

tag = StandardTag("User", "42")
tag.get_tag_version()   # Fetches or seeds version from cache
tag.increment()         # Version += 0.1, persisted to cache
tag.reset_tag_version() # Resets to a new time-based value

ConstantTag

Version is fixed at construction time. Mutations are no-ops. Useful for incorporating static dimensions into composite keys.

tag = ConstantTag("ApiVersion", "v2", version=2.0)
tag.increment()         # No-op
tag.get_tag_version()   # Always 2.0

Cache Handlers

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()

Custom Handlers

Implement the CacheHandler protocol:

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]: ...

KeyRing

FragmentedKeyRing is a factory for defining reusable key templates and producing configured StandardKey instances.

from redis import Redis
from fragmented_keys import FragmentedKeyRing, RedisHandler

redis_handler = RedisHandler(Redis())
memory_handler = MemoryHandler()

ring = FragmentedKeyRing(
    global_options={"type": "standard"},
    global_tag_options={
        "universe": {"type": "constant", "version": 1.0},
    },
    default_cache_handler="redis",
    cache_handlers={
        "redis": redis_handler,
        "memory": memory_handler,
    },
    default_prefix="MyApp",
)

# Define key templates
ring.define_key("Users", [
    "universe",
    {"tag": "planet", "cache_handler": "memory"},
    "city",
])

# Get a configured key object
key_obj = ring.get_key_obj("Users", ["MilkyWay", "Earth", "Chicago"])
cache_key = key_obj.get_key_str()

# Or use dynamic accessor
key_obj = ring.get_users_key_obj("MilkyWay", "Earth", "Chicago")

Tag Options

Options are merged in order: global options → per-tag options → per-key overrides.

Option Type Description
type "standard" | "constant" Tag type (default: "standard")
version float Initial version value
cache_handler str Handler key from cache_handlers dict
prefix str Override global prefix for this tag

Configuration

Global defaults shared across all tags and keys:

from fragmented_keys import Configuration

Configuration.set_default_cache_handler(handler)  # Fallback handler
Configuration.set_global_prefix("MyApp")           # Key namespace prefix

How It Works

  1. Each StandardTag stores its current version in the cache backend under the key {tag}_{instance}{prefix}.
  2. When building a composite key, StandardKey bulk-fetches all tag versions using get_multi (grouped by handler) for efficiency. ConstantTag instances are skipped in bulk fetches since their version is fixed.
  3. The final key is md5("{keyName}_{groupId}:t{tag1Name}:v{version1}:t{tag2Name}:v{version2}...").
  4. Calling tag.increment() bumps the stored version by 0.1, so subsequent key builds produce a different MD5 — effectively invalidating all cached data under the old key without deleting it.

Development

# Install with dev dependencies
uv sync --extra dev

# Run tests
uv run pytest tests/ -v

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fragmented_keys-0.1.4.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fragmented_keys-0.1.4-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file fragmented_keys-0.1.4.tar.gz.

File metadata

  • Download URL: fragmented_keys-0.1.4.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fragmented_keys-0.1.4.tar.gz
Algorithm Hash digest
SHA256 2a4b42939e6fff8a82c8c4c55790f5541f5dd28c22e2722ea018093b7c17458d
MD5 927b2cf392a0e769857a1b2e10a16735
BLAKE2b-256 d3fafa830ba73730379d0ff67dcc996d4b1c706d4aa8f7d9d1647f5effc55e4a

See more details on using hashes here.

File details

Details for the file fragmented_keys-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: fragmented_keys-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fragmented_keys-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1f80d62594a9f9326d230b4d4fa656dc8664be1cb452e6e884c6b4ff15cf3df7
MD5 a4d34e9baf49052fa0742ef3c45f12f9
BLAKE2b-256 7e7d1bc26ec1a33e837599c70bec2e877f3b816d50b3c34212996206b37c742a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page