Skip to main content

lexigram-cache

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


Overview

Multi-backend async caching for the Lexigram 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.lexigram.dev

Install

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

Quick Start

from lexigram import Application
from lexigram.di.module import Module, module
from lexigram.cache import CacheModule, CacheConfig
from lexigram.cache.config import CacheBackendConfig
from lexigram.cache.types import BackendType
from lexigram.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

Option 2 — Profiles + Environment Variables (recommended)

export LEX_CACHE__ENABLED=true
export LEX_CACHE__BACKENDS__0__TYPE=redis
export LEX_CACHE__BACKENDS__0__REDIS_URL=redis://localhost:6379/0

Option 3 — Python

from lexigram.cache import CacheModule, CacheConfig
from lexigram.cache.config import CacheBackendConfig
from lexigram.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 LEX_CACHE__ENABLED Enable the cache module
backends[].name LEX_CACHE__BACKENDS__0__NAME Unique backend name
backends[].type LEX_CACHE__BACKENDS__0__TYPE Backend type: redis, memory, memcached
backends[].redis_url LEX_CACHE__BACKENDS__0__REDIS_URL Redis connection URL
backends[].default_ttl null LEX_CACHE__BACKENDS__0__DEFAULT_TTL Default TTL in seconds
service.enable_protection True LEX_CACHE__SERVICE__ENABLE_PROTECTION Stampede protection
service.circuit_breaker_enabled False LEX_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 LEX_ENV=production

Testing

from lexigram.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/lexigram/cache/module.py CacheModule with configure() and stub()
src/lexigram/cache/config.py CacheConfig, CacheBackendConfig, CacheServiceConfig
src/lexigram/cache/di/provider.py CacheProvider boot and registration
src/lexigram/cache/decorators.py @cacheable decorator
src/lexigram/cache/backends/ Redis, in-memory, and Memcached implementations

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lexigram_cache-0.1.5006.tar.gz (170.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lexigram_cache-0.1.5006-py3-none-any.whl (140.1 kB view details)

Uploaded Python 3

File details

Details for the file lexigram_cache-0.1.5006.tar.gz.

File metadata

  • Download URL: lexigram_cache-0.1.5006.tar.gz
  • Upload date:
  • Size: 170.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.14

File hashes

Hashes for lexigram_cache-0.1.5006.tar.gz
Algorithm Hash digest
SHA256 fbff883697dd212979ffd2eb5ec7f9907e447f618a39b76a31fac1d49d4ca3fc
MD5 da3aa7bcbeb40280f925ee8044310957
BLAKE2b-256 e8bda4404e8c0789afd5ab597b8eb89646d9104e6ce3ed545a45d17b3308cc77

See more details on using hashes here.

File details

Details for the file lexigram_cache-0.1.5006-py3-none-any.whl.

File metadata

File hashes

Hashes for lexigram_cache-0.1.5006-py3-none-any.whl
Algorithm Hash digest
SHA256 890605445633713ce17b6bf3c0874afb979edb5c877b2b5f90b22460e816204c
MD5 b93bde5525c7559fbe2179a81d1be8e5
BLAKE2b-256 44715c6ae56769aae6482850a6db4edb150673c962f759b8e6dc6ca817e26bf7

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.5009

1 file

This release

0.1.5006 This release

2 files

0.1.5001

2 files

0.1.3007

1 file

0.1.3006

1 file

0.1.3005

1 file

0.1.4

2 files

0.1.2

1 file

0.1.0

2 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