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.
Install
pip install git+https://github.com/heysaeid/fastapi-and-logging/tree/master
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fastapi_and_caching-0.0.1.tar.gz
.
File metadata
- Download URL: fastapi_and_caching-0.0.1.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | e62eabdd4119033752a544b3b5a0d3ccf4d5730c4d98de0b4d6b6e27052f598e |
|
MD5 | 267c41990e03b131bec72097bd4d48fa |
|
BLAKE2b-256 | 899a6b27992bbf4487b4d48bc544454f771b665b0e26ccc9da6a58c545a70a79 |
File details
Details for the file fastapi_and_caching-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: fastapi_and_caching-0.0.1-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.2 Linux/6.1.0-22-amd64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b087f06e6bde1765c6a17307a10cdad0453cdab930ec2fa3e090f7b52f73dbc3 |
|
MD5 | ad403f2c083a8842b1f15f627633e158 |
|
BLAKE2b-256 | a8f59eceb0608fa55f390fa7338ae3807279f48ff628c17c19b8ebc1ef663a5b |