Skip to main content

This package provides a simple model structure for redis based storage.

Project description

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.indexes using YourModel-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 SINTER to have an intersection for UUIDs.
  • Then uses MGET to get multiple values at once.
  • Returns a generator that yields YourModel objects (for memory efficiency).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

redis_models-0.4.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

redis_models-0.4.2-py2.py3-none-any.whl (4.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file redis_models-0.4.2.tar.gz.

File metadata

  • Download URL: redis_models-0.4.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for redis_models-0.4.2.tar.gz
Algorithm Hash digest
SHA256 2eb1eb4c5fa3f91fea56f9d145009796ad9afc4e674b203f54e22db57d498781
MD5 ba1c654fb2fdd1e044665e99cf5c9c49
BLAKE2b-256 baa0f28d1d783892413ebdbb4e8a9e14f87366fde194adb6f8a52535f1362203

See more details on using hashes here.

File details

Details for the file redis_models-0.4.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for redis_models-0.4.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0ed208d639a7365ade5f3c0a058f273a9354cbcc6d51d3f9a9de011a5ba88e98
MD5 791b0d941d3f1de14d4fe81d01b7a83e
BLAKE2b-256 ede791e161d92215a25fff3a64e6fdd30643a2a3e8a4d9e85e2151e8eabc3a68

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page