The following sample code includes 2 demos:
One is an example of how to add, modify, add, reduce, and delete string type data;
The other is an example of how to add, modify, add, and delete sorted set type data.
More Redis operation commands can be referenced in Appendix 2.
First, of course, you need to install the Python Redis Library and install it using the PIP command:
Pip Install Redis
The Python Operation Redis sample code is as follows:
Import Redis
Pool = Redis. ConnectionPool (host= ' 192.168.1.1 ', port=7379,db=0)
R = Redis. Strictredis (Connection_pool=pool)
Sname= ' Stringtest '
Stname= ' Sortedtest '
Member= ' abc '
Modify_score=8 #修改值
Add_score=1 #增加值
#操作string数据示例
R.set (sname, 123) #设置
Print R.get (sname) #获取
R.INCR (sname,456) #增加
Print R.get (sname)
R.DECR (sname,123) #减少
Print R.get (sname)
R.delete (sname) #删除
Print R.get (sname)
#操作Sorted Set Data sample
R.zadd (Stname,modify_score,member) #修改
Print Member,r.zscore (stname, member) #获取
R.zincrby (Stname, Member, Add_score) #增加
Print Member,r.zscore (Stname, member)
R.zrem (Stname, member) #删除
Print Member,r.zscore (Stname, member)
Execution results:
>>>
123
579
456
None
ABC 8.0
ABC 9.0
ABC None