Skip to main content

quadkit-cache

Multi-backend caching system for Quadkit Framework — Redis, Memcached, and in-memory caching.


Overview

Multi-backend async caching for the Quadkit Framework. Supports Redis, in-memory, and Memcached backends with stampede protection, circuit breaker, Result-aware caching, and domain model round-trip serialization.

Configure backends via CacheModule.configure() and inject CacheBackendProtocol into any service. The @cacheable decorator provides cache-aside logic with automatic key generation and Result support.

Full documentation: docs.quadkit.dev

Install

uv add quadkit-cache
# Optional extras
uv add "quadkit-cache[redis,memcached,semantic]"

Quick Start

from quadkit import Application
from quadkit.di.module import Module, module
from quadkit.cache import CacheModule, CacheConfig
from quadkit.cache.config import CacheBackendConfig
from quadkit.cache.types import BackendType
from quadkit.contracts.infra.cache import CacheBackendProtocol


@module(
    imports=[
        CacheModule.configure(
            CacheConfig(
                backends=[
                    CacheBackendConfig(
                        name="default",
                        type=BackendType.REDIS,
                        default=True,
                        redis_url="redis://localhost:6379/0",
                    )
                ]
            )
        )
    ]
)
class AppModule(Module):
    pass


async def main():
    async with Application.boot(modules=[AppModule]) as app:
        cache = await app.container.resolve(CacheBackendProtocol)
        await cache.set("greeting", "hello", ttl=60)
        value = await cache.get("greeting")
        print(value.unwrap())  # "hello"


if __name__ == "__main__":
    import asyncio

    asyncio.run(main())

Configuration

Zero-config usage: Call CacheModule.configure() with no arguments; backends are then loaded from the cache: section of application.yaml. Use CacheModule.stub() (in-memory) in tests.

Option 1 — YAML file

# application.yaml
cache:
  enabled: true
  backends:
    - name: redis
      type: redis
      default: true
      redis_url: "redis://localhost:6379/0"
      default_ttl: 300
  service:
    enable_protection: true
export QK_CACHE__ENABLED=true
export QK_CACHE__BACKENDS__0__TYPE=redis
export QK_CACHE__BACKENDS__0__REDIS_URL=redis://localhost:6379/0

Option 3 — Python

from quadkit.cache import CacheModule, CacheConfig
from quadkit.cache.config import CacheBackendConfig
from quadkit.cache.types import BackendType

CacheModule.configure(
    CacheConfig(
        backends=[
            CacheBackendConfig(
                name="redis",
                type=BackendType.REDIS,
                default=True,
                redis_url="redis://localhost:6379/0",
            )
        ]
    )
)

Config reference

Field Default Env var Description
enabled True QK_CACHE__ENABLED Enable the cache module
backends[].name — QK_CACHE__BACKENDS__0__NAME Unique backend name
backends[].type — QK_CACHE__BACKENDS__0__TYPE Backend type: redis, memory, memcached
backends[].redis_url — QK_CACHE__BACKENDS__0__REDIS_URL Redis connection URL
backends[].default_ttl null QK_CACHE__BACKENDS__0__DEFAULT_TTL Default TTL in seconds
service.enable_protection True QK_CACHE__SERVICE__ENABLE_PROTECTION Stampede protection
service.circuit_breaker_enabled False QK_CACHE__SERVICE__CIRCUIT_BREAKER_ENABLED Circuit breaker on backend failures

Module Factory Methods

Method Description
CacheModule.configure(...) Configure with explicit CacheConfig
CacheModule.stub() In-memory backend for unit testing

Key Features

  • Multi-backend — Redis, in-memory, and Memcached with a unified protocol
  • Stampede protection — Distributed lock on cold reads prevents thundering herd
  • Circuit breaker — Opens on repeated backend failures, falls through to origin
  • @cacheable decorator — Cache-aside with automatic key generation and Result support
  • Domain model serialization — Type-tagged JSON envelope preserves type identity round-trip
  • Production security — Blocks insecure Redis passwords when QK_ENV=production

Testing

from quadkit.cache import CacheService

async with Application.boot(modules=[CacheModule.stub()]) as app:
    cache = await app.container.resolve(CacheService)
    await cache.set("key", "value", ttl=60)
    assert await cache.get("key") == "value"

Key Source Files

File What it contains
src/quadkit/cache/module.py CacheModule with configure() and stub()
src/quadkit/cache/config.py CacheConfig, CacheBackendConfig, CacheServiceConfig
src/quadkit/cache/di/provider.py CacheProvider boot and registration
src/quadkit/cache/decorators.py @cacheable decorator
src/quadkit/cache/backends/ Redis, in-memory, and Memcached implementations

Metadata

Release files for quadkit-cache 0.0.42

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

Source distribution (sdist)

Source distribution for quadkit-cache 0.0.42
File Size Uploaded Provenance
quadkit_cache-0.0.42.tar.gz 185.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for quadkit-cache 0.0.42
File Interpreter ABI Platform
quadkit_cache-0.0.42-py3-none-any.whl Python 3 none any Details

Total release size: 331.2 kB

Release files / quadkit_cache-0.0.42.tar.gz

Download URL quadkit_cache-0.0.42.tar.gz
Size 185.1 kB
Tags Source
SHA-256 checksum
How to use checksums
cdb0540e5ac330505796565bf3e8828ba2a43b603c75f1ffa5ef213f384d3940
BLAKE2b-256 checksum
How to use checksums
c0be4ee423ee4fae66fe8c5bd12a52e7df4501f735c54587b51ed93df09287f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.14

Release files / quadkit_cache-0.0.42-py3-none-any.whl

Download URL quadkit_cache-0.0.42-py3-none-any.whl
Size 146.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7d544b447e28067b8ae368f642708b7423b3ee0611088287245ca93564b31111
BLAKE2b-256 checksum
How to use checksums
4e1bd8b6af7fdbe1c3268ed303acc7850b9eb2b13e43b5bbbdea9576aa777604
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.14

Security for quadkit-cache 0.0.42

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.

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