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.

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

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.2
File Size Uploaded Provenance Actions
hydict-0.5.2.tar.gz 5.5 kB Without provenance Details for hydict-0.5.2.tar.gz

Built distribution (wheel)

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

Total release size: 16.5 kB

Release files / hydict-0.5.2.tar.gz

Download URL hydict-0.5.2.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a6ec3baab56f5423ffadcfb4afb49399c0286fbae4ed17a9337dcfba9713cb03
BLAKE2b-256 checksum
How to use checksums
4bfd93514d763c0cc0120d46287b51110554876e0cfde6a23a44b640efbc48e4
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.2-py3-none-any.whl

Download URL hydict-0.5.2-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
78f6abafa297a78a83aef4df45dd2787d0eb74847bbc7176704367e234261c6e
BLAKE2b-256 checksum
How to use checksums
b3e1adc21093461d72f3cf295bcdb81ddd2707cbefa6bd41c4732716205e1b1c
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.2

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