Skip to main content

Deterministic Redis key builder for cache-aside CRUD with cursor pagination

Project description

biplex-utils-cache-key-builder

Deterministic Redis key builder for cache-aside CRUD with cursor pagination.

Installation

pip install biplex-utils-cache-key-builder

Usage

from biplex_utils_cache_key_builder import CacheKeyBuilder

kb = CacheKeyBuilder(tenant="myapp", service="orders")

Single Entity Key

Cache a single entity (e.g., for GET by ID operations):

key = kb.entity_key(
    entity="order",
    company_id="company-123",
    entity_id="order-456"
)
# Result: "myapp:orders:order:item:c:a1b2c3d4e5f6:order-456"

List Version Key

Track cache version for list invalidation:

key = kb.list_version_key(
    entity="order",
    companies_ids=["company-123", "company-456"]
)
# Result: "myapp:orders:order:list:c:b2c3d4e5f6a7:version"

List Cursor Key

Cache paginated list results with cursor-based pagination:

key = kb.list_cursor_key(
    entity="order",
    companies_ids=["company-123"],
    version=1,
    filters={"status": "active"},
    sort=[{"field": "created_at", "direction": "desc"}],
    cursor=None,  # First page
    limit=20
)
# Result: "myapp:orders:order:list:c:a1b2c3d4e5f6:v1:c3d4e5f6a7:start:20"

# For subsequent pages, pass the cursor:
key = kb.list_cursor_key(
    entity="order",
    companies_ids=["company-123"],
    version=1,
    filters={"status": "active"},
    sort=[{"field": "created_at", "direction": "desc"}],
    cursor={"id": "order-100", "created_at": "2024-01-15T10:00:00Z"},
    limit=20
)

Key Features

  • Deterministic: Same inputs always produce the same key
  • Order-independent: Company IDs and dict keys are normalized before hashing
  • Multi-tenant: Keys include tenant and service prefixes
  • Cursor pagination: Built-in support for cursor-based pagination
  • Version tracking: List version keys for cache invalidation strategies

Key Format

Method Format
entity_key {tenant}:{service}:{entity}:item:c:{company_hash}:{entity_id}
list_version_key {tenant}:{service}:{entity}:list:c:{companies_hash}:version
list_cursor_key {tenant}:{service}:{entity}:list:c:{companies_hash}:v{version}:{query_hash}:{cursor_hash}:{limit}

Hash Lengths

Hash Type Length Used For
Company hash 12 chars Single/multiple company IDs
Query hash 10 chars Filters + sort parameters
Cursor hash 8 chars Pagination cursor

Requirements

  • Python 3.10+

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

biplex_utils_cache_key_builder-0.1.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file biplex_utils_cache_key_builder-0.1.0.tar.gz.

File metadata

File hashes

Hashes for biplex_utils_cache_key_builder-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4539b9d6177728fe82f7973a14eb9f56ddcf1503918a0e73def9981db0c7a1cd
MD5 01a5bcdb764f85ffe38922c46bf05422
BLAKE2b-256 28b86a722e1bcfd0adc33818f5aa7b54cc481013a54d44a1e4bfeb1ed2fb54ec

See more details on using hashes here.

File details

Details for the file biplex_utils_cache_key_builder-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for biplex_utils_cache_key_builder-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9b9005c407c53fddc53544d9e7d8722fcafb095ad6c3ae7e515c3d10fc2046c1
MD5 5c1886a33517b8861a72c6e8273ac861
BLAKE2b-256 20be20cbd83e6a1f0d64dc33f0986f0b9c592447ec7bc1090025f71c1ac53f9d

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