![2b7f19fc9874e4733d82d6860175c41e.png](https://i-blog.csdnimg.cn/blog_migrate/b3845d6ef3c8617d5f3b5d9273072d8b.jpeg)
LPUSH key value [value ...]
RPUSH key value [value ...]
LPUSHX key value
RPUSHX key value
LPOP key
RPOP key
BLPOP key [key ...] timeout
BRPOP key [key ...] timeout
LRANGE key start stop
LTRIM key start stop
LLEN key
RPOPLPUSH source destination
BRPOPLPUSH source destination timeout
消息队列
排行榜
待阅消息
朋友圈
监控程序
keyname = "task:mq"
r.lpush(keyname, u"你好啊 不太灵光的程序员!")
r.lpush(keyname, u"更新个红锁")
消费者A 非阻塞
keyname = "task:mq"while 1:
message = r.rpop(keyname)print(message, datetime.now())
time.sleep(1)
消费者B 阻塞
keyname = "task:mq"while 1:
message = r.brpop(keyname, timeout=5)print(message, datetime.now())
import pandas as pdimport randomfor d in pd.date_range(start='2020-02-20', end='2020-02-25', freq='D'):
date = d.date()
tops = f"article:tops:{date}"for title in [f"《Redis实现消息队列的6种方案》 访问量 " + str(random.randint(1000, 2000)),
f"《让运维更简单的7种定时任务实现方式》 访问量 " + str(random.randint(800, 1000)),
f"《细品28岁程序员退休创业背后的可怕故事》 访问量 " + str(random.randint(600, 800)),
f"《工作中都有哪些让你心累的时刻》 访问量 " + str(random.randint(100, 500)),]:
details = r.rpush(tops, title)
查看日榜数据
import pandas as pdfor d in pd.date_range(start='2020-02-20', end='2020-02-25', freq='D'):
date = d.date()
tops = f"article:tops:{date}"print(f"{date} 日榜 ")for index in range(0, 10, 2):
details = r.lrange(tops, index, index+1)if details:print(details)else:print("没有下一页了")break
moments1 = """
老干妈:
我冤枉啊
2020-07-01
"""
moments2 = """
XXX:
老干妈上架大客户专属1000瓶辣椒酱 售价9999
2020-07-02
"""
moments3 = """
XXX:
腾讯公司未展开调查直接冻结老干妈1600万
2020-07-02
"""
keyname = "user:1000:moments"
r.lpush(keyname, moments1, moments2, moments3)print("今天新的朋友圈消息待刷新", r.llen(keyname))while 1:print(r.rpop(keyname))
time.sleep(2)
def get_api(url):print(url, "successful")
time.sleep(2)
r.lpush("task:mq", "api1", "api2", "api3", "api4")while 1:
url = r.brpoplpush("task:mq", "task:mq", timeout=30)
get_api(url)
end
推荐阅读:
让运维更简单的7种定时任务实现方式
细品28岁程序员退休创业背后的可怕故事
Python行业就业现状
基于Redis实现消息队列(上)
基于Redis实现消息队列(中)
基于Redis实现消息队列(下)
如有收获,点个在看,诚挚感谢