redis性能提升
2024-08-26 10:53:27
1.redis请求执行原理
redis客户端与redis服务器之间使用TCP协议进行连接,一个科幻可以通过一个socket连接发送多个请求命令,
但每个请求命令发出后client通常会阻塞并等待redis服务器处理,redis服务器处理完毕后会将结果通过响应报文返回给client,因此当执行多条命令的时候都需要等待上一条命令执行完毕后才能执行。
2.合并请求
每次向redis服务器执行请求,都是一次TCP round trip,
因此将请求合并,作为一次请求,可以很好的提升性能。
示例如下:
import redis
cache = redis.Redis('127.0.0.1',
6379,
db=3,
password='test',
decode_responses=True
)
cache.delete('test_set')
for i in range(10):
cache.sadd('test_set',i)
print(cache.sinter('test_set'))
运行结果:
import redis
cache = redis.Redis('127.0.0.1',
6379,
db=3,
password='test',
decode_responses=True
)
values = [0,1,2,3,4,5,6,7,8,9]
cache.delete('test_set')
cache.sadd('test_set',*values)
print(cache.sinter('test_set'))
运行结果:
3.批量处理
每次向redis服务器发送请求,最后发送批量执行命令,redis服务器执行完毕,返回响应。
当不能合并请求时,如每次sadd的key值不同,可使用批量处理的方式,提升性能。
示例如下:
import redis
cache = redis.Redis('127.0.0.1',
6379,
db=3,
password='test',
decode_responses=True
)
for i in range(10):
cache.delete('test_set_'+str(i))
for i in range(10):
cache.sadd('test_set_'+str(i),i)
for i in range(10):
print(cache.sinter('test_set_'+str(i)))
运行结果:
import redis
cache = redis.Redis('120.26.217.149',
6379,
db=3,
password='51yunchedevredis',
decode_responses=True
)
pipeline = cache.pipeline()
for i in range(10):
pipeline.delete('test_set_'+str(i))
for i in range(10):
pipeline.sadd('test_set_'+str(i),i)
pipeline.execute()
for i in range(10):
print(cache.sinter('test_set_'+str(i)))
运行结果:
最新文章
- java分享第十八天-02( java结合testng,利用XML做数据源的数据驱动)
- OC的runtime运行机制
- java 程序中添加socks 5代理
- 【Ubuntu】NAT配置
- python中关于正则表达式一
- ubuntu14.04 swap not avalible交换分区不能使用
- Apache调优
- JVM调优的几种策略(转)
- Linux下is not in the sudoers file(转)
- 《剑指offer》数组中出现次数超过数组长度一半的数字
- ionic start 创建ionic项目报错,及解决过程
- 【一天一道LeetCode】#97. Interleaving String
- windows命令中的cd
- php生成随机字符串可指定纯数字、纯字母或者混合的
- c++ <;stdarg.h>; 解决变参问题的宏定义;
- windows10环境下VMware14中Ubuntu16.04解决如何上网问题
- jQuery取得radio的值 取select得值
- 从App业务逻辑中提炼API接口
- ORM 对象关系映射
- 数据库时间内接受的是lang类型的时间 分为三种字段 第一种只存日期 第二种存日期+时间 第三种时间戳