redis python api operation

single database embodiment redis

. 1  # Redis single connector embodiment 
2  '' ' 
. 3  Redis-Server redis_diy.conf
 . 4  ' '' 
. 5  Import Redis
 . 6 Conn = redis.StrictRedis (= Host ' 192.168.160.135 ' , Port = ' 6380 ' , DB = 0, password = ' 123456 ' ) # redis profile, db redis database designated isolation regions, there are a total of 16 0-15, default is 0 
. 7  Print (conn.dbsize ())
 . 8  Print (conn.keys ())
 . 9 Conn. SET ( ' Age ' , 18 is )
 10  Print (conn.keys ())
11 print(conn.get('name'))
12 print(conn.exists('name'))
single database embodiment redis

redis-sentinel (Sentinel)

. 1  # redis-Sentinel connector 
2  '' ' 
3  start redis server (6390 primary library):
 . 4      redis-Server msconf / redis-6390.conf
 . 5      redis-Server msconf / redis-6391.conf
 . 6      redis-Server msconf / redis -6392.conf
 . 7      Redis-Server msconf / Redis-6393.conf
 . 8  start Sentinel functions:
 . 9      Redis-Sentinel redis_sentinel / Redis-sentinel_26390.conf
 10      Redis-Sentinel redis_sentinel / Redis-sentinel_26391.conf
 . 11      Redis-Sentinel redis_sentinel / redis- sentinel_26392.conf
 12 is  ' '' 
13 is  
14  # # import redis sentinel package 
15  fromredis.sentinel Import the Sentinel
 16  # specified sentinel address and port number (connection Sentinel) 
. 17 sentinel the Sentinel = ([( ' 192.168.160.135 ' , 26390), ( ' 192.168.160.135 ' , 26391), ( ' 192.168.160.135 ' , 26392)], socket_timeout = 0.1 )
 18 is  # test, obtain the following information from the main library and the library: mymaster Sentinel configuration file is specified monitoring the main library 
. 19  Print (sentinel.discover_master ( ' mymaster ' )) # host library information 
20 is  Print (sentinel.discover_slaves ( ' mymaster ' )) #From the library information 
21 is  
22 is  # separate read and write configuration # 
23  # write node: main library 
24 Master sentinel.master_for = ( ' mymaster ' , password = 123456, socket_timeout = 0.1) # Note that the master library set security mode password 
25  Print ( Master)
 26 is  # read node: from the library 
27 Slave = sentinel.slave_for ( ' mymaster ' , password = 123456, socket_timeout = 0.1) # Note that the security mode from the library set password 
28  Print (Slave)
 29  
30  # separate read and write tests 
31 is  Print (master.dbsize ())
 32  Print(master.keys())
33 print(slave.keys())
34 master.set('name4',4)
35 print(slave.keys())
36 print(slave.get('name4'))
redis-sentinel (Sentinel)

redis-cluster (cluster)

1  # rediscluster cluster: HTTPS: //github.com/Grokzen/redis-py-cluster 
2  # PIP install Redis-Py-Cluster 
3  from rediscluster Import StrictRedisCluster
 4  # is little to link the cluster a database, a plurality prevent some databases will hang 
. 5 startup_nodes = [{ " Host " : " 192.168.160.135 " , " Port " : " 7000 " }, { " Host " : " 192.168.160.135 " , " Port " :"7001"},
 6                  {"host": "192.168.160.135", "port": "7002"}, {"host": "192.168.160.135", "port": "7003"},
 7                  {"host": "192.168.160.135", "port": "7004"}, {"host": "192.168.160.135", "port": "7005"} ]
 8 rc = StrictRedisCluster(startup_nodes=startup_nodes,password=123456, decode_responses=True)
 9 rc.set('name1',1234)
10 print(rc.keys())
11 print(rc.dbsize())
12 print(rc.get('name1'))
redis-cluster (cluster)

 

Guess you like

Origin www.cnblogs.com/open-yang/p/11256449.html