ImportREDISR=Redis. Redis (Host='1xx.x24.3xx.x0',#IP,password='hk139bc&*',#Passwordport=6379,#Port numberDb=1#The first few databases)#string format Operation#Change and deleteR.set ('Wahaha',"{A: ' 1 ', B: ' 2 '}") R.delete ('Wahaha') R.setex ('a','111', 50)#set the data for the expiration period of 50 seconds
Print (R.get (' a ')) #b ' 111 '
Print (R.get (' a '). Decode ()) #111
#带文件夹操作
R.setex (' A:aa ', ' 111 ', 50)
R.setex (' a:vaa ', ' 111 ', 50)
Print (R.get (' A:aa ')) #b ' 111 '
# Get all keys Print (R.keys ()) # get to all keys [B ' A ', B ' myself ', B ' stu_info_nhy ', b ' ytt ', B ' Bab ', B ' ab '] Print (R.keys ('a*')) # get to all the A starts with key [B ' A ', B ' ab ']
#hash type of dataR.hset ('Aaa:aa','a','{1: "Good Drink", 2: "B"}')Print(R.type ('Aaa:aa'))#B ' Hash 'Print(R.hget ('Aaa:aa','a'). Decode ())#{1: "Good Drink", 2: "B"}Print(R.hgetall ('Aaa:aa'))#{b ' a ': B ' {1: ' \xe5\xa5\xbd\xe5\x96\x9d ', 2: ' B '} '}R.expire ('Aaa:aa', 50)#Set Expiration TimePrint(R.ttl ('Aaa:aa'))#returns the time at which the current distance expires
R.hdel (' Stu_info ', ' Gyx ') #删除指定keyr. Delete (' Stu_info ') #删除整个key
Python Redis operations