mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2026-03-13 07:49:55 +08:00
Use newer version of Redis dependency incorporating async support
This commit is contained in:
@@ -13,10 +13,10 @@ pip install 'fastapi-users[redis]'
|
|||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
```py
|
```py
|
||||||
import aioredis
|
import redis.asyncio
|
||||||
from fastapi_users.authentication import RedisStrategy
|
from fastapi_users.authentication import RedisStrategy
|
||||||
|
|
||||||
redis = aioredis.from_url("redis://localhost:6379", decode_responses=True)
|
redis = redis.asyncio.from_url("redis://localhost:6379", decode_responses=True)
|
||||||
|
|
||||||
def get_redis_strategy() -> RedisStrategy:
|
def get_redis_strategy() -> RedisStrategy:
|
||||||
return RedisStrategy(redis, lifetime_seconds=3600)
|
return RedisStrategy(redis, lifetime_seconds=3600)
|
||||||
@@ -24,7 +24,7 @@ def get_redis_strategy() -> RedisStrategy:
|
|||||||
|
|
||||||
As you can see, instantiation is quite simple. It accepts the following arguments:
|
As you can see, instantiation is quite simple. It accepts the following arguments:
|
||||||
|
|
||||||
* `redis` (`aioredis.Redis`): An instance of `aioredis.Redis`. Note that the `decode_responses` flag set to `True` is necessary.
|
* `redis` (`redis.asyncio.Redis`): An instance of `redis.asyncio.Redis`. Note that the `decode_responses` flag set to `True` is necessary.
|
||||||
* `lifetime_seconds` (`Optional[int]`): The lifetime of the token in seconds. Defaults to `None`, which means the token doesn't expire.
|
* `lifetime_seconds` (`Optional[int]`): The lifetime of the token in seconds. Defaults to `None`, which means the token doesn't expire.
|
||||||
|
|
||||||
!!! tip "Why it's inside a function?"
|
!!! tip "Why it's inside a function?"
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import secrets
|
import secrets
|
||||||
from typing import Generic, Optional
|
from typing import Generic, Optional
|
||||||
|
|
||||||
import aioredis
|
import redis.asyncio
|
||||||
|
|
||||||
from fastapi_users import exceptions, models
|
from fastapi_users import exceptions, models
|
||||||
from fastapi_users.authentication.strategy.base import Strategy
|
from fastapi_users.authentication.strategy.base import Strategy
|
||||||
@@ -9,7 +9,9 @@ from fastapi_users.manager import BaseUserManager
|
|||||||
|
|
||||||
|
|
||||||
class RedisStrategy(Strategy[models.UP, models.ID], Generic[models.UP, models.ID]):
|
class RedisStrategy(Strategy[models.UP, models.ID], Generic[models.UP, models.ID]):
|
||||||
def __init__(self, redis: aioredis.Redis, lifetime_seconds: Optional[int] = None):
|
def __init__(
|
||||||
|
self, redis: redis.asyncio.Redis, lifetime_seconds: Optional[int] = None
|
||||||
|
):
|
||||||
self.redis = redis
|
self.redis = redis
|
||||||
self.lifetime_seconds = lifetime_seconds
|
self.lifetime_seconds = lifetime_seconds
|
||||||
|
|
||||||
|
|||||||
@@ -96,6 +96,7 @@ dev = [
|
|||||||
"httpx",
|
"httpx",
|
||||||
"asgi_lifespan",
|
"asgi_lifespan",
|
||||||
"uvicorn",
|
"uvicorn",
|
||||||
|
"types-redis",
|
||||||
]
|
]
|
||||||
sqlalchemy = [
|
sqlalchemy = [
|
||||||
"fastapi-users-db-sqlalchemy >=4.0.0",
|
"fastapi-users-db-sqlalchemy >=4.0.0",
|
||||||
@@ -107,8 +108,7 @@ oauth = [
|
|||||||
"httpx-oauth >=0.4,<0.7"
|
"httpx-oauth >=0.4,<0.7"
|
||||||
]
|
]
|
||||||
redis = [
|
redis = [
|
||||||
"aioredis >=2.0.1,<2.1.0",
|
"redis >=4.3.3,<5.0.0",
|
||||||
"hiredis >=2.0.0,<2.1.0",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.urls]
|
[project.urls]
|
||||||
|
|||||||
Reference in New Issue
Block a user