Redis Models
This package provides a simple model structure for redis based storage. It acts like an ORM and allows you to create models with validation.
Installation
pip install redis_models
Usage
First, you need to create your model. It's similar to Pydantic models, or built-in dataclass.
from redis_models.models import RedisModel
class MyUserModel(RedisModel):
name: str
age: int
class Meta:
redis_url = "redis://localhost:6379/0"
indexes = ("name", )
Let's create a User object
user = User(name="John Doe", age=30)
Let's try creating an object that does not fit to the definition
try:
bad_user = User(name="John", eye_color="blue")
except ValidationError:
pass
Let's write it to Redis
user.save()
Let's see its id. ids are UUID4 hex strings
print(user.id)
Fetch the record from Redis
user_again = User.get(id=user.id)
See the object as dictionary
print(user.asdict())
Let's delete the record
user_again.delete()
Trying to find a non-existing record throws NotFound exception
from redis_models.errors import NotFound
try:
user_not_found = User.get(id=user_again.id)
except NotFound:
print("User not found!")
Trying to filter with exact values.
In this example, name field is noted as an index in the model.
User.filter(name="nejdet")
How it works
This package relies on RedisModelMeta and RedisModel classes.
RedisModelMeta is a type constructor and RedisModel is a class that contains required methods for derived models.
When you call YourModel.save() the method will
-
Run
YourModel.asdict()and get the whole data serialized to JSON (Therefore each field must be JSON-serializable!) -
Create a key like
YourModel-data-<SOME_UUID>and puts the whole serialized data as value -
Create an index set for each field you put in
YourModel.Meta.indexesusingYourModel-index-<INDEX_FIELD_NAME>-<INDEX_FIELD_VALUE>format and UUID for each record as a value.
When you call YourModel.get(id=id_value) the method will search among YourModel-data-<SOME_UUID> keys and return the data.
When you call YourModel.filter(index1=value1, index2=value2) the method will
- Use
SINTERto have an intersection for UUIDs. - Then uses
MGETto get multiple values at once. - Returns a generator that yields
YourModelobjects (for memory efficiency).
Metadata
Release files for redis_models 0.4.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| redis_models-0.4.3.tar.gz | 4.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| redis_models-0.4.3-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 9.0 kB
Release files / redis_models-0.4.3.tar.gz
| Download URL | redis_models-0.4.3.tar.gz |
|---|---|
| Size | 4.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9336faef2b597f8adfd90eb0bc419d7a4612955bc0924a2b62cb1823c22f82e4
|
|
BLAKE2b-256 checksum How to use checksums |
4255d05fd59df1b33871a488533ad2051d76c7dcc6bc5bd48689d34e148c513d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.32.3
|
Release files / redis_models-0.4.3-py2.py3-none-any.whl
| Download URL | redis_models-0.4.3-py2.py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
42e4393b2eb5ad9d25aa6c775ec2e33ae7065fff4f9de11df2067f4c9e25029a
|
|
BLAKE2b-256 checksum How to use checksums |
c306c852f7223e3c8940549327eebd9e2c7a6b6b3bc51916ccc30032802ae3a0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.32.3
|