Skip to main content

Async caching library with Redis, Memory, L1/L2, AutoSerializer (orjson/msgspec)

Project description

Light Cache

Light Cache é uma biblioteca Python para caching assíncrono com suporte a Redis, memória, L1/L2, decorators fáceis, orjson e msgspec.
Projetada para FastAPI, Granian, ML/BRMS e qualquer aplicação async Python.


🔹 Recursos principais

  • Cache assíncrono com múltiplos backends:
    • Redis (com senha e TLS)
    • Memória local (L1)
    • Cache em camadas (LayeredCache: L1 + L2)
  • Decorator @cache_response para funções async
  • Serializadores rápidos:
    • AutoSerializer (orjson)
    • MsgspecSerializer (msgspec.Struct)
  • Dogpile protection (evita múltiplas execuções pesadas)
  • TTL com jitter (evita stampede)
  • Métricas simples: hits / misses
  • Suporta versionamento de chaves (key_version)
  • Seguro: falhas no cache não quebram a aplicação

🔹 Instalação

pip install async_cache

Exemplo

import msgspec

from fastapi import FastAPI
import asyncio
import random

import uvicorn

class QuoteResult(msgspec.Struct):
    product_id: str
    price: float
    discount: float

from light_cache.backends import (
    MemoryCacheBackend,
    RedisCacheBackend,
    LayeredCacheBackend,
)
from light_cache.core.decorators import cache_response
from light_cache.core.serializers import MsgspecSerializer    

memory_cache = MemoryCacheBackend()
redis_cache = RedisCacheBackend(
    redis_url="redis://localhost:6379/0",
    prefix="fastapi-demo",
)

cache = LayeredCacheBackend(
    l1=memory_cache,
    l2=redis_cache,
    l1_ttl=2,
    )

    
app = FastAPI(title="Async Cache Demo")    

@app.get("/health")
@cache_response(
    cache=cache,
    key_prefix="health",
    ttl=60,
    use_lock=False,
)
async def health():
    return {
        "status": "ok",
        "value": random.random(),
    }
    
    
@app.get("/quote/{product_id}")
@cache_response(
    cache=cache,
    key_prefix="quote",
    ttl=60,
    serializer=MsgspecSerializer(),
)
async def calculate_quote(product_id: str):
    # simula cálculo pesado
    await asyncio.sleep(1)

    return QuoteResult(
        product_id=product_id,
        price=round(random.uniform(100, 500), 2),
        discount=round(random.uniform(0, 0.3), 2),
    )

    
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)
    

Project details


Download files

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

Source Distribution

light_cache_async-0.1.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

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

light_cache_async-0.1.0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file light_cache_async-0.1.0.tar.gz.

File metadata

  • Download URL: light_cache_async-0.1.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for light_cache_async-0.1.0.tar.gz
Algorithm Hash digest
SHA256 eb406ef89a41727c5e93c6f108d2665191123cddd24814282edf01b7e175c488
MD5 64f4b6025c07dec9f8c347f1804ccb76
BLAKE2b-256 680789cccdcd185d1450b23b90a8e57a12019a2a800cbcc6807472fc4bc740c7

See more details on using hashes here.

File details

Details for the file light_cache_async-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for light_cache_async-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e3350dc54b0f3296492c7e87a5368dd100257cba9f743b590fa64ff857ceb185
MD5 16065c4662fd37fbc01b3d30bb74d9fc
BLAKE2b-256 b23e52fe20a2f79341c4e7315723257c4cb2e1332035053606910e91ce8b4124

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page