python redis demo

The Code, redis-demo

. 1  # ! / Usr / bin / Python the env 
2  # _ * _ Coding: UTF-_ * _. 8 
. 3  
. 4  Import Redis
 . 5  
. 6  
. 7  # ### configuration parameters 
. 8 Host = ' 192.168.0.12 ' 
. 9 Port = 16000
 10 password = ' wrfg6OTNaXTqd96H7TK7bYIV ' 
. 11  
12 is  
13 is  # ### 
14  # connection Redis 
15  # single value 
16  DEF redis_conn (Host, Port, password, DB, Key, ACT = " GET " , value = '' ):
. 17      R & lt redis.StrictRedis = (= Host Host, Port = Port, password = password, DB = DB, decode_responses = False)
 18 is  
. 19      # determines the type of key 
20 is      S = r.type (key)
 21 is      Print ( ' type of key: % s ' % S)
 22 is  
23 is      # set Key 
24      IF ACT == " set " :
 25              r.set (Key, value)
 26 is              Print ( ' has been completed set, db% s, key =% s, % s value ' % (DB, Key, value))
 27              return value
 28 
29      IF r.exists (key):
 30          # query 
31 is          IF ACT == " GET " :
 32              # take the key value 
33 is              value = r.get (key)
 34 is              Print ( ' are finding db% s, key =% s \ % S value = n- ' % (DB, key, value))
 35              return value
 36  
37 [          # remove 
38 is          elif ACT == " del " :
 39              # take the key value 
40              value = r.get (key)
 41 is             r.delete (Key)
 42 is              Print ( ' has been deleted, db% s, key =% s, \ n value S% ' % (DB, Key, value))
 43 is              return   
44 is      the else :
 45          Print ( ' Key does not exist ' )
 46 is  
47  
48 Key = " old_user_pop_win_count_20190910_22 " 
49 DB = 2
 50  # calling function, GET 
51 is Result = redis_conn (Host, Port, password, DB, Key, ACT = " GET " )
 52 is  Print ( ' Result: ' , Result )
53 
54 #del
55 result=redis_conn(host,port,password,db,key,act="del")
56 print('result:',result)
57 
58 
59 #set
60 db=1
61 key="age"
62 result=redis_conn(host,port,password,db,key,act="set",value="88")

 

Guess you like

Origin www.cnblogs.com/lisa2016/p/11499100.html