Skip to content

v0.1.0 — Pre-Release

Pre-release
Pre-release

Choose a tag to compare

@JamshidS JamshidS released this 01 Feb 14:48
531e50c

📦 v0.1.0 — Pre-Release

🎯 Overview

fastapi-cacheable is a lightweight and ergonomic caching library for FastAPI that brings Spring-style cache decorators like @cacheable, @cache_put, and @cache_evict to the Python ecosystem. It ships with first-class async support and a Redis backend, allowing developers to declaratively cache expensive asynchronous operations and API results with minimal boilerplate.

Inspired by declarative caching patterns in frameworks like Spring Boot, this library makes it easy to implement read-through caching, manual cache updates, and explicit cache eviction in FastAPI apps. ([GitHub][1])


💡 Key Features

✅ Declarative Cache Decorators
Provides decorators that clearly express caching intent without cluttering business logic:

  • @cacheable: Read-through caching (returns cached value or computes and stores)
  • @cache_put: Always recompute and update cache
  • @cache_evict: Invalidate cache entries by key or namespace ([GitHub][1])

✅ Redis Backend (async)
Built-in support for Redis via redis.asyncio, making caching suitable for production workloads and high throughput scenarios. ([GitHub][1])

✅ FastAPI Friendly
Designed to integrate easily with FastAPI’s async ecosystem and request lifecycles. ([GitHub][1])

✅ Customizable Keying Strategy
Cache keys combine namespace, key, and a hash of function arguments for deterministic cache lookup. ([GitHub][1])


🚀 Get Started

pip install -e .
# or for extras
pip install -e ".[msgpack,pydantic]"
import redis.asyncio as redis
from fastapi import FastAPI
from fastapi_cacheable.backend.redis import RedisCacheBackend
from fastapi_cacheable import cacheable, cache_put, cache_evict, CacheConfig

app = FastAPI()

redis_client = redis.Redis.from_url("redis://localhost:6379/0")
CacheConfig.init(RedisCacheBackend(redis_client, key_prefix="myapp"))

@app.get("/users/{user_id}")
@cacheable(namespace="users", key="get_user", ttl=60)
async def get_user(user_id: int):
    # expensive computation or DB call
    return {"user_id": user_id, "name": f"user-{user_id}"}

🧠 How It Works

  • Cache keys are generated based on a combination of namespace, user-provided key, and hashed arguments. ([GitHub][1])
  • The Redis backend stores serialized values (default JSON). ([GitHub][1])
  • Decorators can control when a result is cached or evicted, enabling both read-through and write/update caching patterns. ([GitHub][1])

📣 Why This Matters

Caching is critical for performance in API-driven applications, reducing latency and resource usage for repeated queries. Most existing solutions for FastAPI provide response caching, but fastapi-cacheable focuses on function-level and domain-centric caching with explicit control patterns similar to mature caching abstractions in other ecosystems. ([GitHub][2])


📌 Release Notes

v0.1.0 (Pre-Release)

  • Initial implementation of core decorators
  • Redis async backend
  • Example FastAPI integration
  • No published package yet — source-install recommended

🗂 Example Projects & Docs

You can find usage examples in the examples/ folder of this repo. ([GitHub][1])


📄 License

This project is licensed under the MIT License, making it permissive for open-source and commercial usage. ([GitHub][1])