Redis collection 03 (data type: list type (List), python and redis interaction)

Redis collection 03 (data type: list type (List), python and redis interaction)

python learning directory portal

List data type (List)

  • Features
1、元素是字符串类型
2、列表头尾增删快,中间增删慢,增删元素是常态
3、元素可重复
4、最多可包含2^32 -1个元素
5、索引同python列表
  • List commonly used commands
# 增
1、从列表头部压入元素
	LPUSH key value1 value2 
    返回:list长度
2、从列表尾部压入元素
	RPUSH key value1 value2
    返回:list长度
3、从列表src尾部弹出1个元素,压入到列表dst的头部
	RPOPLPUSH src dst
    返回:被弹出的元素
4、在列表指定元素后/前插入元素
	LINSERT key after|before value newvalue
    返回:
		1,如果命令执行成功,返回列表的长度
		2,如果没有找到 pivot ,返回 -1
		3,如果 key 不存在或为空列表,返回 0 

# 查
5、查看列表中元素
	LRANGE key start stop
  # 查看列表中所有元素: LRANGE key 0 -1
6、获取列表长度
	LLEN key

# 删
7、从列表头部弹出1个元素
	LPOP key
8、从列表尾部弹出1个元素
	RPOP key
9、列表头部,阻塞弹出,列表为空时阻塞
	BLPOP key timeout
10、列表尾部,阻塞弹出,列表为空时阻塞
	BRPOP key timeout
  # 关于BLPOP 和 BRPOP
  	1、如果弹出的列表不存在或者为空,就会阻塞
	2、超时时间设置为0,就是永久阻塞,直到有数据可以弹出
	3、如果多个客户端阻塞再同一个列表上,使用First In First Service原则,先到先服务
11、删除指定元素
	LREM key count value  -3 a
    count>0:表示从头部开始向表尾搜索,移除与value相等的元素,数量为count
	count<0:表示从尾部开始向表头搜索,移除与value相等的元素,数量为count
	count=0:移除表中所有与value相等的值
    返回:被移除元素的数量
    
12、保留指定范围内的元素
	LTRIM key start stop
    返回:ok
    样例:
  		LTRIM mylist1 0 2 # 只保留前3条
  		# 应用场景: 保存微博评论最后500条
  		LTRIM weibo:comments 0 499
# 改
13、将列表 key 下标为 index 的元素的值设置为 value
	LSET key index newvalue

Exercise

1、查看所有的键
   #keys *
2、向列表 spider:urls 中以RPUSH放入如下几个元素:01_baidu.com、02_taobao.com、03_sina.com、04_jd.com、05_xxx.com
   # rpush spider:urls 01_baidu.com 
3、查看列表中所有元素
   # lrange spider:urls 0 -1
4、查看列表长度
   # llen spider:urls
5、将列表中01_baidu.com 改为 01_tmall.com
   #lset spider:urls 0 01_tmall.com
6、在列表中04_jd.com之后再加1个元素 02_taobao.com
   #linsert spider:urls after 04_jd.com 02_taobao.com
7、弹出列表中的最后一个元素
   # rpop spider:urls
8、删除列表中所有的 02_taobao.com
   # lrem spider:urls 0 02_taobao.com
9、剔除列表中的其他元素,只剩前3# ltrim spider:urls 0 2

python interactive redis

  • Module (redis)

Ubuntu

检查  pip3 freeze|grep 'redis'

安装  sudo pip3 install redis
  • manual
import redis
# 创建数据库连接对象
r = redis.Redis(host='127.0.0.1',port=6379,db=0,password='123456')
  • Common command code example
import redis
r = redis.Redis(host='127.0.0.1', port=6379, db=3, password='123456')

# key_list = r.keys('*')
# for key in key_list:
#     print(key.decode())

#print(r.exists('k2'))
  • python operation list
# r.lpush('pyl1', 'Tom', 'Jack', 'Lili')
# #[b'Lili', b'Jack', b'Tom']
# print(r.lrange('pyl1', 0, -1))
#b'Tom'
#print(r.rpop('pyl1'))

List case: One process is responsible for production tasks, and one process is responsible for consumption tasks

Process 1: Producer

import redis
r = redis.Redis(host='127.0.0.1', port=6379, db=3, password='123456')

#任务类型(email)_发件人_收件人_内容
task_str = '%s_%s_%s_%s'%('email','guoxiaonao', 'wangweichao', 'hahaha')
r.lpush('pypc', task_str)


Process 2: Consumer

import redis

r = redis.Redis(host='127.0.0.1', port=6379, db=3, password='123456')

while True:
    task = r.brpop('pypc', 10)
    #(b'pypc', b'email_guoxiaonao_wangweichao_hahaha')
    #print(task)
    if task:
        task_str = task[1].decode()
        task_list = task_str.split('_')
        print(task_list)

  • python operation string
#####string#######
# r.set('pys1', 'guoxiaonao')
# print(r.get('pys1'))
# r.mset({'pys2':'wang', 'pys3':'wei'})
# print(r.mget('pys2','pys3'))

#数值操作
#返回值 int
#print(r.incrby('pys6', 10))
#返回值 字节串
#print(r.get('pys6'))

Here, the editor is very sincere to thank you for reading this article. If it is helpful to your technical learning, I hope you can offer your valuable three combos [Like, Favorite, Share], your encouragement, comment and Suggestion is my biggest motivation! I am a tenacious and hard worker in the Internet industry! Ollie

Guess you like

Origin blog.csdn.net/weixin_38640052/article/details/107812567
Recommended