Package: redis-schematics
Provides Redis persistence to Schematics models with cutomizable abstraction levels.InstallingUsing pip:pip install redis_schematicsUnderstanding Persistence layersThere 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 storage model 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.SimpleRedisMixinAdd 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 as on the example below.You may use this Mixin when you have frequent matches 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 onfilter,allandgeton non primary key operations.HashRedisMixinAdd 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 matches 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 onfilterandgeton
non primary key operations.QuickstartCreating models with persistenceNote: you should include a pk, but don’t bother setting it’s value manually.
We can infer it from anidfield or by setting a tuple of field names using__unique_together__.fromdatetimeimportdatetime,timedeltafromredisimportStrictRedisfromredis_schematicsimportSimpleRedisMixinfromschematicsimportmodels,typesclassIceCreamModel(models.Model,SimpleRedisMixin):pk=types.StringType()# Just include a pkid=types.StringType()flavour=types.StringType()amount_kg=types.IntType()best_before=types.DateTimeType()Setting on RedisSaving is simple asset().vanilla=IceCreamModel(dict(id='vanilla',flavour='Sweet Vanilla',amount_kg=42,best_before=datetime.now()+timedelta(days=2),))chocolate=IceCreamModel(dict(id='chocolate',flavour='Delicious Chocolate',amount_kg=12,best_before=datetime.now()+timedelta(days=3),))vanilla.set()chocolate.set()Getting from RedisThere are two basic ways to get an element from Redis: by pk or by value.
You can use the classmethodsmatch_for_pk(pk)ormatch_for_values(**Kwargs)or just simplymatch(**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_for_pk('vanilla')IceCreamModel.match_for_values(amount__gte=30)IceCreamModel.match(id='vanilla')# match on pkIceCreamModel.match(best_before__gte=datetime.now())# match on valuesvanilla.refresh()Fetching all and filteringYou can also useall()to deserialize all and filters. Notice that
this invlolves deserializing all stored objects.IceCreamModel.all()IceCreamModel.filter(amount__gte=30)Deleting and expiringTo remove objects, you can set__expire__or use thedelete()method.
Notice that expires work differently on single key and multiple keys approaches.classMyVolatileModel(models.Model,SimpleRedisMixin):__expire__=3600# model expire (in seconds)pk=types.StringType()vanilla.delete()JSONIf you want json serialization, you have at least two options:Patch the default serializer.Write a custom JSONEncoder.We’ve implemented a handy patch funtion, you need to add this
code to somewhere at the top of everything to automagically add
json serialization capabilities:fromredis_schematics.patchesimportpatch_jsonpatch_json()NoteEventually__json__will be added to the stdlib, seehttps://bugs.python.org/issue27362RoadmapWe are still0.x, but we are very close to a stable API. Checkour roadmapfor a glance of what’s missing.