Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more blocking commands #253

Merged
merged 9 commits into from
Feb 22, 2024
Merged
61 changes: 56 additions & 5 deletions src/NRedisStack/CoreCommands/CoreCommandBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static SerializedCommand ClientSetInfo(SetInfoAttr attr, string value)
return new SerializedCommand(RedisCoreCommands.CLIENT, RedisCoreCommands.SETINFO, attrValue, value);
}

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

public static SerializedCommand BzPopMin(RedisKey[] keys, double timeout)
public static SerializedCommand BZPopMin(RedisKey[] keys, double timeout)
{
return BlockingCommandWithKeysAndTimeout(RedisCoreCommands.BZPOPMIN, keys, timeout);
}

public static SerializedCommand BZPopMax(RedisKey[] keys, double timeout)
{
return BlockingCommandWithKeysAndTimeout(RedisCoreCommands.BZPOPMAX, keys, timeout);
}

public static SerializedCommand BLMPop(double timeout, RedisKey[] keys, ListSide listSide, long? count)
{
if (keys.Length == 0)
{
throw new ArgumentException("At least one key must be provided.");
}

List<object> args = new List<object>();

args.Add(timeout);
args.Add(keys.Length);
args.AddRange(keys.Cast<object>());
args.Add(listSide == ListSide.Left ? CoreArgs.LEFT : CoreArgs.RIGHT);

if (count != null)
{
args.Add(CoreArgs.COUNT);
args.Add(count);
}

return new SerializedCommand(RedisCoreCommands.BLMPOP, args);
}

public static SerializedCommand BLPop(RedisKey[] keys, double timeout)
{
return BlockingCommandWithKeysAndTimeout(RedisCoreCommands.BLPOP, keys, timeout);
}

public static SerializedCommand BRPop(RedisKey[] keys, double timeout)
{
return BlockingCommandWithKeysAndTimeout(RedisCoreCommands.BRPOP, keys, timeout);
}

public static SerializedCommand BLMove(RedisKey source, RedisKey destination, ListSide sourceSide, ListSide destinationSide, double timeout)
{
List<object> args = new List<object>();
args.Add(source);
args.Add(destination);
args.Add(sourceSide == ListSide.Left ? CoreArgs.LEFT : CoreArgs.RIGHT);
args.Add(destinationSide == ListSide.Left ? CoreArgs.LEFT : CoreArgs.RIGHT);
args.Add(timeout);

return new SerializedCommand(RedisCoreCommands.BLMOVE, args);
}

public static SerializedCommand BRPopLPush(RedisKey source, RedisKey destination, double timeout)
{
List<object> args = new List<object>();
args.Add(source);
args.Add(destination);
args.Add(timeout);

return new SerializedCommand(RedisCoreCommands.BZPOPMIN, args);
return new SerializedCommand(RedisCoreCommands.BRPOPLPUSH, args);
}

public static SerializedCommand BzPopMax(RedisKey[] keys, double timeout)
private static SerializedCommand BlockingCommandWithKeysAndTimeout(String command, RedisKey[] keys, double timeout)
{
if (keys.Length == 0)
{
Expand All @@ -69,7 +120,7 @@ public static SerializedCommand BzPopMax(RedisKey[] keys, double timeout)
args.AddRange(keys.Cast<object>());
args.Add(timeout);

return new SerializedCommand(RedisCoreCommands.BZPOPMAX, args);
return new SerializedCommand(command, args);
}
}
}
225 changes: 210 additions & 15 deletions src/NRedisStack/CoreCommands/CoreCommands.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ public RedisValueWithScore(RedisValue value, double score)
/// <c>ZADD my-set 5.1 my-value</c>, the score is <c>5.1</c>.
/// </summary>
public double Score { get; }
}
}
6 changes: 4 additions & 2 deletions src/NRedisStack/CoreCommands/Literals/CommandArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ namespace NRedisStack.Core.Literals
internal static class CoreArgs
{
public const string COUNT = "COUNT";
public const string lib_name = "LIB-NAME";
public const string lib_ver = "LIB-VER";
public const string LEFT = "LEFT";
public const string MAX = "MAX";
public const string MIN = "MIN";
public const string RIGHT = "RIGHT";
public const string lib_name = "LIB-NAME";
public const string lib_ver = "LIB-VER";
}
}
5 changes: 5 additions & 0 deletions src/NRedisStack/CoreCommands/Literals/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ namespace NRedisStack.Core.Literals
/// </summary>
internal static class RedisCoreCommands
{
public const string BLMOVE = "BLMOVE";
public const string BLMPOP = "BLMPOP";
public const string BLPOP = "BLPOP";
public const string BRPOP = "BRPOP";
public const string BRPOPLPUSH = "BRPOPLPUSH";
public const string BZMPOP = "BZMPOP";
public const string BZPOPMAX = "BZPOPMAX";
public const string BZPOPMIN = "BZPOPMIN";
Expand Down
36 changes: 36 additions & 0 deletions src/NRedisStack/ResponseParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -766,5 +766,41 @@ public static Dictionary<string, RedisResult>[] ToDictionarys(this RedisResult r

return new Tuple<RedisKey, List<RedisValueWithScore>>(resultKey, valuesWithScores);
}

public static Tuple<RedisKey, RedisValue>? ToListPopResult(this RedisResult result)
{
if (result.IsNull)
{
return null;
}

var resultArray = (RedisResult[])result!;
var resultKey = resultArray[0].ToRedisKey();
var value = resultArray[1].ToRedisValue();

return new Tuple<RedisKey, RedisValue>(resultKey, value);
}

public static Tuple<RedisKey, List<RedisValue>>? ToListPopResults(this RedisResult result)
{
if (result.IsNull)
{
return null;
}

var resultArray = (RedisResult[])result!;
var resultKey = resultArray[0].ToRedisKey();
var resultSetItems = resultArray[1].ToArray();

List<RedisValue> values = new List<RedisValue>();

foreach (var resultSetItem in resultSetItems)
{
var value = (RedisValue)resultSetItem!;
values.Add(value);
}

return new Tuple<RedisKey, List<RedisValue>>(resultKey, values);
}
}
}
Loading
Loading