# fastapi-and-caching
<strong>FastAPI and Caching</strong> is an extension for FastAPI that provides support for various caching mechanisms, allowing you to easily leverage caching within your FastAPI applications.
[![Package version](https://img.shields.io/pypi/v/fastapi-and-caching?color=%2334D058&label=pypi%20package)](https://pypi.org/project/fastapi-and-caching/)
[![Downloads](https://img.shields.io/pypi/dm/fastapi-and-caching)](https://pypi.org/project/fastapi-and-caching/)
[![Supported Python versions](https://img.shields.io/pypi/pyversions/fastapi-and-caching.svg?color=%2334D058)](https://pypi.org/project/fastapi-and-caching/)
[![License](https://img.shields.io/badge/License-MIT-green.svg)](https://github.com/heysaeid/fastapi-and-caching/blob/master/LICENSE)
# Install
```
pip install fastapi-and-caching
```
## How to Use:
### RedisCache
First, configure it as follows:
```python
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:
```python
@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.
Raw data
{
"_id": null,
"home_page": "https://github.com/heysaeid/fastapi-and-caching",
"name": "fastapi-and-caching",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.7",
"maintainer_email": null,
"keywords": "fastapi, fastapi caching, fastapi and caching",
"author": "Saeid Noormohammadi",
"author_email": "heysaeid92@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/10/bc/24aae7cbf3ac76fda37f32c7c54e61f99e3eaa3afc4371664a359ca9e384/fastapi_and_caching-0.0.11.tar.gz",
"platform": null,
"description": "# fastapi-and-caching\n<strong>FastAPI and Caching</strong> is an extension for FastAPI that provides support for various caching mechanisms, allowing you to easily leverage caching within your FastAPI applications.\n\n[![Package version](https://img.shields.io/pypi/v/fastapi-and-caching?color=%2334D058&label=pypi%20package)](https://pypi.org/project/fastapi-and-caching/)\n[![Downloads](https://img.shields.io/pypi/dm/fastapi-and-caching)](https://pypi.org/project/fastapi-and-caching/)\n[![Supported Python versions](https://img.shields.io/pypi/pyversions/fastapi-and-caching.svg?color=%2334D058)](https://pypi.org/project/fastapi-and-caching/)\n[![License](https://img.shields.io/badge/License-MIT-green.svg)](https://github.com/heysaeid/fastapi-and-caching/blob/master/LICENSE)\n\n# Install\n```\npip install fastapi-and-caching\n```\n\n## How to Use:\n\n### RedisCache\nFirst, configure it as follows:\n\n```python\nfrom contextlib import asynccontextmanager\nfrom fastapi import FastAPI\nfrom fastapi_and_caching import RedisCache\nfrom config import settings\n\n\napp = FastAPI()\ncache = RedisCache(namespace=\"fastapi\")\n\n@asynccontextmanager\nasync def lifespan(app: FastAPI):\n await cache.init(connection_url=\"redis://localhost\")\n\n yield\n\n await cache.close()\n```\n\nThen you can use it as follows:\n\u200d\u200d\u200d\n```python\n@app.get(\"/\")\n@cache.cached(key=\"root\", expire=30, prefix=\"router\")\ndef root():\n return \"FastAPI And Caching\"\n```\n\nCached input parameters:\n- `key` (str, optional): The key under which to store the cached result. \n Defaults to the function name if not provided.\n- `expire` (int, optional): Time in seconds for the cache to expire. Defaults to 60 seconds.\n- `prefix` (str, optional): A prefix to add to the cache key. Defaults to None.\n- `none` (bool, optional): Whether to cache None values. Defaults to True.\n- `use_params` (bool, optional): Whether to include function parameters in the cache key. Defaults to True.\n- `key_builder` (callable, optional): A custom function to build the cache key. Defaults to None.\n\n#### Other methods:\ncache.keys()\n- `key` (str): The specific key or pattern to search for in the cache.\n- `prefix` (str): A prefix to be added to the key before searching.\n\n\ncache.get()\n- `key` (str): The key to be used for retrieving the cached value.\n- `prefix` (str, optional): A prefix to be added to the key before retrieving. Defaults to None.\n- `params` (dict, optional): Additional parameters to be considered when generating the cache key. Defaults to None.\n- `key_builder` (typing.Callable, optional): A custom function for building the cache key. Defaults to None.\n\n\ncache.set()\n- `key` (str): The key under which to store the value in the cache.\n- `value` (str): The value to be stored in the cache.\n- `expire` (int, optional): Time in seconds for the cache entry to expire. Defaults to None.\n- `prefix` (str, optional): A prefix to be added to the key before storing. Defaults to None.\n- `params` (dict, optional): Additional parameters to be considered when generating the cache key. Defaults to None.\n- `key_builder` (typing.Callable, optional): A custom function for building the cache key. Defaults to None.\n- `**kwargs`: Additional keyword arguments to be passed to the cache backend's set method.\n\n\ncache.exists()\n- `key` (str): The key to check for existence in the cache.\n- `prefix` (str, optional): A prefix to be added to the key before checking. Defaults to None.\n\n\ncache.expire()\n- `key` (str): The key for which to set the expiration time.\n- `seconds` (int): The number of seconds until the key expires.\n\n\ncache.delete()\n- `key` (str): The key to be deleted from the cache.\n- `prefix` (str, optional): A prefix to be added to the key before deletion. Defaults to None.\n- `params` (dict, optional): Additional parameters to be considered when generating the cache key. Defaults to None.\n\n\ncache.delete_startswith()\n- `key` (str): The pattern to match at the beginning of cache keys.\n- `prefix` (str, optional): A prefix to be added to the key before deletion. Defaults to None.\n- `params` (dict, optional): Additional parameters to be considered when generating the cache key. Defaults to None.",
"bugtrack_url": null,
"license": "MIT",
"summary": "Easily use caching in FastAPI!",
"version": "0.0.11",
"project_urls": {
"Documentation": "https://github.com/heysaeid/fastapi-and-caching",
"Homepage": "https://github.com/heysaeid/fastapi-and-caching",
"Repository": "https://github.com/heysaeid/fastapi-and-caching"
},
"split_keywords": [
"fastapi",
" fastapi caching",
" fastapi and caching"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "42fa1e388132b23bae9cf36aa0c83690ba2d1bf1c17d60f4af2efad7e566b381",
"md5": "fd78e5f1b78082d71ccd67056c6eaee4",
"sha256": "c69266f91b19fc3f8eac815b30d74ddf59c356e505263e541369777e884c3551"
},
"downloads": -1,
"filename": "fastapi_and_caching-0.0.11-py3-none-any.whl",
"has_sig": false,
"md5_digest": "fd78e5f1b78082d71ccd67056c6eaee4",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.7",
"size": 6338,
"upload_time": "2024-07-30T15:45:10",
"upload_time_iso_8601": "2024-07-30T15:45:10.069661Z",
"url": "https://files.pythonhosted.org/packages/42/fa/1e388132b23bae9cf36aa0c83690ba2d1bf1c17d60f4af2efad7e566b381/fastapi_and_caching-0.0.11-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "10bc24aae7cbf3ac76fda37f32c7c54e61f99e3eaa3afc4371664a359ca9e384",
"md5": "05eac1d567f2e802c8cca628d64e0ef0",
"sha256": "3e70e625f51e53b2cd67390fbe8e12eca002d5aef5f68bb1ec193af0908a0719"
},
"downloads": -1,
"filename": "fastapi_and_caching-0.0.11.tar.gz",
"has_sig": false,
"md5_digest": "05eac1d567f2e802c8cca628d64e0ef0",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.7",
"size": 4774,
"upload_time": "2024-07-30T15:45:11",
"upload_time_iso_8601": "2024-07-30T15:45:11.724422Z",
"url": "https://files.pythonhosted.org/packages/10/bc/24aae7cbf3ac76fda37f32c7c54e61f99e3eaa3afc4371664a359ca9e384/fastapi_and_caching-0.0.11.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-30 15:45:11",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "heysaeid",
"github_project": "fastapi-and-caching",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "fastapi-and-caching"
}