Skip to main content

Redis storage backend for schematics.

Project description

Provides Redis persistence to Schematics models with cutomizable abstraction levels.


Using pip:

pip install redis_schamatics

Understanding Persistence layers

There are several ways to implement complex objects persitence on a key-value-set database such as redis. The best way to do it depends on your application constraints. We think that providing a good abstraction for your application is to allow you to choose which abstraction you want to use. Below you can find a comparison on different provided abstraction layers.

Currently we only support a SimpleRedisMixin and SimpleRedisModel, but you can use BaseRedisMixin to build your own persistance layers.


Add Redis persistance to an object using a simple approach. Each object correnspond to a single key on redis prefixed with the object namespace, which correnponds to a serialized object. To use this mixin you just need to declare a primary key such as:

You may use this Mixin when you have frequent match on primary key and set operations, unique expires, hard memory contraints or just wants a 1-1 object-key approach. You may not use this Mixin if you need performance on filter, all and get on non primary key operations.


Add Redis persistance to an object using a single hash approach. Each type correnspond to a single key on redis containing a hash set with every instance as an entry on the set which contains a serialized object.

You may use this Mixin when you have frequent match on primary key, set and all operations, hard memory contraints or wants a single key approach. You may not use this Mixin if you need performance on filter and get on non primary key operations.


Creating models with persistence

Note: you should include a pk, but don’t bother setting it’s value manually. We can infer it from an id field or by setting a tuple of field names using __unique_together__.

from datetime import datetime, timedelta

from redis import StrictRedis
from redis_schematics import SimpleRedisMixin
from schematics import models, types

class IceCreamModel(models.Model, SimpleRedisMixin):
    pk = types.StringType()  # Just include a pk
    id = types.StringType()
    flavour = types.StringType()
    amount_kg = types.IntType()
    best_before = types.DateTimeType()

Setting on Redis

Saving is simple as set().

 vanilla = IceCreamModel(dict(
     flavour='Sweet Vanilla',
     amount_kg=42, + timedelta(days=2),

chocolate = IceCreamModel(dict(
     flavour='Delicious Chocolate',
     amount_kg=12, + timedelta(days=3),


Getting from Redis

There are two basic ways to get an element from Redis: by pk or by value. You can use the classmethods match_for_pk(pk) or match_for_values(**Kwargs) or just simply match(**kwargs) to let us choose which one. Notice that the performance from both methods is a lot different, so you may avoid matching for values on high performance environments. You may also use refresh to reload an object from storage if it has been modified.


IceCreamModel.match(id='vanilla')  # match on pk
IceCreamModel.match(  # match on values


Fetching all and filtering

You can also use all() to deserialize all and filters. Notice that this invlolves deserializing all stored objects.


Deleting and expiring

To remove objects, you can set __expire__ or use the delete() method. Notice that expires work differently on single key and multiple keys approaches.

class MyVolatileModel(models.Model, SimpleRedisMixin):
    __expire__ = 3600  # model expire (in seconds)
    pk = types.StringType()



  • Support a distributed Mixin with one key per field.
  • Support a distributed Hash Mixin with one hash per field.
  • Consistent set of unit tests.
  • Support redis relationships between models.
  • Support transaction aware methods.

Project details

Release history Release notifications

This version
History Node


History Node


History Node


Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
redis_schematics-0.2.1.tar.gz (9.1 kB) Copy SHA256 hash SHA256 Source None Sep 14, 2017

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page