python管道命令_Python-Redis-常用操作&管道

other 方法

print(r.get('name')) # 查询key为name的值

r.delete("gender") # 删除key为gender的键值对

print(r.keys()) # 查询所有的Key

print(r.dbsize()) # 当前redis包含多少条数据

r.save() # 执行"检查点"操作,将数据写回磁盘。保存时阻塞

# r.flushdb() # 清空r中的所有数据

管道(pipeline)

redis默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,

如果想要在一次请求中指定多个命令,则可以使用pipline实现一次请求指定多个命令,并且默认情况下一次pipline 是原子性操作。

管道(pipeline)是redis在提供单个请求中缓冲多条服务器命令的基类的子类。它通过减少服务器-客户端之间反复的TCP数据库包,从而大大提高了执行批量命令的功能。

import redis

import time

pool= redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)

r= redis.Redis(connection_pool=pool)

# pipe= r.pipeline(transaction=False) # 默认的情况下,管道里执行的命令可以保证执行的原子性,执行pipe = r.pipeline(transaction=False)可以禁用这一特性。

# pipe= r.pipeline(transaction=True)

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"))

other 方法

print(r.get('name')) # 查询key为name的值

r.delete("gender") # 删除key为gender的键值对

print(r.keys()) # 查询所有的Key

print(r.dbsize()) # 当前redis包含多少条数据

r.save() # 执行"检查点"操作,将数据写回磁盘。保存时阻塞

# r.flushdb() # 清空r中的所有数据

管道(pipeline)

redis默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,

如果想要在一次请求中指定多个命令,则可以使用pipline实现一次请求指定多个命令,并且默认情况下一次pipline 是原子性操作。

管道(pipeline)是redis在提供单个请求中缓冲多条服务器命令的基类的子类。它通过减少服务器-客户端之间反复的TCP数据库包,从而大大提高了执行批量命令的功能。

import redis

import time

pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)

r = redis.Redis(connection_pool=pool)

# pipe = r.pipeline(transaction=False) # 默认的情况下,管道里执行的命令可以保证执行的原子性,执行pipe = r.pipeline(transaction=False)可以禁用这一特性。

# pipe = r.pipeline(transaction=True)

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"))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值