python operation of the pipeline study Redis study notes

Some operations

import redis
#db =0 表示链接到index =0的数据库,decode_responses = True,放入数据库的value是str类型
pool = redis.ConnectionPool(host = 'localhost',port = 6379, db = 0,password = None,decode_responses = True)
r =  redis.StrictRedis(connection_pool = pool)

#删除,这个删除指的是删除Redis的数据类型,string,list,hash,set/zset
r.delete('school')

#检查是否存在name = age的数据
print(r.exists('age'))

#模糊匹配,还是只能匹配到name,不能匹配都里面的具体数据
print(r.keys('ex*'))

#重命名name
r.rename('example','example1')

#随机获取name
print(r.randomkey())

#获取所有name
print(r.keys())

Pipe is to be an atomic operation with a number of commands. Transaction parameters can be disabled using an atomic operation. r.pipeline (transaction = False).

import redis
#db =8表示链接到index =8的数据库,decode_responses = True,放入数据库的value是str类型
pool = redis.ConnectionPool(host = 'localhost',port = 6379, db = 8,password = None,decode_responses = True)
r =  redis.StrictRedis(connection_pool = pool)

#创建一个管道
pipe = r.pipeline()

pipe.set('name', 'jack')
pipe.set('role', 'sb')
pipe.sadd('faz', 'baz')
pipe.incr('num')    # 如果num不存在则vaule为1,如果存在,则value自增1
pipe.execute()

print(r.get("name"))
print(r.get("role"))
print(r.get("num"))


#管道可以将命令写在一起
pipe.set('hello', 'redis').sadd('faz', 'baz').incr('num').execute()
print(r.get("name"))
print(r.get("role"))
print(r.get("num"))

Guess you like

Origin blog.csdn.net/weixin_34200628/article/details/90968600