# FastAPI Cache
[](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)
[](https://opensource.org/licenses/MIT)
[](https://pypi.python.org/pypi/fastapi-cache/)
[](https://pepy.tech/project/fastapi-cache)
[](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.
* 0.1.0 Added TTL support for InMemoryCacheBackend. Added `expire()` method that update ttl value for key.
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
FastAPI缓存 将简单的轻量级缓存系统实现为FastAPI中的依赖项。 安装 pip install fastapi-cache 用法示例 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
资源推荐
资源详情
资源评论




























收起资源包目录





























共 23 条
- 1
资源评论


基少成多
- 粉丝: 33
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 520网络情人节祝福语简短一句话.docx
- 计算机专业单片机课程设计要求.docx
- 基于元胞自动机的适应网络病毒传播研究.pptx
- 网络公司第一季度工作总结.pptx
- 网络咨询解答技巧.ppt
- 数据库课程设计机票预订系统.doc
- 信息系统安全等级保护第二级要求技术要求物理安全物理位置选择------.pdf
- 软件工程需求分析.doc
- 2023年计算机二级MSOFFICE模拟考试题及答案题目.doc
- 移动通信试题基础题.doc
- 设备报废申请单(Excel表格通用模板).xlsx
- 数字医学图像处理复习资料.pdf
- 高级语言程序设计.doc
- 互联网公司员工的辞职信.doc
- 东莞大剧院综合布线系统智能化系统项目工程设计文件.doc
- easy-query-SQL资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
