Skip to main content

hydict

hydict is a dict-compatible hierarchical cache for Python. It uses local in-memory storage as L1 and can use Redis or Valkey as L2.

Version 0.5.3

Version 0.5.3 provides the synchronous L1/L2 cache API, invalidation, metrics, per-key stampede protection, database-friendly serialization, and optional JSON formatting for API responses.

Features

  • Fast, local L1 in-memory cache
  • Optional Redis/Valkey L2 cache
  • L1 promotion after an L2 cache hit
  • TTL support, including TTL preservation during L2-to-L1 promotion
  • LRU eviction with an optional max_entries limit
  • Redis connection pooling
  • Thread-safe L1 operations
  • Cache invalidation
  • Cache statistics
  • get_or_set() with per-key locking to reduce cache stampedes
  • Python results with optional .json(indent=4, pretty=True) formatting
  • Serialization for common SQL values such as Decimal, date/time values, timedelta, and bytes

Quick start

from hydict import HDict

cache = HDict(max_entries=100)

cache["name"] = "Veeresh"
assert cache["name"] == "Veeresh"

Use set() when a value needs a TTL:

cache.set("session:1", {"user_id": 1}, ttl=60)

Redis or Valkey backend

Pass RedisConfig to enable L2 storage. A compatible Redis or Valkey server must be available at the configured address.

from hydict import HDict, RedisConfig

cache = HDict(
    RedisConfig(
        host="localhost",
        port=6379,
        max_connections=10,
    )
)

cache.set("user:1", {"name": "Veeresh"}, ttl=300)

Reads check L1 first. On an L1 miss, HDict checks L2 and promotes an L2 hit into L1.

Loading missing values

get_or_set() returns an existing value when present. For a missing key, it calls the loader, stores the returned value, and returns it. Concurrent callers for the same key share a per-key lock, so only one loader call populates that key at a time.

user = cache.get_or_set(
    "user:1",
    lambda: load_user_from_database(),
    ttl=60,
)

# Native Python mapping with optional API JSON formatting.
json_response = user.json(indent=4, pretty=True)

Invalidation and metrics

cache.invalidate("user:1")

print(cache.stats())
# {
#     "l1_hits": 0,
#     "l2_hits": 0,
#     "total_hits": 0,
#     "misses": 0,
#     "loader_calls": 0,
# }

invalidate() removes the key from L1 and, when configured, L2.

Development

Run the test suite:

python -m pip install -e .
pytest -q

Redis integration tests are opt-in. Set HYDICT_REDIS_INTEGRATION=1 before running the integration test suite with a Redis server available.

Roadmap

The next planned release is 0.6.0 (V6), which will introduce an asynchronous API for asyncio applications. Async support is not part of version 0.5.3.

See docs/architecture.md for cache behavior and design notes, the developer guide for API and implementation details, and CHANGELOG.md for release history.

Metadata

Release files for hydict 0.5.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hydict 0.5.3
File Size Uploaded Provenance Actions
hydict-0.5.3.tar.gz 6.1 kB Without provenance Details for hydict-0.5.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hydict 0.5.3
File Interpreter ABI Platform Actions
hydict-0.5.3-py3-none-any.whl Python 3 none any Details for hydict-0.5.3-py3-none-any.whl

Total release size: 17.8 kB

Release files / hydict-0.5.3.tar.gz

Download URL hydict-0.5.3.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
310098c8192e421956ba28deb74db768aad633f5f184446bb5fb105ad0c3909b
BLAKE2b-256 checksum
How to use checksums
4fafdef75ce19398e60ce04ab1d3924aef822525e7739a087fd774427527014c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Release files / hydict-0.5.3-py3-none-any.whl

Download URL hydict-0.5.3-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
abd36be8c9dcf91e1172b51a445a990e66633c96ab194b81fe0890c5e8173d10
BLAKE2b-256 checksum
How to use checksums
5014ebd2e20db2a9ef76da59264d1e11ca3bff2b5dc1c271238e692226bf9bfb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Security for hydict 0.5.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page