Fastrq基于 Redis 的隊(duì)列和堆棧
基于redis的隊(duì)列、雙向隊(duì)列、優(yōu)先隊(duì)列和堆棧,以及眾多增強(qiáng)版本
支持定長(zhǎng)。向滿隊(duì)列PUSH會(huì)失敗,向容量不足的隊(duì)列PUSH同樣會(huì)失敗。
支持可溢出。定長(zhǎng)隊(duì)列長(zhǎng)度超過(guò)容量限制,元素會(huì)從另一端溢出。
PUSH/POP支持批量操作
隊(duì)列類型:
Queue
FIFO
Deque
支持從前端/后端PUSH/POP
Capped Queue/Deque
容量固定
向一個(gè)滿的隊(duì)列PUSH會(huì)失敗
向一個(gè)容量不足的隊(duì)列PUSH會(huì)失敗
Overflow-able Capped Queue/Deque
隊(duì)列長(zhǎng)度超過(guò)容量自動(dòng)溢出
單向隊(duì)列從前端溢出
雙向隊(duì)列從PUSH端的另一端溢出
Priority Queue
分值越低,優(yōu)先級(jí)越高
Capped Priority Queue
容量固定
Overflow-able Capped Priority Queue
隊(duì)列長(zhǎng)度超過(guò)容量自動(dòng)溢出
溢出順序按優(yōu)先級(jí)從低到高
Stack
LIFO
Capped Stack
容量固定
源碼安裝
python setup.py install
pip安裝
pip install fastrq
使用
from fastrq.queue import Queue, CappedQueue
from fastrq.deque import Deque
from fastrq.stack import Stack
from fastrq.priorityqueue import PriorityQueue
# queue
q = Queue("fastrq_queue")
q.push(1)
q.push([2, 3])
q.ttl(10) # set the lifetime in seconds
q.range(0, -1) # got ['1', '2', '3']
q.range(0, 1) # got ['1', '2']
q.pop()
q.pop(2)
q.destruct() # destruct the queue
cq = CappedQueue("fastrq_capped_queue", 3)
cq.push(1)
cq.push(2)
cq.push([3, 4]) # got "err_qof"
cq.push(3)
cq.push(4) # got "err_qf"
of_cq = OfCappedQueue("fastrq_of_capped_queue", 3)
of_cq.push(1)
of_cq.push([2, 3, 4]) # "1" would be pushed out
# deque
dq = Deque("fastrq_deque")
dq.push_front([1, 2])
dq.push_back([3, 4])
dq.pop_front()
dq.pop_back()
# priority queue
pq = PriorityQueue("fastrq_priority_queue")
pq.push({'alibaba': 1})
pq.push({'google': 0, 'microsoft': 1})
pq.pop()
pq.pop(2)
# stack
s = Stack("fastrq_stack")
s.push([1,2,3])
s.pop()評(píng)論
圖片
表情
