Installing Redis-py
sudo Install Redis
Connecting to a database
= Redis. Strictredis (host='localhost', port=6379, db=0)
Note: Use R=reids. Redis (host= ' localhost ', port=6379,db=0) is also available. Difference: Redis is a subclass of Strictredis for backwards compatibility with older versions of Redis-py. The official recommendation is to use Strictredis.
Simple Redis operation
>>> R.set ('name','Jihite') True>>> R.set ('score', -) True>>>R.keys () ['score','name']>>> R.get ('name')'Jihite'>>> R.get ('score')' -'>>> R.delete ('score')1>>>R.keys () ['name']>>>r.save () True>>>R.keys () ['name']>>>r.flushdb () True>>>R.keys () []
Pipeline operation
The pipeline is a subclass of the base class where Redis caches multiple server commands in a single request, which greatly improves the performance of bulk commands by reducing the number of repeated TCP database packets between server-clients.
Example
>>> p =R.pipeline ()>>> P.hset ('MySet','name','Jihite') Pipeline<connectionpool<connection6379, db=0>>>>>> P.hset ('MySet','score', -) Pipeline<connectionpool<connection6379, db=0>>>>>> P.hget ('MySet','name') Pipeline<connectionpool<connection6379, db=0>>>>>>P.execute () [1L,1L,'Jihite']>>> R.hget ('MySet','name')'Jihite'>>> R.hget ('MySet','score')' -'
Note: Pipeline commands can be written together, such as:
>>> p =R.pipeline ()>>> P.set ('name','Jihite'). Set ('score', -). Set ('School','Bupt'). Get ('score'). Execute () [True, True, true ,' -']
Connection Pools
Redis-py Manage connections to Redis Server by connecting to the pool, by default each Redis connection instance automatically creates its own link pool, which can take advantage of an already existing link pool.
>>> pool = Redis. ConnectionPool (host='localhost', port=6379, db=0)>> > r = Redis. Redis (Connection_pool=pool)
Python Operation Redis