Skip to content

Updating documentation to use redis.asyncio #447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ TOC
ULIDs
UlidPrimaryKey
WSL
aioredis
async
asyncio
cls
Expand Down
2 changes: 1 addition & 1 deletion docs/connections.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
8 changes: 4 additions & 4 deletions docs/fastapi_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down
16 changes: 8 additions & 8 deletions docs/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```
Expand Down Expand Up @@ -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"
```
Expand All @@ -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.
Expand All @@ -141,7 +141,7 @@ from redis_om import HashModel

class Customer(HashModel):
# ... Fields ...

class Config:
orm_mode = True
arbitrary_types_allowed = True
Expand Down Expand Up @@ -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.
Expand Down
Loading