Python 使用pipeline 一次性操作 Redis

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haeasringnar/article/details/82958239

1、普通操作实例

import redis

# 连接池方式 db表示选择数据库
pool = redis.ConnectionPool(host='127.0.0.1',port=6379,db=0)
r = redis.Redis(connection_pool=pool)
r.set('name', 'world')
print(r.get('name'))

2、pipeline操作实例

import redis
import time

# 连接池方式 db表示选择数据库
pool = redis.ConnectionPool(host='127.0.0.1',port=6379,db=0)
r = redis.Redis(connection_pool=pool)

# 缓冲多条命令,然后一次性执行
pipe = r.pipeline()

# 正常情况下,50秒后会执行这些命令,在50秒之前去 数据库查看是没有值的
pipe.set('name', 'world')
time.sleep(50)
pipe.get('name')
pipe.execute()

猜你喜欢

转载自blog.csdn.net/haeasringnar/article/details/82958239