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

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.1
File Size Uploaded Provenance Actions
hydict-0.5.1.tar.gz 5.4 kB Without provenance Details for hydict-0.5.1.tar.gz

Built distribution (wheel)

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

Total release size: 16.2 kB

Release files / hydict-0.5.1.tar.gz

Download URL hydict-0.5.1.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8b9423d1f6ea2b6aced08e0bd3e188dfebc5982aa4b09b4b59969dc606506799
BLAKE2b-256 checksum
How to use checksums
f2623c0fe04281ade5e785d071bbe757e84223fa844bcf3f8694079c29d5152c
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.1-py3-none-any.whl

Download URL hydict-0.5.1-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
93e30806f0d4e74188e1610a121d42fdd699ae6b496938fd4f9b0e4b54fac877
BLAKE2b-256 checksum
How to use checksums
58d546fd395f820b0f63f8a44e2687b8b8e64456bd4015667d205a8e796fe619
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.1

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