Documentation for python-stdnet's DEVELOPMENT version. Get the release docs here.
Redis is an advanced key-value store where each key is associated with a value. What makes Redis different from many other key-value databases, is that values can be of different types:
- Strings
- Lists
- Sets
- Sorted Sets
- Hash tables
In other words, you can look at redis as a data structure server, the networked equivalent of the standard template library in C++.
Redis loads and maintains the whole data-set into memory, but the data-set is persistent, since at the same time it is saved on disk, so that when the server is restarted data can be loaded back in memory. If you need speed, Redis is great solution.
Note
stdnet is a made up word from std for Standard Template Library and net for networked.
The connection string is a way to specify the various parameters of the backend to use. Redis supports the following parameters:
A full connection string could be:
redis://127.0.0.1:6379?db=3&password=bla&namespace=test.&timeout=5
Each stdnet.odm.StdModel class has an associated base key which specifies the namespace for all keys associated with it:
>>> from stdnet import getdb
>>> from stdnet.apps.searchengine import WordItem
>>> rdb = getdb('redis://localhost:6379?db=7&namespace=bla.')
>>> rdb.basekey(WordItem._meta)
'bla.searchengine.worditem'
Each stdnet.odm.StdModel instance is mapped into a redis Hash table. The hash table key is uniquely evaluated by the model hash and the id of the model instance and it is stored at:
<<basekey>>:obj:<<id>>
For example, a WordItem with id 1 is mapped by the database handler in the code snipped above, into a redis hash table at key bla.searchengine.worditem:obj:1. The hash fields and values are given by the field name and values of the model instance.
Indexes are obtained by using sets with keys obtained using the following form:
<<basekey>>:idx:<<field name>>:<<field value>>
If the model specify an implicit ordering via the stdnet.odm.Metaclass.ordering attribute, indexes are stored in sorted sets rather than sets.
For some models you may need to specify certain field to be unique across the Model. For example the following User model:
class User(odm.StdModel):
username = odm.SymbolField(unique=True)
emauil = odm.SymbolField(unique=True)
password = odm.CharField(required=True)
specifies two constrains. In redis these constraints are stored into two separate hash tables with key given by:
<<basekey>>:uni:<<field name>>
Therefore our User model will have two additional hash tables at:
<<basekey>>:uni:username
<<basekey>>:uni:email
Each hash table map a field value to the id containing that value
Redis stdnet.odm.Session and Query are handled by lua scripts which perform them in a single atomic operation.
A stdnet.odm.Query is handled in redis by two different lua scripts:
The list of arguments passed to the stdnet.lib.lua.load_query script: