Skip to main content

A flexible async/sync Redis cache with tag-based invalidation support for FastAPI, Pydantic

Project description

Tagged Cache

A flexible async/sync Redis cache with tag-based invalidation support for FastAPI, Pydantic

Installation

pip install tagged-cache

Example

import asyncio
from fastapi import FastAPI
from redis.asyncio import Redis
from pydantic import BaseModel
from tagged_cache import TaggedCache
from contextlib import asynccontextmanager

redis_client = Redis(host="localhost", port=6379, decode_responses=True)
cache = TaggedCache(redis_client=redis_client)


@asynccontextmanager
async def lifespan(_: FastAPI):
    yield
    redis_client.close()

app = FastAPI(title="TaggedCache Base Test", lifespan=lifespan)


class TestModel(BaseModel):
    message: str
    counter: int

request_counter = 0

@app.get("/test-cache/{item_id}", response_model=TestModel)
@cache.cache(ttl=60, tags=["item:{item_id}"])
async def get_cached_item(item_id: int):
    global request_counter
    request_counter += 1

    await asyncio.sleep(1.0)
    
    return TestModel(
        message=f"Item {item_id} data fetched from heavy database", 
        counter=request_counter
    )

@app.post("/test-invalidate/{item_id}")
async def invalidate_item_cache(item_id: int):
    await cache.invalidate_tag(f"item:{item_id}")
    return {"status": f"Cache for item:{item_id} successfully invalidated"}

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

tagged_cache-0.1.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

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

tagged_cache-0.1.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tagged_cache-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2b514f75a035bb878b587e4548e3d5f4d1787f4bbd5c07f44d1f0e58dbfa5ffe
MD5 738b7dad7b99df0c2d41558a25ce5207
BLAKE2b-256 aa00c15fa5a6ec013068de2518376c5df582120041aaa3ca8890108c23cd5c87

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tagged_cache-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for tagged_cache-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 41bbbaf47d8a62974e9b797c41a5f22cce2c8920f0bb287a5e792febc460f3b3
MD5 39f7ff3d1c72bfb2d81eb9404f572ed9
BLAKE2b-256 95ad0a8ea5531016eb7a1e5523713501f6eb3c73f38d7de753a527c86b0ce861

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