Redis ORM library that gives redis easy-to-use objects with fields and speeds a development up, inspired by Django ORM.
Install using pip:
pip install redis-models
You can configure the way redis-models work using the BaseRedisManager
class.
BaseRedisManager(
# redis.ConnectionPool - will try to connect to localhost:6389/0 if none is provided
connection_pool=None,
# Default prefix for all keys stored in redis.
prefix='redis_test',
# Should deserialization errors raise an exception?
ignore_deserialization_errors=True,
# Whether KEYS or SCAN should be used for getting all instances matching a pattern from redis.
# When the database size is relatively small, KEYS is significantly faster, however, when
# the database is getting bigger, SCAN has better performance since it does not require to load all
# keys at once
use_keys=True,
# Perform actions in a non-blocking manner (do not wait for ack from redis).
non_blocking=False,
)
This package has Django-like architecture for RedisModel
classes.
A RedisModel
has a RedisModelManager
class which can be used to create, query, update, delete
existing instances in redis.
RedisField
- base class for nesting all fields, support default value, set of choices,
and whether field is nullable (empty).
RedisString
- stringRedisNumber
- int or floatRedisId
- instances IDsRedisBool
- boolRedisDecimal
- working accurately with numbers via decimalRedisJson
- for data, that can be used withjson.loads
/json.dumps
.RedisList
- listRedisDict
- dictRedisDateTime
- for work with date and time, via python datetime.datetimeRedisDate
- for work with date, via python datetime.dataRedisForeignKey
- for link to other instanceRedisManyToMany
- for links to other instances
Using your model manager, you can query and filter instances of the model. For example, for the model:
from redis_models import (RedisModel, RedisString, RedisDateTime, )
class BotSession(RedisModel):
session_token = RedisString(default='session_token_value')
created = RedisDateTime(default=datetime.datetime.now)
It is possible to query:
BotSession.objects.query(session_token='session_token_value')
# Will return the instances with session_token='session_token_value'
yesterday = datetime.datetime.now() - datetime.timedelta(days=1)
BotSession.objects.query(created__gte=yesterday)
# Will return all instances that have created >= yesterday.
Supported filtering:
exact
- equalityiexact
- case-independent equalitycontains
- is filter string in the value stringicontains
- is filter string case-independent in the value stringin
- is value in the provided listgt
- is value greatergte
- is value greater or equalslt
- is value lesslte
- is value less or equalsstartswith
- is string starts withistartswith
- is string case-independent starts withendswith
- is string ends withiendswith
- is string case-independent ends wthrange
- is value in provided rangeisnull
- is value in ["null", None]