Updating documentation to use redis.asyncio (#447)

* removing aioredis from documentation

* fix: proper python 3.11 support (#446)

* fix: propper python 3.11 support

* fix tox config

* wider python 3.x support

* Update ci.yml

Co-authored-by: dvora-h <67596500+dvora-h@users.noreply.github.com>

* Add poetry.lock to .gitignore (#457)

* Add poetry.lock to .gitignore

* remove poetry.lock

* PR comments readme update

Co-authored-by: Yaraslau Zhylko <YaraslauZhylko@gmail.com>
Co-authored-by: dvora-h <67596500+dvora-h@users.noreply.github.com>
main
Chayim 1 year ago committed by GitHub
parent 1aa619e418
commit 6955225c08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -27,7 +27,6 @@ TOC
ULIDs
UlidPrimaryKey
WSL
aioredis
async
asyncio
cls

@ -80,7 +80,7 @@ class Customer(HashModel):
database = redis
```
The `get_redis_connection()` function is a Redis OM helper that passes keyword arguments to either `aioredis.Redis.from_url()` or `redis.Redis.from_url()`, depending on whether you are using Redis OM in async or sync mode.
The `get_redis_connection()` function is a Redis OM helper that passes keyword arguments to either `redis.asyncio.Redis.from_url()` or `redis.Redis.from_url()`, depending on whether you are using Redis OM in async or sync mode.
You can also manually construct a client object:

@ -36,7 +36,7 @@ Let's look at an example FastAPI app that uses Redis OM.
import datetime
from typing import Optional
import aioredis
import redis
from fastapi import FastAPI, HTTPException
from starlette.requests import Request
@ -94,7 +94,7 @@ async def get_customer(pk: str, request: Request, response: Response):
@app.on_event("startup")
async def startup():
r = aioredis.from_url(REDIS_CACHE_URL, encoding="utf8",
r = redis.asyncio.from_url(REDIS_CACHE_URL, encoding="utf8",
decode_responses=True)
FastAPICache.init(RedisBackend(r), prefix="fastapi-cache")
@ -148,7 +148,7 @@ Here is the previous FastAPI app, but using asyncio-compatible Redis OM code:
import datetime
from typing import Optional
import aioredis
import redis
from fastapi import FastAPI, HTTPException
from starlette.requests import Request
@ -206,7 +206,7 @@ async def get_customer(pk: str, request: Request, response: Response):
@app.on_event("startup")
async def startup():
r = aioredis.from_url(REDIS_CACHE_URL, encoding="utf8",
r = redis.asyncio.from_url(REDIS_CACHE_URL, encoding="utf8",
decode_responses=True)
FastAPICache.init(RedisBackend(r), prefix="fastapi-cache")

@ -19,10 +19,18 @@ Python 3.7.0
If you don't have Python installed, you can download it from [Python.org](https://www.python.org/downloads/), use [pyenv](https://github.com/pyenv/pyenv), or install Python with your operating system's package manager.
This library requires [redis-py](https://pypi.org/project/redis) version 4.2.0 or higher.
## Redis
Redis OM saves data in Redis, so you will need Redis installed and running to complete this tutorial.
We recommend the [redis-stack](https://hub.docker.com/r/redis/redis-stack) image because it includes Redis capabilities that this library uses to provide extra features. Later sections of this guide will provide more detail about these features.
You can also use the official Redis Docker image, which is hosted on [Docker Hub](https://hub.docker.com/_/redis). However this does not include the Search and JSON modules required to store JSON models and use the `find` query interface.
**NOTE**: We'll talk about how to actually start Redis with Docker when we discuss _running_ Redis later in this guide.
### Downloading Redis
The latest version of Redis is available from [Redis.io](https://redis.io/). You can also install Redis with your operating system's package manager.
@ -33,17 +41,7 @@ The latest version of Redis is available from [Redis.io](https://redis.io/). You
Redis doesn't run directly on Windows, but you can use Windows Subsystem for Linux (WSL) to run Redis. See [our video on YouTube](https://youtu.be/_nFwPTHOMIY) for a walk-through.
Windows users can also use Docker. See the next section on running Redis with Docker for more information.
### Using Redis With Docker
Instead of installing Redis manually or with a package manager, you can run Redis with Docker.
We recommend the [redis-stack](https://hub.docker.com/r/redis/redis-stack) image because it includes Redis modules that Redis OM can use to give you extra features. Later sections of this guide will provide more detail about these features.
You can also use the official Redis Docker image, which is hosted on [Docker Hub](https://hub.docker.com/_/redis). However this does not include the Search and JSON modules required to store JSON models and use the `find` query interface.
**NOTE**: We'll talk about how to actually start Redis with Docker when we discuss _running_ Redis later in this guide.
Windows users can also use the Docker image mentioned previously.
## Recommended: RediSearch and RedisJSON

@ -45,7 +45,7 @@ from redis_om import HashModel
class Customer(HashModel):
first_name: str
last_name: str
class Meta:
global_key_prefix = "customer-dashboard"
```
@ -94,15 +94,15 @@ class BaseModel(HashModel, ABC):
global_key_prefix = "customer-dashboard"
database = redis
class Customer(BaseModel):
first_name: str
last_name: str
class Meta:
database = other_redis
print(Customer.global_key_prefix)
# > "customer-dashboard"
```
@ -122,11 +122,11 @@ Here is a table of the settings available in the Meta object and what they contr
| global_key_prefix | A string prefix applied to every Redis key that the model manages. This could be something like your application's name. | "" |
| model_key_prefix | A string prefix applied to the Redis key representing every model. For example, the Redis Hash key for a HashModel. This prefix is also added to the redisearch index created for every model with indexed fields. | "" |
| primary_key_pattern | A format string producing the base string for a Redis key representing this model. This string should accept a "pk" format argument. **Note:** This is a "new style" format string, which will be called with `.format()`. | "{pk}" |
| database | An aioredis.Redis or redis.Redis client instance that the model will use to communicate with Redis. | A new instance created with connections.get_redis_connection(). |
| database | A redis.asyncio.Redis or redis.Redis client instance that the model will use to communicate with Redis. | A new instance created with connections.get_redis_connection(). |
| primary_key_creator_cls | A class that adheres to the PrimaryKeyCreator protocol, which Redis OM will use to create a primary key for a new model instance. | UlidPrimaryKey |
| index_name | The RediSearch index name to use for this model. Only used if at least one of the model's fields are marked as indexable (`index=True`). | "{global_key_prefix}:{model_key_prefix}:index" |
| embedded | Whether or not this model is "embedded." Embedded models are not included in migrations that create and destroy indexes. Instead, their indexed fields are included in the index for the parent model. **Note**: Only `JsonModel` can have embedded models. | False |
| encoding | The default encoding to use for strings. This encoding is given to redis-py or aioredis at the connection level. In both cases, Redis OM will decode binary strings from Redis using your chosen encoding. | "utf-8" |
| encoding | The default encoding to use for strings. This encoding is given to redis-py at the connection level. In both cases, Redis OM will decode binary strings from Redis using your chosen encoding. | "utf-8" |
## Configuring Pydantic
Every Redis OM model is also a Pydantic model, so in addition to configuring Redis OM behavior with the Meta object, you can control Pydantic configuration via the Config object within a model class.
@ -141,7 +141,7 @@ from redis_om import HashModel
class Customer(HashModel):
# ... Fields ...
class Config:
orm_mode = True
arbitrary_types_allowed = True
@ -177,7 +177,7 @@ So, in short, if you want to use container types, use `JsonModel`.
Good news! Container types _are_ supported with `JsonModel`.
We will use Pydantic's JSON serialization and encoding to serialize your `JsonModel` and save it in Redis.
### Default Values
Fields can have default values. You set them by assigning a value to a field.

@ -37,7 +37,6 @@ include=[
[tool.poetry.dependencies]
python = ">=3.7,<4.0"
redis = ">=3.5.3,<5.0.0"
aioredis = "^2.0.0"
pydantic = "^1.10.2"
click = "^8.0.1"
pptree = "^3.1"

Loading…
Cancel
Save