Package: redisca
Master branch:InstallationUsing PyPi (recommended):sudo pip install rediscaorwget https://pypi.python.org/packages/source/r/redisca/redisca-X.tar.gz
tar xvf redisca-X.tar.gz
sudo python redisca-X/setup.py installModelfromrediscaimportModelfromrediscaimportEmailfromrediscaimportDateTimeclassUser(Model):email=Email(field='eml',# Define link with 'eml' hash key.index=True,# Index support.unique=True,# Makes sure that field is unique across db.)created=DateTime(field='created',# Define link with 'created' hash key.new=datetime.utcnow,# Value which is used as default in User.new())age=Integer(field='age',# Define link with 'age' hash key.index=True,# Enable index.)user=User.new()# Create model with random id and "new" fields values.user=User.new(model_id='your_id')# Or use custom id if needed.user.getid()# user iduser.email='foo@bar.com'user.save()# Saving routinesuser.exists()# Trueuser.delete()# Deletion routinesuser.exists()# FalseFieldsField is the way how you should control data in your models. Just define class variables with field-specific options and take classic ORM’s advantages:data validation;native python data types support;transparent relations between models;indexes support (incl. unique indexes).Available parameters:field- redis hash field name to store value in.index- makes field searchable.unique- tells that value should be unique across database. Model.save() will raise an Exception if model of same class already exists with given value.new- field value which is used as default in Model.new(). Functions, methods and built-in’s are acceptable as callback values.Built-in fields:String- extendsIndexFieldwith additional parametersminlenandmaxlen.Email- extendsIndexFieldfield with email validation support.Integer- extendsRangeIndexFieldwith parametersminvalandmaxval. Accepts int and numeric strings. Returns int.Reference- extendsIndexFieldwithcls(reference class) parameter. Accepts and returns instance ofcls.MD5Pass- extendsStringfield. Acts like string but converts given string to md5 sum.DateTime- extendsRangeIndexFieldwithout additional parameters. Accepts datetime and int(timestamp) values. Returns datetime.Getting DataUsing idHere is an example how to get model instance using id(empty model returned if it not exists yet):user=User('user id')print(user.email)# 'foo@bar.com'Each initialized model is saved in registry and returned on each attempt of re-init:user1=User('user_id')user2=User('user_id')user1isuser2# Always is Trueuser.free()# Unregister model instance.User.free_all()# Cleanup User's registry.Model.free_all()# Unregister all known models.Find by Indexusers=User.email=='foo@bar.com'# or User.email.find('foo@bar.com')Subclasses ofRangeIndexFieldhas a limited support for ranged queries:users=User.age>=10# User.age.range(minval=10)More complex queries are also possible:# SELECT * FROM `users` where `age` BETWEEN 0 AND 100 LIMIT 10 OFFSET 50;users=User.age.range(minval=0,maxval=100,start=50,num=10)Dict APIAll fields are linked to model dict keys. Use can use model dict API to read and writeredis hashdata AS IS:user=User('id')user['eml']='foo@bar.com'user['age']=10Connecting to RedisGlobal database connection setup looks like this:fromrediscaimportconffromredisimportRedisconf.db=Redis()Note:rediscauses localhost:6379(0) as default database. You can setupinheritableper-model database connection usingconfclass decorator:fromrediscaimportModelfromrediscaimportconffromredisimportRedis@conf(db=Redis())classUser(Model):passKey FormatModel key format is:model_key_prefix:model_idDefault model_key_prefix islowercased class name. Useconfclass decorator to override it like this:fromrediscaimportModelfromrediscaimportconf@conf(prefix='usr')classUser(Model):passprint(User.getprefix())# 'usr'print(user.getkey())# 'usr:1'ToolsID Generatorfromrediscaimporthexidfromrediscaimportintidprint(hexid())# 59d369790print(hexid())# 59d3697bcprint(intid())# 24116751882print(intid())# 24116788848Flask SupportfromrediscaimportFlaskRediscaapp=Flask()app.config['REDISCA']={# redis.StrictRedis constructor kwargs dict.}FlaskRedisca(app)Optionalautosaveconstructor parameter tellsrediscathat all known models should be saved at the end of request (if no exception raised). Unchanged and deleted instances are ignored. If you want to skip locally changed instances use free() method during request life.Requirementsredis-py 2.7+python 2.7/3.2+ or pypy 2.1+Python 3.x supportPy3k support is still a sort of experiment but I’m looking carefuly into full compability with cutting-edge builds of CPython. There are no known issues with it actually.