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

运行结果:

最新文章

  1. java分享第十八天-02( java结合testng,利用XML做数据源的数据驱动)
  2. OC的runtime运行机制
  3. java 程序中添加socks 5代理
  4. 【Ubuntu】NAT配置
  5. python中关于正则表达式一
  6. ubuntu14.04 swap not avalible交换分区不能使用
  7. Apache调优
  8. JVM调优的几种策略(转)
  9. Linux下is not in the sudoers file(转)
  10. 《剑指offer》数组中出现次数超过数组长度一半的数字
  11. ionic start 创建ionic项目报错,及解决过程
  12. 【一天一道LeetCode】#97. Interleaving String
  13. windows命令中的cd
  14. php生成随机字符串可指定纯数字、纯字母或者混合的
  15. c++ <stdarg.h> 解决变参问题的宏定义;
  16. windows10环境下VMware14中Ubuntu16.04解决如何上网问题
  17. jQuery取得radio的值 取select得值
  18. 从App业务逻辑中提炼API接口
  19. ORM 对象关系映射
  20. 数据库时间内接受的是lang类型的时间 分为三种字段 第一种只存日期 第二种存日期+时间 第三种时间戳

热门文章

  1. C语言变量的声明位置
  2. Effective C++ 38-42
  3. vue provide和inject使用
  4. 几款Android开发人员必备小工具
  5. Druid register mbean error
  6. [转载]打造自己喜欢的Linux桌面----archlinux
  7. 获取JSON格式的字符串各个属性对应的值
  8. HDUOJ-----1066Last non-zero Digit in N!
  9. jquery.masonry瀑布流插件的4个使用步骤
  10. C语言訪问MySQL数据库的方法