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.0

Version 0.5.0 is the V5 release. It includes the synchronous cache API, invalidation, metrics, and per-key stampede protection. V5 release verification recorded 82 passing tests.

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

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

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.

Continuous integration and releases

GitHub Actions runs unit tests for pull requests and runs the complete pytest -q suite against Redis 7 with HYDICT_REDIS_INTEGRATION=1 for release validation. Publishing runs only for version tags such as v0.5.0, and only after both test jobs succeed. After a successful PyPI upload, the workflow also creates a GitHub Release with generated notes and the wheel/source archive attached.

To enable publishing, create the PYPI_TOKEN repository secret with a PyPI API token. The workflow passes it to Twine without placing the token in the repository or workflow logs.

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.0.

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.0

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.0
File Size Uploaded
hydict-0.5.0.tar.gz 5.5 kB Details

Built distribution (wheel)

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

Total release size: 16.4 kB

Release files / hydict-0.5.0.tar.gz

Download URL hydict-0.5.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
745aa2016bc07531f51d46eb95cdfd0b01373c853f1b2a923a5b3983665ba20c
BLAKE2b-256 checksum
How to use checksums
d87669deae763f9a84027892661e5f9e7a09acb248dabc60a4b04e8bc1ffb7cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

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

Download URL hydict-0.5.0-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3b6075564e13a5e955e544497bb297e4a39f55f2e418c8953385a5b3149272f7
BLAKE2b-256 checksum
How to use checksums
8ab1178a8472c5267910e2922b210cb32af2b8357037f11b2ad6af37c2a4c2a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

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