# FastAPI Cache
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/2ec5c44e899943c8920d3c3e31616784)](https://app.codacy.com/manual/ivan.sushkov/fastapi_cache?utm_source=github.com&utm_medium=referral&utm_content=comeuplater/fastapi_cache&utm_campaign=Badge_Grade_Dashboard)
[![License](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
[![PyPi Version](https://img.shields.io/pypi/v/fastapi-cache.svg)](https://pypi.python.org/pypi/fastapi-cache/)
[![Downloads](https://pepy.tech/badge/fastapi-cache)](https://pepy.tech/project/fastapi-cache)
[![Build Status](https://travis-ci.com/comeuplater/fastapi_cache.svg?branch=master)](https://travis-ci.com/comeuplater/fastapi_cache)
Implements simple lightweight cache system as dependencies in FastAPI.
## Installation
```sh
pip install fastapi-cache
```
## Usage example
```python
from fastapi import Depends, FastAPI
from fastapi_cache import caches, close_caches
from fastapi_cache.backends.redis import CACHE_KEY, RedisCacheBackend
app = FastAPI()
def redis_cache():
return caches.get(CACHE_KEY)
@app.get('/')
async def hello(
cache: RedisCacheBackend = Depends(redis_cache)
):
in_cache = await cache.get('some_cached_key')
if not in_cache:
await cache.set('some_cached_key', 'new_value', 5)
return {'response': in_cache or 'default'}
@app.on_event('startup')
async def on_startup() -> None:
rc = RedisCacheBackend('redis://redis')
caches.set(CACHE_KEY, rc)
@app.on_event('shutdown')
async def on_shutdown() -> None:
await close_caches()
```
## TODO
* [X] Add tests
* [ ] ~~Add registry decorator~~
* [ ] Add dependency for requests caching
## Acknowledgments
* [Balburdia](https://github.com/Balburdia)
* [xobtoor](https://github.com/xobtoor)
* [jersobh](https://github.com/jersobh)
## Changelog
* 0.0.6 Added typings for backends. Specific arguments now need to be passed through **kwargs.
Set default encoding to utf-8 for redis backend, removed default TTL for redis keys.
Raw data
{
"_id": null,
"home_page": "https://github.com/comeuplater/fastapi_cache",
"name": "fastapi-cache",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "redis,aioredis,asyncio,fastapi,starlette,cache",
"author": "Ivan Sushkov <comeuplater>",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/4d/f8/0cbd6a6ac2309bedff4f98a345d9629953afdf0dc25f7b2f2218384d881f/fastapi-cache-0.1.0.tar.gz",
"platform": "",
"description": "# FastAPI Cache\n\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/2ec5c44e899943c8920d3c3e31616784)](https://app.codacy.com/manual/ivan.sushkov/fastapi_cache?utm_source=github.com&utm_medium=referral&utm_content=comeuplater/fastapi_cache&utm_campaign=Badge_Grade_Dashboard)\n[![License](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)\n[![PyPi Version](https://img.shields.io/pypi/v/fastapi-cache.svg)](https://pypi.python.org/pypi/fastapi-cache/)\n[![Downloads](https://pepy.tech/badge/fastapi-cache)](https://pepy.tech/project/fastapi-cache)\n[![Build Status](https://travis-ci.com/comeuplater/fastapi_cache.svg?branch=master)](https://travis-ci.com/comeuplater/fastapi_cache)\n\nImplements simple lightweight cache system as dependencies in FastAPI.\n\n## Installation\n\n```sh\npip install fastapi-cache\n```\n\n## Usage example\n```python\nfrom fastapi import Depends, FastAPI\n\nfrom fastapi_cache import caches, close_caches\nfrom fastapi_cache.backends.redis import CACHE_KEY, RedisCacheBackend\n\napp = FastAPI()\n\n\ndef redis_cache():\n return caches.get(CACHE_KEY)\n\n\n@app.get('/')\nasync def hello(\n cache: RedisCacheBackend = Depends(redis_cache)\n):\n in_cache = await cache.get('some_cached_key')\n if not in_cache:\n await cache.set('some_cached_key', 'new_value', 5)\n\n return {'response': in_cache or 'default'}\n\n\n@app.on_event('startup')\nasync def on_startup() -> None:\n rc = RedisCacheBackend('redis://redis')\n caches.set(CACHE_KEY, rc)\n\n\n@app.on_event('shutdown')\nasync def on_shutdown() -> None:\n await close_caches()\n```\n\n## TODO\n\n* [X] Add tests\n* [ ] ~~Add registry decorator~~\n* [ ] Add dependency for requests caching\n\n## Acknowledgments\n\n* [Balburdia](https://github.com/Balburdia)\n* [xobtoor](https://github.com/xobtoor)\n* [jersobh](https://github.com/jersobh)\n\n\n## Changelog\n\n* 0.0.6 Added typings for backends. Specific arguments now need to be passed through **kwargs.\nSet default encoding to utf-8 for redis backend, removed default TTL for redis keys.",
"bugtrack_url": null,
"license": "MIT License",
"summary": "FastAPI simple cache",
"version": "0.1.0",
"project_urls": {
"Homepage": "https://github.com/comeuplater/fastapi_cache"
},
"split_keywords": [
"redis",
"aioredis",
"asyncio",
"fastapi",
"starlette",
"cache"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4df80cbd6a6ac2309bedff4f98a345d9629953afdf0dc25f7b2f2218384d881f",
"md5": "ea8260fbcd2ec22aaf1f572f0f01b8d5",
"sha256": "1f57e6e666672c84e3dd5d4141ec808d5339d158e10c87a87eb9ce11ff8b1735"
},
"downloads": -1,
"filename": "fastapi-cache-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "ea8260fbcd2ec22aaf1f572f0f01b8d5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4786,
"upload_time": "2021-02-12T14:58:56",
"upload_time_iso_8601": "2021-02-12T14:58:56.929770Z",
"url": "https://files.pythonhosted.org/packages/4d/f8/0cbd6a6ac2309bedff4f98a345d9629953afdf0dc25f7b2f2218384d881f/fastapi-cache-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2021-02-12 14:58:56",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "comeuplater",
"github_project": "fastapi_cache",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"requirements": [],
"tox": true,
"lcname": "fastapi-cache"
}