Stay Hungry,Stay Foolish!

Using Redis with FastAPI

Using Redis with FastAPI

https://developer.redis.com/develop/python/fastapi/

https://github.com/fanqingsong/fastapi-redis-tutorial

FastAPI is a Python web framework based on the Starlette microframework. With deep support for asyncio, FastAPI is indeed very fast. FastAPI also distinguishes itself with features like automatic OpenAPI (OAS) documentation for your API, easy-to-use data validation tools, and more.

Of course, the best way to make your FastAPI service even faster is to use Redis. Unlike most databases, Redis excels at low-latency access because it's an in-memory database.

In this tutorial, we'll walk through the steps necessary to use Redis with FastAPI. We're going to build IsBitcoinLit, an API that stores Bitcoin sentiment and price averages in Redis Stack using a timeseries data structure, then rolls these averages up for the last three hours.

 

aioredis

https://aioredis.readthedocs.io/en/latest/getting-started/

import asyncio

import aioredis


async def main():
    redis = aioredis.from_url("redis://localhost")
    await redis.set("my-key", "value")
    value = await redis.get("my-key")
    print(value)


if __name__ == "__main__":
    asyncio.run(main())

 

redis timeseries api

https://redis.io/commands/ts.create/

 

Redis 数据类型

https://www.runoob.com/redis/redis-data-types.html

 

background task

https://fastapi.tiangolo.com/tutorial/background-tasks/

 

posted @ 2023-12-06 22:47  lightsong  阅读(28)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel