-
Notifications
You must be signed in to change notification settings - Fork 44
/
RedisQueue.cs
120 lines (96 loc) · 4.4 KB
/
RedisQueue.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using StackExchange.Redis;
using Gofer.NET;
namespace Gofer.NET
{
public class RedisQueue
{
private ConnectionMultiplexer Redis { get; }
/// <summary>
/// Redis Queue implements a FIFO data structure backed by a redis instance.
///
/// The "Left" of the list is considered to be the tail of the queue,
/// and the "Right" the head. Note this is opposite of the StackExchange.Redis
/// documentation. This is necessary to properly use the `ListRightPopLeftPushAsync`
/// method to transfer a value from the head of a queue to the tail of another
/// (used to assure that a message is consumed at-least once).
/// </summary>
/// <param name="redis"></param>
public RedisQueue(ConnectionMultiplexer redis)
{
Redis = redis;
}
public async Task<long> Count(RedisKey queueName)
{
return await Redis.GetDatabase().ListLengthAsync(queueName);
}
public async Task Push(RedisKey queueName, RedisValue value)
{
await Redis.GetDatabase().ListLeftPushAsync(queueName, value);
}
public async Task<RedisValue> Pop(RedisKey queueName)
{
return await Redis.GetDatabase().ListRightPopAsync(queueName);
}
public async Task<RedisValue> PopPush(RedisKey popFromQueueName, RedisKey pushToQueueName)
{
return await Redis.GetDatabase().ListRightPopLeftPushAsync(popFromQueueName, pushToQueueName);
}
public async Task<RedisValue> Peek(RedisKey queueName)
{
return await Redis.GetDatabase().ListGetByIndexAsync(queueName, -1);
}
public async Task<RedisValue> PeekTail(RedisKey queueName)
{
return await Redis.GetDatabase().ListGetByIndexAsync(queueName, 0);
}
/// <summary>
/// Removes the first inserted value from the queue. (Searches from head to tail of queue)
/// </summary>
/// <returns>false if no value was found to be removed, true otherwise.</returns>
public async Task<bool> Remove(RedisKey queueName, RedisValue value)
{
var removedCount = await Redis.GetDatabase().ListRemoveAsync(queueName, value, -1);
return Math.Abs(removedCount) == 1;
}
/// <summary>
/// Removes the last inserted value from the queue. (Searches from tail to head of queue)
/// </summary>
/// <returns>false if no value was found to be removed, true otherwise.</returns>
public async Task<bool> RemoveTail(RedisKey queueName, RedisValue value)
{
var removedCount = await Redis.GetDatabase().ListRemoveAsync(queueName, value, 1);
return Math.Abs(removedCount) == 1;
}
public async Task<IEnumerable<RedisValue>> PopBatch(RedisKey queueName, int batchSize)
{
var db = Redis.GetDatabase();
var transaction = db.CreateTransaction();
// `LRANGE` fetches batch values, but in LIFO order
var listValuesTask = transaction.ListRangeAsync(queueName, start: -batchSize, stop: -1);
var listTrimTask = transaction.ListTrimAsync(queueName, start: 0, stop: -batchSize-1);
await transaction.ExecuteAsync();
var listValues = await listValuesTask;
await listTrimTask;
// Convert list values to FIFO orer
return listValues.Reverse();
}
public async Task<IEnumerable<RedisValue>> PopAll(RedisKey queueName)
{
var db = Redis.GetDatabase();
var transaction = db.CreateTransaction();
// `LRANGE <list> 0 1` fetches all values, but in LIFO order
var listValuesTask = transaction.ListRangeAsync(queueName, start: 0, stop: - 1);
// `LTRIM <list> 1 0` removes all values
var listTrimTask = transaction.ListTrimAsync(queueName, start: 1, stop: 0);
await transaction.ExecuteAsync();
var listValues = await listValuesTask;
await listTrimTask;
// Convert list values to FIFO orer
return listValues.Reverse();
}
}
}