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 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.
Metadata
Release files for fastapi-and-caching 0.0.11
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastapi_and_caching-0.0.11.tar.gz | 4.8 kB | Without provenance | Details for fastapi_and_caching-0.0.11.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_and_caching-0.0.11-py3-none-any.whl | Python 3 | none | any | Details for fastapi_and_caching-0.0.11-py3-none-any.whl |
Total release size: 11.1 kB
Release files / fastapi_and_caching-0.0.11.tar.gz
| Download URL | fastapi_and_caching-0.0.11.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3e70e625f51e53b2cd67390fbe8e12eca002d5aef5f68bb1ec193af0908a0719
|
|
BLAKE2b-256 checksum How to use checksums |
10bc24aae7cbf3ac76fda37f32c7c54e61f99e3eaa3afc4371664a359ca9e384
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.11.2 Linux/6.1.0-22-amd64
|
Release files / fastapi_and_caching-0.0.11-py3-none-any.whl
| Download URL | fastapi_and_caching-0.0.11-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c69266f91b19fc3f8eac815b30d74ddf59c356e505263e541369777e884c3551
|
|
BLAKE2b-256 checksum How to use checksums |
42fa1e388132b23bae9cf36aa0c83690ba2d1bf1c17d60f4af2efad7e566b381
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.11.2 Linux/6.1.0-22-amd64
|
Security for fastapi-and-caching 0.0.11
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.