mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-08-15 11:11:16 +08:00
37 lines
1.4 KiB
Markdown
37 lines
1.4 KiB
Markdown
# Redis
|
|
|
|
[Redis](https://redis.io/) is an ultra-fast key-store database. As such, it's a good candidate for token management. In this strategy, a token is generated and associated with the user id. in the database. On each request, we try to retrieve this token from Redis to get the corresponding user id.
|
|
|
|
## Installation
|
|
|
|
You should install the library with the optional dependencies for Redis:
|
|
|
|
```sh
|
|
pip install 'fastapi-users[redis]'
|
|
```
|
|
|
|
## Configuration
|
|
|
|
```py
|
|
import redis.asyncio
|
|
from fastapi_users.authentication import RedisStrategy
|
|
|
|
redis = redis.asyncio.from_url("redis://localhost:6379", decode_responses=True)
|
|
|
|
def get_redis_strategy() -> RedisStrategy:
|
|
return RedisStrategy(redis, lifetime_seconds=3600)
|
|
```
|
|
|
|
As you can see, instantiation is quite simple. It accepts the following arguments:
|
|
|
|
* `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.
|
|
* `key_prefix` (`str`): The prefix used to set the key in the Redis stored. Defaults to `fastapi_users_token:`.
|
|
|
|
!!! tip "Why it's inside a function?"
|
|
To allow strategies to be instantiated dynamically with other dependencies, they have to be provided as a callable to the authentication backend.
|
|
|
|
## Logout
|
|
|
|
On logout, this strategy will delete the token from the Redis store.
|