Redis

Redis 是一个开源的基于内存的数据结构存储器。通常可作为数据库,缓存和消息中介。它支持的数据结构有:字符串、哈希表、列表、集合、支持范围查询的有序集合、位图、hyperloglogs和带查询半径的地理空间索引。Redis有内置的复制、Lua脚本、LRU缓存、事务和不同层级的磁盘持久化功能,还通过Redis Sentinel提供了高可用性,通过Redis集群实现了自动化分割。

Pipeline

当业务需要发送多个相互独立的消息给redis,而不需要阻塞等待回答的时候,可以使用pipeline,将几个消息打包发送,这样可以减少网络的传输次数,提高性能。

#!/usr/bin/python3
import redis
import time key = 'Redis:Test'
num = 100 def test_with_out_pipeline(count=1):
r = redis.Redis(host='192.168.192.34',port=6379)
for i in range(num):
r.incr(key, count) def test_with_pipeline(count=1):
r = redis.Redis(host='192.168.192.34', port=6379)
pipe = r.pipeline()
for i in range(num):
pipe.incr(count)
res = pipe.execute()
return res def bench(desc):
start = time.clock()
desc()
end = time.clock()
cost = end - start
print("function {} cost {}".format(desc.__name__, str(cost))) if __name__ == '__main__':
bench(test_with_out_pipeline)
bench(test_with_pipeline)

使用场景,通过redis记录某种现象出现的次数,例如消费次数。pipline有优点,那就肯定有缺点,缺点是:

1.如果pipeline包含的信息太长,redis在处理的时候就会占用更多的内存。同时pipeline在处理的时候回独占链接(没仔细研究过),这期间的其他任何操作都会失败。建议就是给pipeline使用一个单独的Client。

2.pipeline只是将数据批量打包发送,很可能里面有部分请求处理失败的情况。这个时候使用LUA脚本会更加合适,而且LUA脚本可以保证原子性。

参考:

http://shift-alt-ctrl.iteye.com/blog/1863790

http://www.redis.cn/commands/eval.html

List

Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素导列表的头部(左边)或者尾部(右边)

一个列表最多可以包含 232 - 1 个元素 (4294967295, 每个列表超过40亿个元素)。

#!/usr/bin/python3
import redis
import time key = 'Redis:ListTest' if __name__ == '__main__':
r = redis.Redis(host='192.168.192.34',port=6379)
r.lpush(key, '1')
r.lpush(key, '2')
r.rpush(key, '3')
print(r.llen(key))
res = r.rpop(key)
print(res)
res = r.lpop(key)
print(res)

在做爬虫的过程中,需要保留从网页上分析之后的URL,那就可以使用列表。可以使用不同的key来保存不同的优先级队列,在爬取得时候先获取优先级最高的队列,如果该队列为空,获取优先级比较低的队列。

Redis pipeline and list的更多相关文章

  1. redis pipeline

    redis pipeline 简而言之就是把多个redis命令打包,一起发送给redis server,并且一起返回结果,减少客户端和服务器之间的多次“折返跑”

  2. 如何用好redis pipeline

    编者注:pipeline是Redis的一个提高吞吐量的机制,适用于多key读写场景,比如同时读取多个key的value,或者更新多个key的value.工作过程中发现挺多小伙伴都对pipeline多少 ...

  3. 【spring boot】spring boot 基于redis pipeline 管道,批量操作redis命令

    spring boot 2.x 使用RedisTemplate 操作 =================================== 1.pom.xml <!--spring2.0集成r ...

  4. laravel中redis pipeline用法说明

    $res = Redis::pipeline(function($pipe) use($params) { for ($i = 0; $i < 1000; $i++) { $pipe->g ...

  5. redis 学习(11)-- redis pipeline

    redis pipeline 什么是流水线(pipeline) 首先来看 redis 执行一次操作所需要的时间: 1 次时间 = 1 次网络时间 + 1次命令时间 执行 n 次就需要: n 次时间 = ...

  6. 等待 Redis 应答 Redis pipeline It's not just a matter of RTT

    小结: 1.When pipelining is used, many commands are usually read with a single read() system call, and ...

  7. 说说 Redis pipeline

    更多技术文章,请关注我的个人博客 www.immaxfang.com 和小公众号 Max的学习札记. Redis 客户端和服务端之间是采用 TCP 协议进行通信的,是基于 Request/Respon ...

  8. (7)redis pipeline

    redis是一个cs模式的tcp server,使用和http类似的请求响应协议.一个client可以通过一个socket连接发起多个请求命令.每个请求命令发出后client通常 会阻塞并等待redi ...

  9. Redis Pipeline原理分析

    转载请注明出处:http://www.cnblogs.com/jabnih/ 1. 基本原理 1.1 为什么会出现Pipeline Redis本身是基于Request/Response协议的,正常情况 ...

随机推荐

  1. ios开发理解nil,Nil, NULL

    nil是一个对象指针为空,Nil是一个类指针为空,NULL是基本数据类型为空.这些可以理解为nil,Nil, NULL的区别吧. iOS剪切板 UIPasteboard *pasteboard = [ ...

  2. Centos挂载windows共享文件夹

    1.windows7共享一个文件夹(1)新建一个用户:devin,密码:admin123(2)在E盘新建一个文件夹,share,并设置共享 对用户devin共享,并让其权限为:读取和写入. 2.lin ...

  3. Linux命令-free

    显示系统内存使用情况 free  [-m / -g / -h] [root@localhost test]# free -m total used free shared buff/cache ava ...

  4. Android AlarmManager类的应用(实现闹钟功能)

    1.AlarmManager,顾名思义,就是“提醒”,是Android中常用的一种系统级别的提示服务,可以实现从指定时间开始,以一个固定的间隔时间执行某项操作,所以常常与广播(Broadcast)连用 ...

  5. linux 大量的TIME_WAIT解决办法

    发现存在大量TIME_WAIT状态的连接tcp        0      0 127.0.0.1:3306              127.0.0.1:41378             TIME ...

  6. PHP也20岁了

    当今许多世界著名的编程语言的年纪已经够大了.举个例子,PHP昨天过了生日已经20岁了,Python也24岁,HTML已经服务了22年,Ruby和JavaScript有20年,Java前段时间刚过了20 ...

  7. 用root直接登入ubuntu 14_04

    官网下载地址:http://www.ubuntu.com/download/desktop64位桌面版:http://www.ubuntu.com/ubuntu-releases/14.04/ubun ...

  8. leetcode:Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  9. jQuery中filter(),not(),split()的用法

    filter(),not(): <script type="text/javascript"> $(document).ready(function() { //输出 ...

  10. Excel文件操作方式比较

    C++读取Excel的XLS文件的方法有很多,但是也许就是因为方法太多,大家在选择的时候会很疑惑. 由于前两天要做导表工具,比较了常用的方法,总结一下写个短文, 1.OLE的方式 这个大约是最常用的方 ...