Skip to main content

Easily use caching in FastAPI!

Project description

fastapi-and-caching

FastAPI and Caching is an extension for FastAPI that provides support for various caching mechanisms, allowing you to easily leverage caching within your FastAPI applications.

Package version Downloads Supported Python versions License

Install

pip install fastapi-and-caching

How to Use:

RedisCache

First, configure it as follows:

from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi_and_caching import RedisCache
from config import settings


app = FastAPI()
cache = RedisCache(namespace="fastapi")

@asynccontextmanager
async def lifespan(app: FastAPI):
    await cache.init(connection_url="redis://localhost")

    yield

    await cache.close()

Then you can use it as follows: ‍‍‍

@app.get("/")
@cache.cached(key="root", expire=30, prefix="router")
def root():
    return "FastAPI And Caching"

Cached input parameters:

  • key (str, optional): The key under which to store the cached result. Defaults to the function name if not provided.
  • expire (int, optional): Time in seconds for the cache to expire. Defaults to 60 seconds.
  • prefix (str, optional): A prefix to add to the cache key. Defaults to None.
  • none (bool, optional): Whether to cache None values. Defaults to True.
  • use_params (bool, optional): Whether to include function parameters in the cache key. Defaults to True.
  • key_builder (callable, optional): A custom function to build the cache key. Defaults to None.

Other methods:

cache.keys()

  • key (str): The specific key or pattern to search for in the cache.
  • prefix (str): A prefix to be added to the key before searching.

cache.get()

  • key (str): The key to be used for retrieving the cached value.
  • prefix (str, optional): A prefix to be added to the key before retrieving. Defaults to None.
  • params (dict, optional): Additional parameters to be considered when generating the cache key. Defaults to None.
  • key_builder (typing.Callable, optional): A custom function for building the cache key. Defaults to None.

cache.set()

  • key (str): The key under which to store the value in the cache.
  • value (str): The value to be stored in the cache.
  • expire (int, optional): Time in seconds for the cache entry to expire. Defaults to None.
  • prefix (str, optional): A prefix to be added to the key before storing. Defaults to None.
  • params (dict, optional): Additional parameters to be considered when generating the cache key. Defaults to None.
  • key_builder (typing.Callable, optional): A custom function for building the cache key. Defaults to None.
  • **kwargs: Additional keyword arguments to be passed to the cache backend's set method.

cache.exists()

  • key (str): The key to check for existence in the cache.
  • prefix (str, optional): A prefix to be added to the key before checking. Defaults to None.

cache.expire()

  • key (str): The key for which to set the expiration time.
  • seconds (int): The number of seconds until the key expires.

cache.delete()

  • key (str): The key to be deleted from the cache.
  • prefix (str, optional): A prefix to be added to the key before deletion. Defaults to None.
  • params (dict, optional): Additional parameters to be considered when generating the cache key. Defaults to None.

cache.delete_startswith()

  • key (str): The pattern to match at the beginning of cache keys.
  • prefix (str, optional): A prefix to be added to the key before deletion. Defaults to None.
  • params (dict, optional): Additional parameters to be considered when generating the cache key. Defaults to None.

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

fastapi_and_caching-0.0.11.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

fastapi_and_caching-0.0.11-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_and_caching-0.0.11.tar.gz.

File metadata

  • Download URL: fastapi_and_caching-0.0.11.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.2 Linux/6.1.0-22-amd64

File hashes

Hashes for fastapi_and_caching-0.0.11.tar.gz
Algorithm Hash digest
SHA256 3e70e625f51e53b2cd67390fbe8e12eca002d5aef5f68bb1ec193af0908a0719
MD5 05eac1d567f2e802c8cca628d64e0ef0
BLAKE2b-256 10bc24aae7cbf3ac76fda37f32c7c54e61f99e3eaa3afc4371664a359ca9e384

See more details on using hashes here.

File details

Details for the file fastapi_and_caching-0.0.11-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_and_caching-0.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 c69266f91b19fc3f8eac815b30d74ddf59c356e505263e541369777e884c3551
MD5 fd78e5f1b78082d71ccd67056c6eaee4
BLAKE2b-256 42fa1e388132b23bae9cf36aa0c83690ba2d1bf1c17d60f4af2efad7e566b381

See more details on using hashes here.

Supported by

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