One, Redis API support
Redis provides APIs for a variety of development languages to facilitate the development of language connections using Redis. HTTPS://redis.io/clients The official website provides API programs for different development languages.
Python Connect to Redis
HTTPS://Redis.io/clients#python has provided us with many kinds of Python-connected APIs, and we usually choose a "smiley face" with "asterisk" Use of Redis-py is recommended here.
Redis-py Installation Method
Redis-py requires a running Redis server. See Redis'sQuickStart for installation instructions. To install the Redis-using from source:$ sudo python setup.py install
Python Connect to Redis
redis-py provides Python connection and operation Redis mode: redis-py provides two classes of Redis and Strictredis for implementing Redis commands. Strictredis is used to implement most of the official commands and uses the official syntax and commands (for example, the SET command corresponds to the Strictredis.set method). Redis is a subclass of Strictredis for backwards compatibility with older versions of Redis-py. simply put, the official recommendation is to use the Strictredis method. The Redis class is not recommended because he and wedo not have the same operation in Redis-cli, the main difference is the following three aspects.
Redis Connected Shard Cluster
(1Redis-py did not provide redis-cluster support, go to GitHub to find a bit, there is a call redis-py-cluster source, but with Redis-py is not an author, the address is: https://Github.com/grokzen/redis-py-clusterwatch,star,fork is not too bad. (2) Install Latest stable release frompypi$ pip Install Redis-py-Cluster or fromsource$ python setup.py Install (3) using>>> fromrediscluster Import Strictrediscluster>>> Startup_nodes = [{"Host":"127.0.0.1","Port":"7000"}] >>> # note:decode_responses must beSetTo True when used with Python3>>> rc = Strictrediscluster (Startup_nodes=startup_nodes, decode_responses=True)>>> RC.Set("Foo","Bar") True>>> Print (RC.Get("Foo")) 'Bar'
Python connects to Redis Sentinel
>>> fromRedis.sentinel Import Sentinel>>> Sentinel = Sentinel ([('localhost',26379)], socket_timeout=0.1) >>> Sentinel.discover_master ('MyMaster') ('127.0.0.1',6379) >>> Sentinel.discover_slaves ('MyMaster') [('127.0.0.1',6380)] >>> master = sentinel.master_for ('MyMaster', socket_timeout=0.1) >>> slave = sentinel.slave_for ('MyMaster', socket_timeout=0.1) >>> Master.Set('Foo','Bar') >>> slave.Get('Foo') 'Bar'
Python String Type Usage Introduction
String Type basic operation: R.Set('Foo','Bar') print (R.Get('Foo')) R.mset (K1="v1", k2="v2") R.mget ('K1','K2') R.INCR ('Num') R.Get('Num') R.INCR ('Num') R.Get('Num')
Python Hash Type Usage Introduction
#插入hash类型键值r. Hset ("Hash1","K1","v1") R.hset ("Hash1","K2","v2"# Take all the Keyprint in the hash (R.hkeys ("Hash1") # The value of a single hash key corresponds to print (R.hget ("Hash1","K1") # Multiple hash keys correspond to the value print (R.hmget ("Hash1","K1","K2") ) #批量设置r. Hmset ("Hash2", {"K2":"v2","K3":"v3"}) #批量取出 print (R.hget ("Hash2","K2") # Single out"Hash2"The key-K2 corresponds to Valueprint (R.hmget ("Hash2","K2","K3") # Bulk out"Hash2"The key-K2 K3print (R.hmget ("Hash2", ["K2","K3"]) # Bulk out"Hash2"The key-K2 K3 corresponding value #取出所有的键值对hgetall (name)
Python List Type Usage introduction
#设置列表: R.lpush ("List1", One, A, -) #查询列表所有值print (R.lrange ('List1',0, -1) # Indicates a right-to-left set list R.rpush ("List2", One, A, -) # list length print (R.llen ("List2") # Slice out value, range is index number 0-3Print (R.lrange ("List2",0,3))
Python Set Type usage introduction
# add element R.sadd to the collection ("set1") # The length of the collection is 4print (R.scard ("set1")) # gets all the members in the collection print (R.smembers (" set1"))
Python sort set Type usage introduction
#对应的有序集合中添加元素r. Zadd ("Zset1", n1= One, n2= A) R.zadd ("Zset2",'M1', A,'m2', -) # Set length print (R.zcard ("Zset1") # Set length print (R.zcard ("Zset2") # gets all the elements in the ordered collection print (R.zrange ("Zset1",0, -1) # gets all elements and fractions in an ordered collection of 2print (R.zrange ("Zset2",0, -1, withscores=True)) For more information, see: Redis forPython Development Manual
Redis Multi-API development Practice