Skip to content

Commit db17b89

Browse files
gerzseshacharPash
andauthored
Add more blocking commands (#253)
* Rename existing blocking commands The current naming for the blocking commands does not feel right. For example "BzmPop", it feels like there are no naming rules. So rename existing methods by this rule: Use capital letters at the beginning of human-readable words, e.g. "Pop", and also use capital letters where the Redis command has a single letter, e.g. "B", which stands for "Blocking", "Z" which stands for "Set" and "M" which stands for "Multi". So the new name is "BZMPop". It's still a good time to do such renaming, because these commands have not been released. Also it is a crucial moment to get the naming as right as possible, for the same reason. * Add ToString to RedisValueWithScore * Add support for BLPOP and BRPOP commands Issues #249 and #250 * Add support for the BLMPOP command Issue #248 * Add support for the BLMOVE and BRPOPLPUSH commands Issues #235 and #251 * Try to fix flaky test * Use Redis fixture in CoreTests * Remove ToString, not really needed --------- Co-authored-by: Gabriel Erzse <[email protected]> Co-authored-by: shacharPash <[email protected]>
1 parent 91fd35c commit db17b89

File tree

8 files changed

+669
-121
lines changed

8 files changed

+669
-121
lines changed

src/NRedisStack/CoreCommands/CoreCommandBuilder.cs

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static SerializedCommand ClientSetInfo(SetInfoAttr attr, string value)
2121
return new SerializedCommand(RedisCoreCommands.CLIENT, RedisCoreCommands.SETINFO, attrValue, value);
2222
}
2323

24-
public static SerializedCommand BzmPop(double timeout, RedisKey[] keys, MinMaxModifier minMaxModifier, long? count)
24+
public static SerializedCommand BZMPop(double timeout, RedisKey[] keys, MinMaxModifier minMaxModifier, long? count)
2525
{
2626
if (keys.Length == 0)
2727
{
@@ -44,21 +44,72 @@ public static SerializedCommand BzmPop(double timeout, RedisKey[] keys, MinMaxMo
4444
return new SerializedCommand(RedisCoreCommands.BZMPOP, args);
4545
}
4646

47-
public static SerializedCommand BzPopMin(RedisKey[] keys, double timeout)
47+
public static SerializedCommand BZPopMin(RedisKey[] keys, double timeout)
48+
{
49+
return BlockingCommandWithKeysAndTimeout(RedisCoreCommands.BZPOPMIN, keys, timeout);
50+
}
51+
52+
public static SerializedCommand BZPopMax(RedisKey[] keys, double timeout)
53+
{
54+
return BlockingCommandWithKeysAndTimeout(RedisCoreCommands.BZPOPMAX, keys, timeout);
55+
}
56+
57+
public static SerializedCommand BLMPop(double timeout, RedisKey[] keys, ListSide listSide, long? count)
4858
{
4959
if (keys.Length == 0)
5060
{
5161
throw new ArgumentException("At least one key must be provided.");
5262
}
5363

5464
List<object> args = new List<object>();
65+
66+
args.Add(timeout);
67+
args.Add(keys.Length);
5568
args.AddRange(keys.Cast<object>());
69+
args.Add(listSide == ListSide.Left ? CoreArgs.LEFT : CoreArgs.RIGHT);
70+
71+
if (count != null)
72+
{
73+
args.Add(CoreArgs.COUNT);
74+
args.Add(count);
75+
}
76+
77+
return new SerializedCommand(RedisCoreCommands.BLMPOP, args);
78+
}
79+
80+
public static SerializedCommand BLPop(RedisKey[] keys, double timeout)
81+
{
82+
return BlockingCommandWithKeysAndTimeout(RedisCoreCommands.BLPOP, keys, timeout);
83+
}
84+
85+
public static SerializedCommand BRPop(RedisKey[] keys, double timeout)
86+
{
87+
return BlockingCommandWithKeysAndTimeout(RedisCoreCommands.BRPOP, keys, timeout);
88+
}
89+
90+
public static SerializedCommand BLMove(RedisKey source, RedisKey destination, ListSide sourceSide, ListSide destinationSide, double timeout)
91+
{
92+
List<object> args = new List<object>();
93+
args.Add(source);
94+
args.Add(destination);
95+
args.Add(sourceSide == ListSide.Left ? CoreArgs.LEFT : CoreArgs.RIGHT);
96+
args.Add(destinationSide == ListSide.Left ? CoreArgs.LEFT : CoreArgs.RIGHT);
97+
args.Add(timeout);
98+
99+
return new SerializedCommand(RedisCoreCommands.BLMOVE, args);
100+
}
101+
102+
public static SerializedCommand BRPopLPush(RedisKey source, RedisKey destination, double timeout)
103+
{
104+
List<object> args = new List<object>();
105+
args.Add(source);
106+
args.Add(destination);
56107
args.Add(timeout);
57108

58-
return new SerializedCommand(RedisCoreCommands.BZPOPMIN, args);
109+
return new SerializedCommand(RedisCoreCommands.BRPOPLPUSH, args);
59110
}
60111

61-
public static SerializedCommand BzPopMax(RedisKey[] keys, double timeout)
112+
private static SerializedCommand BlockingCommandWithKeysAndTimeout(String command, RedisKey[] keys, double timeout)
62113
{
63114
if (keys.Length == 0)
64115
{
@@ -69,7 +120,7 @@ public static SerializedCommand BzPopMax(RedisKey[] keys, double timeout)
69120
args.AddRange(keys.Cast<object>());
70121
args.Add(timeout);
71122

72-
return new SerializedCommand(RedisCoreCommands.BZPOPMAX, args);
123+
return new SerializedCommand(command, args);
73124
}
74125
}
75126
}

src/NRedisStack/CoreCommands/CoreCommands.cs

Lines changed: 210 additions & 15 deletions
Large diffs are not rendered by default.

src/NRedisStack/CoreCommands/DataTypes/RedisValueWithScore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ public RedisValueWithScore(RedisValue value, double score)
2828
/// <c>ZADD my-set 5.1 my-value</c>, the score is <c>5.1</c>.
2929
/// </summary>
3030
public double Score { get; }
31-
}
31+
}

