The first thing to do is pip install Redis
Import Redis,json
ip=' 211.149.218.16 '
port=6379
db=0
Passwd=' 123456 '
R = Redis. Redis (Host=ip,Port=port,Db=db,PASSWORD=PASSWD)#链接 Redis
R.set (' Name ',' Nhy ')#如果key一样则冲掉原来的值
R.set (' Name ', [1,2,3,4])
#r. Set (' name ', {' name ': ' hehehe '})
Name = R.get (' Name ')
Name.decode ()#转成字符串, String type, Redis contains string, bytes type, to be converted to string with decode ()
New_name = Json.loads (Name.decode ())#用json转成list
Print (name)
Print (new_name)
PrintType (new_name))
R.setex (' Nhy ',' Lingyul ',10)#设置key, value and time of failure seconds
R.mset (Name=' HH ',Age=' 18 ')#批量set值, batch setup cannot set expiration time
Print (R.keys (' *n* '))#打印所有key
#哈希类型的key
R.hset (' Lingyul ',' User ',' Lingyul ') #r. Hset (' key ', ' key ', ' value ') to get the result:
# row key value
# 1 user lingyul
print (R.hget ( ' Lingyul ', #单独获取指定的name的值
R.hgetall ( Lingyul ') #全部获取
R.delete ( ' key ') #吧整个key都删掉
R.hdel ( ' Lingyul ', ' user ') #删除指定的key
r.set ( ' User:lingyul ', ' hahaha ') #如果设置的key里面有冒号, first create a folder to the left of the colon, There are key and value under the folder, and the hash type is the same
 
Redis Database Operations