String python Operation Basic Operation of the Redis study notes

Redis commands the operation of String

import redis
#db =2 表示链接到index =2的数据库,decode_responses = True,放入数据库的value是str类型,否则默认是byte类型
pool = redis.ConnectionPool(host = 'localhost',port = 6379, db = 2,decode_responses = True)
r =  redis.StrictRedis(connection_pool = pool)
#设置键值对
r.set('gender','male')
print(r.get('gender'))
#只有当key不存在时才进行操作
print(r.set('zoo','dog',nx = True))
#只有当key存在时,才进行操作
print(r.set('zoo','dog',xx = True))
#批量设置
r.mset({'k1':'v1','k2':'v2'})
#批量获取,得到一个列表
print(r.mget('k1','k2'))
#参数也可以是列表
print(r.mget(['k1','k2','gender']))
#设置新值的同时获取旧值
print(r.getset('k1','123456789'))
print(r['k1'])
#对k1的value进行切片操作
print(r.getrange('k1',1,5))
#从index = 4处替换k1的value为mgh
r.setrange('k1',4,'mgh')
print(r['k1'])
#返回value的长度
print(r.strlen('k1'))
#当key不存在时则出创建,否则自增,m默认是1(因为默认0,然后自增1),amount 可以修改增加的值
r.incr('helloworld',amount = 10)
print(r['helloworld'])
#和上面一样,自增换成了float
r.incrbyfloat('helloworld',amount = 1.0)
print(r['helloworld'])
#自减。和自增相反.如果不存在默认是-1(因为默认0,然后自减1).
r.decr('theworld',amount = 10)#自减10
print(r['theworld'])
#在value后便追加'zzzzzzzzzz'
r.append('k1','zzzzzzzzz')
print(r['k1'])

The results obtained

male
True
True
['v1', 'v2']
['v1', 'v2', 'male']
v1
123456789
23456
1234mgh89
9
10
11
-10
1234mgh89zzzzzzzzz

The operation is complete database screenshot


17778542-467eb030dcf6233c.png
Visual Database Screenshot

Guess you like

Origin blog.csdn.net/weixin_34329187/article/details/90968599