src/NRedisStack/CoreCommands/Literals/CommandArgs.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ namespace NRedisStack.Core.Literals
33
internal static class CoreArgs
44
{
55
public const string COUNT = "COUNT";
6-
public const string lib_name = "LIB-NAME";
7-
public const string lib_ver = "LIB-VER";
6+
public const string LEFT = "LEFT";
87
public const string MAX = "MAX";
98
public const string MIN = "MIN";
9+
public const string RIGHT = "RIGHT";
10+
public const string lib_name = "LIB-NAME";
11+
public const string lib_ver = "LIB-VER";
1012
}
1113
}

src/NRedisStack/CoreCommands/Literals/Commands.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ namespace NRedisStack.Core.Literals
55
/// </summary>
66
internal static class RedisCoreCommands
77
{
8+
public const string BLMOVE = "BLMOVE";
9+
public const string BLMPOP = "BLMPOP";
10+
public const string BLPOP = "BLPOP";
11+
public const string BRPOP = "BRPOP";
12+
public const string BRPOPLPUSH = "BRPOPLPUSH";
813
public const string BZMPOP = "BZMPOP";
914
public const string BZPOPMAX = "BZPOPMAX";
1015
public const string BZPOPMIN = "BZPOPMIN";

src/NRedisStack/ResponseParser.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,5 +766,41 @@ public static Dictionary<string, RedisResult>[] ToDictionarys(this RedisResult r
766766

767767
return new Tuple<RedisKey, List<RedisValueWithScore>>(resultKey, valuesWithScores);
768768
}
769+
770+
public static Tuple<RedisKey, RedisValue>? ToListPopResult(this RedisResult result)
771+
{
772+
if (result.IsNull)
773+
{
774+
return null;
775+
}
776+
777+
var resultArray = (RedisResult[])result!;
778+
var resultKey = resultArray[0].ToRedisKey();
779+
var value = resultArray[1].ToRedisValue();
780+
781+
return new Tuple<RedisKey, RedisValue>(resultKey, value);
782+
}
783+
784+
public static Tuple<RedisKey, List<RedisValue>>? ToListPopResults(this RedisResult result)
785+
{
786+
if (result.IsNull)
787+
{
788+
return null;
789+
}
790+
791+
var resultArray = (RedisResult[])result!;
792+
var resultKey = resultArray[0].ToRedisKey();
793+
var resultSetItems = resultArray[1].ToArray();
794+
795+
List<RedisValue> values = new List<RedisValue>();
796+
797+
foreach (var resultSetItem in resultSetItems)
798+
{
799+
var value = (RedisValue)resultSetItem!;
800+
values.Add(value);
801+
}
802+
803+
return new Tuple<RedisKey, List<RedisValue>>(resultKey, values);
804+
}
769805
}
770806
}

0 commit comments

Comments
 (0)