Skip to content

Commit 28b34e0

Browse files
authored
specifying keys in read/write commands (#463)
1 parent 27edd7b commit 28b34e0

File tree

4 files changed

+54
-32
lines changed

4 files changed

+54
-32
lines changed

src/Redis.OM/RedisCommands.cs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using Redis.OM.Contracts;
88
using Redis.OM.Modeling;
9+
using StackExchange.Redis;
910

1011
namespace Redis.OM
1112
{
@@ -84,7 +85,7 @@ public static async Task<string> SetAsync(this IRedisConnection connection, obje
8485
/// <returns>How many new fields were created.</returns>
8586
public static async Task<int> HSetAsync(this IRedisConnection connection, string key, params KeyValuePair<string, object>[] fieldValues)
8687
{
87-
var args = new List<object> { key };
88+
var args = new List<object> { new RedisKey(key) };
8889
foreach (var kvp in fieldValues)
8990
{
9091
args.Add(kvp.Key);
@@ -104,7 +105,7 @@ public static async Task<int> HSetAsync(this IRedisConnection connection, string
104105
/// <returns>How many new fields were created.</returns>
105106
public static async Task<int> HSetAsync(this IRedisConnection connection, string key, TimeSpan timeSpan, params KeyValuePair<string, object>[] fieldValues)
106107
{
107-
var args = new List<object> { key };
108+
var args = new List<object> { new RedisKey(key) };
108109
foreach (var kvp in fieldValues)
109110
{
110111
args.Add(kvp.Key);
@@ -124,7 +125,7 @@ public static async Task<int> HSetAsync(this IRedisConnection connection, string
124125
/// <returns>whether the operation succeeded.</returns>
125126
public static async Task<bool> JsonSetAsync(this IRedisConnection connection, string key, string path, string json)
126127
{
127-
var result = await connection.ExecuteAsync("JSON.SET", key, path, json);
128+
var result = await connection.ExecuteAsync("JSON.SET", new RedisKey(key), path, json);
128129
return result == "OK";
129130
}
130131

@@ -139,7 +140,7 @@ public static async Task<bool> JsonSetAsync(this IRedisConnection connection, st
139140
public static async Task<bool> JsonSetAsync(this IRedisConnection connection, string key, string path, object obj)
140141
{
141142
var json = JsonSerializer.Serialize(obj, RedisSerializationSettings.JsonSerializerOptions);
142-
var result = await connection.ExecuteAsync("JSON.SET", key, path, json);
143+
var result = await connection.ExecuteAsync("JSON.SET", new RedisKey(key), path, json);
143144
return result == "OK";
144145
}
145146

@@ -154,7 +155,7 @@ public static async Task<bool> JsonSetAsync(this IRedisConnection connection, st
154155
/// <returns>whether the operation succeeded.</returns>
155156
public static async Task<bool> JsonSetAsync(this IRedisConnection connection, string key, string path, string json, TimeSpan timeSpan)
156157
{
157-
var args = new[] { key, path, json };
158+
var args = new object[] { new RedisKey(key), path, json };
158159
return (await connection.SendCommandWithExpiryAsync("JSON.SET", args, key, timeSpan)).First() == "OK";
159160
}
160161

@@ -227,7 +228,7 @@ public static async Task<bool> JsonSetAsync(this IRedisConnection connection, st
227228
public static int HSet(this IRedisConnection connection, string key, TimeSpan timeSpan, params KeyValuePair<string, object>[] fieldValues)
228229
{
229230
var args = new List<object>();
230-
args.Add(key);
231+
args.Add(new RedisKey(key));
231232
foreach (var kvp in fieldValues)
232233
{
233234
args.Add(kvp.Key);
@@ -246,7 +247,7 @@ public static int HSet(this IRedisConnection connection, string key, TimeSpan ti
246247
/// <returns>How many new fields were created.</returns>
247248
public static int HSet(this IRedisConnection connection, string key, params KeyValuePair<string, object>[] fieldValues)
248249
{
249-
var args = new List<object> { key };
250+
var args = new List<object> { new RedisKey(key) };
250251
foreach (var kvp in fieldValues)
251252
{
252253
args.Add(kvp.Key);
@@ -266,7 +267,7 @@ public static int HSet(this IRedisConnection connection, string key, params KeyV
266267
/// <returns>whether the operation succeeded.</returns>
267268
public static bool JsonSet(this IRedisConnection connection, string key, string path, string json)
268269
{
269-
var result = connection.Execute("JSON.SET", key, path, json);
270+
var result = connection.Execute("JSON.SET", new RedisKey(key), path, json);
270271
return result == "OK";
271272
}
272273

@@ -281,7 +282,7 @@ public static bool JsonSet(this IRedisConnection connection, string key, string
281282
public static bool JsonSet(this IRedisConnection connection, string key, string path, object obj)
282283
{
283284
var json = JsonSerializer.Serialize(obj, RedisSerializationSettings.JsonSerializerOptions);
284-
var result = connection.Execute("JSON.SET", key, path, json);
285+
var result = connection.Execute("JSON.SET", new RedisKey(key), path, json);
285286
return result == "OK";
286287
}
287288

@@ -296,7 +297,7 @@ public static bool JsonSet(this IRedisConnection connection, string key, string
296297
/// <returns>whether the operation succeeded.</returns>
297298
public static bool JsonSet(this IRedisConnection connection, string key, string path, string json, TimeSpan timeSpan)
298299
{
299-
var arr = new[] { key, path, json };
300+
var arr = new object[] { new RedisKey(key), path, json };
300301
return connection.SendCommandWithExpiry("JSON.SET", arr, key, timeSpan).First() == "OK";
301302
}
302303

@@ -570,7 +571,7 @@ public static string Set(this IRedisConnection connection, object obj, TimeSpan
570571
/// <returns>the object pulled out of redis.</returns>
571572
public static T? JsonGet<T>(this IRedisConnection connection, string key, params string[] paths)
572573
{
573-
var args = new List<string> { key };
574+
var args = new List<object> { new RedisKey(key) };
574575
args.AddRange(paths);
575576
var res = (string)connection.Execute("JSON.GET", args.ToArray());
576577
return !string.IsNullOrEmpty(res) ? JsonSerializer.Deserialize<T>(res, RedisSerializationSettings.JsonSerializerOptions) : default;
@@ -586,7 +587,7 @@ public static string Set(this IRedisConnection connection, object obj, TimeSpan
586587
/// <returns>the object pulled out of redis.</returns>
587588
public static async Task<T?> JsonGetAsync<T>(this IRedisConnection connection, string key, params string[] paths)
588589
{
589-
var args = new List<string> { key };
590+
var args = new List<object> { new RedisKey(key) };
590591
args.AddRange(paths);
591592
var res = (string)await connection.ExecuteAsync("JSON.GET", args.ToArray());
592593
return !string.IsNullOrEmpty(res) ? JsonSerializer.Deserialize<T>(res, RedisSerializationSettings.JsonSerializerOptions) : default;
@@ -601,7 +602,7 @@ public static string Set(this IRedisConnection connection, object obj, TimeSpan
601602
public static IDictionary<string, RedisReply> HGetAll(this IRedisConnection connection, string keyName)
602603
{
603604
var ret = new Dictionary<string, RedisReply>();
604-
var res = connection.Execute("HGETALL", keyName).ToArray();
605+
var res = connection.Execute("HGETALL", new RedisKey(keyName)).ToArray();
605606
for (var i = 0; i < res.Length; i += 2)
606607
{
607608
ret.Add(res[i], res[i + 1]);
@@ -619,7 +620,7 @@ public static IDictionary<string, RedisReply> HGetAll(this IRedisConnection conn
619620
public static async Task<IDictionary<string, RedisReply>> HGetAllAsync(this IRedisConnection connection, string keyName)
620621
{
621622
var ret = new Dictionary<string, RedisReply>();
622-
var res = (await connection.ExecuteAsync("HGETALL", keyName)).ToArray();
623+
var res = (await connection.ExecuteAsync("HGETALL", new RedisKey(keyName))).ToArray();
623624
for (var i = 0; i < res.Length; i += 2)
624625
{
625626
ret.Add(res[i], res[i + 1]);
@@ -664,7 +665,7 @@ public static async Task<IDictionary<string, RedisReply>> HGetAllAsync(this IRed
664665
sha,
665666
keys.Count().ToString(),
666667
};
667-
args.AddRange(keys);
668+
args.AddRange(keys.Select(x => new RedisKey(x)).Cast<object>());
668669
args.AddRange(argv);
669670
try
670671
{
@@ -756,15 +757,15 @@ public static async Task<IDictionary<string, RedisReply>> HGetAllAsync(this IRed
756757
/// <param name="connection">the connection.</param>
757758
/// <param name="key">the key to unlink.</param>
758759
/// <returns>the status.</returns>
759-
public static async Task<long> UnlinkAsync(this IRedisConnection connection, string key) => await connection.ExecuteAsync("UNLINK", key);
760+
public static async Task<long> UnlinkAsync(this IRedisConnection connection, string key) => await connection.ExecuteAsync("UNLINK", new RedisKey(key));
760761

761762
/// <summary>
762763
/// Unlinks array of keys.
763764
/// </summary>
764765
/// <param name="connection">the connection.</param>
765766
/// <param name="keys">the keys to unlink.</param>
766767
/// <returns>the status.</returns>
767-
public static async Task<long> UnlinkAsync(this IRedisConnection connection, string[] keys) => await connection.ExecuteAsync("UNLINK", keys);
768+
public static async Task<long> UnlinkAsync(this IRedisConnection connection, string[] keys) => await connection.ExecuteAsync("UNLINK", keys.Select(x => new RedisKey(x)).Cast<object>().ToArray());
768769

769770
/// <summary>
770771
/// Unlinks the key and then adds an updated value of it.
@@ -835,7 +836,7 @@ private static RedisReply[] SendCommandWithExpiry(
835836
TimeSpan ts)
836837
{
837838
var commandTuple = Tuple.Create(command, args);
838-
var expireTuple = Tuple.Create("PEXPIRE", new object[] { keyToExpire, ((long)ts.TotalMilliseconds).ToString(CultureInfo.InvariantCulture) });
839+
var expireTuple = Tuple.Create("PEXPIRE", new object[] { new RedisKey(keyToExpire), ((long)ts.TotalMilliseconds).ToString(CultureInfo.InvariantCulture) });
839840
return connection.ExecuteInTransaction(new[] { commandTuple, expireTuple });
840841
}
841842

@@ -847,7 +848,7 @@ private static Task<RedisReply[]> SendCommandWithExpiryAsync(
847848
TimeSpan ts)
848849
{
849850
var commandTuple = Tuple.Create(command, args);
850-
var expireTuple = Tuple.Create("PEXPIRE", new object[] { keyToExpire, ((long)ts.TotalMilliseconds).ToString(CultureInfo.InvariantCulture) });
851+
var expireTuple = Tuple.Create("PEXPIRE", new object[] { new RedisKey(keyToExpire), ((long)ts.TotalMilliseconds).ToString(CultureInfo.InvariantCulture) });
851852
return connection.ExecuteInTransactionAsync(new[] { commandTuple, expireTuple });
852853
}
853854
}

test/Redis.OM.Unit.Tests/CoreTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,5 +493,25 @@ public void TestUnlink()
493493
connection.Execute("SET", key1, "bar");
494494
Assert.Equal(1,connection.Unlink(new []{key1, key2}));
495495
}
496+
497+
[SkipIfMissingEnvVar("CLUSTER_HOST_PORT")]
498+
public async Task TestClusterOperations()
499+
{
500+
var hostInfo = System.Environment.GetEnvironmentVariable("CLUSTER_HOST_PORT");
501+
Console.WriteLine($"Current host info: {hostInfo}");
502+
var connectionString = $"redis://{hostInfo}";
503+
var provider = new RedisConnectionProvider(connectionString);
504+
var connection = provider.Connection;
505+
506+
var tasks = new List<Task>();
507+
for(var i = 0; i < 10_000; i++)
508+
{
509+
var person = new Person();
510+
tasks.Add(connection.SetAsync(person));
511+
}
512+
513+
await Task.WhenAll(tasks);
514+
}
515+
496516
}
497517
}

test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ public async Task TestUpdateJson()
992992
var steve = await collection.FirstAsync(x => x.Name == "Steve");
993993
steve.Age = 33;
994994
await collection.UpdateAsync(steve);
995-
await _substitute.Received().ExecuteAsync("EVALSHA", Arg.Any<string>(), "1", "Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET", "$.Age", "33");
995+
await _substitute.Received().ExecuteAsync("EVALSHA", Arg.Any<string>(), "1", new RedisKey("Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N"), "SET", "$.Age", "33");
996996
Scripts.ShaCollection.Clear();
997997
}
998998

@@ -1010,8 +1010,8 @@ public async Task TestUpdateJsonUnloadedScriptAsync()
10101010
var steve = await collection.FirstAsync(x => x.Name == "Steve");
10111011
steve.Age = 33;
10121012
await collection.UpdateAsync(steve);
1013-
await _substitute.Received().ExecuteAsync("EVALSHA", Arg.Any<string>(), "1", "Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET", "$.Age", "33");
1014-
await _substitute.Received().ExecuteAsync("EVAL", Scripts.JsonDiffResolution, "1", "Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET", "$.Age", "33");
1013+
await _substitute.Received().ExecuteAsync("EVALSHA", Arg.Any<string>(), "1", new RedisKey("Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N"), "SET", "$.Age", "33");
1014+
await _substitute.Received().ExecuteAsync("EVAL", Scripts.JsonDiffResolution, "1", new RedisKey("Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N"), "SET", "$.Age", "33");
10151015
Scripts.ShaCollection.Clear();
10161016
}
10171017

@@ -1028,8 +1028,8 @@ public void TestUpdateJsonUnloadedScript()
10281028
var steve = collection.First(x => x.Name == "Steve");
10291029
steve.Age = 33;
10301030
collection.Update(steve);
1031-
_substitute.Received().Execute("EVALSHA", Arg.Any<string>(), "1", "Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET", "$.Age", "33");
1032-
_substitute.Received().Execute("EVAL", Scripts.JsonDiffResolution, "1", "Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET", "$.Age", "33");
1031+
_substitute.Received().Execute("EVALSHA", Arg.Any<string>(), "1", new RedisKey("Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N"), "SET", "$.Age", "33");
1032+
_substitute.Received().Execute("EVAL", Scripts.JsonDiffResolution, "1", new RedisKey("Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N"), "SET", "$.Age", "33");
10331033
Scripts.ShaCollection.Clear();
10341034
}
10351035

@@ -1043,7 +1043,7 @@ public async Task TestUpdateJsonName()
10431043
var steve = await collection.FirstAsync(x => x.Name == "Steve");
10441044
steve.Name = "Bob";
10451045
await collection.UpdateAsync(steve);
1046-
await _substitute.Received().ExecuteAsync("EVALSHA", Arg.Any<string>(), "1", "Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET", "$.Name", "\"Bob\"");
1046+
await _substitute.Received().ExecuteAsync("EVALSHA", Arg.Any<string>(), "1", new RedisKey("Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N"), "SET", "$.Name", "\"Bob\"");
10471047
Scripts.ShaCollection.Clear();
10481048
}
10491049

@@ -1058,12 +1058,12 @@ public async Task TestUpdateJsonNestedObject()
10581058
steve.Address = new Address { State = "Florida" };
10591059
await collection.UpdateAsync(steve);
10601060
var expected = $"{{{Environment.NewLine} \"State\": \"Florida\"{Environment.NewLine}}}";
1061-
await _substitute.Received().ExecuteAsync("EVALSHA", Arg.Any<string>(), "1", "Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET", "$.Address", expected);
1061+
await _substitute.Received().ExecuteAsync("EVALSHA", Arg.Any<string>(), "1", new RedisKey("Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N"), "SET", "$.Address", expected);
10621062

10631063
steve.Address.City = "Satellite Beach";
10641064
await collection.UpdateAsync(steve);
10651065
expected = "\"Satellite Beach\"";
1066-
await _substitute.Received().ExecuteAsync("EVALSHA", Arg.Any<string>(), "1", "Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET", "$.Address.City", expected);
1066+
await _substitute.Received().ExecuteAsync("EVALSHA", Arg.Any<string>(), "1", new RedisKey("Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N"), "SET", "$.Address.City", expected);
10671067

10681068
Scripts.ShaCollection.Clear();
10691069
}
@@ -1079,7 +1079,7 @@ public async Task TestUpdateJsonWithDouble()
10791079
steve.Age = 33;
10801080
steve.Height = 71.5;
10811081
await collection.UpdateAsync(steve);
1082-
await _substitute.Received().ExecuteAsync("EVALSHA", Arg.Any<string>(), "1", "Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET", "$.Age", "33", "SET", "$.Height", "71.5");
1082+
await _substitute.Received().ExecuteAsync("EVALSHA", Arg.Any<string>(), "1", new RedisKey("Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N"), "SET", "$.Age", "33", "SET", "$.Height", "71.5");
10831083
Scripts.ShaCollection.Clear();
10841084
}
10851085

@@ -1103,7 +1103,7 @@ public async Task TestDeleteAsync()
11031103
Assert.True(collection.StateManager.Data.ContainsKey(key));
11041104
Assert.True(collection.StateManager.Snapshot.ContainsKey(key));
11051105
await collection.DeleteAsync(steve);
1106-
await _substitute.Received().ExecuteAsync("UNLINK", key);
1106+
await _substitute.Received().ExecuteAsync("UNLINK", new RedisKey(key));
11071107
Assert.False(collection.StateManager.Data.ContainsKey(key));
11081108
Assert.False(collection.StateManager.Snapshot.ContainsKey(key));
11091109
}
@@ -1119,7 +1119,7 @@ public void TestDelete()
11191119
Assert.True(collection.StateManager.Data.ContainsKey(key));
11201120
Assert.True(collection.StateManager.Snapshot.ContainsKey(key));
11211121
collection.Delete(steve);
1122-
_substitute.Received().Execute("UNLINK", key);
1122+
_substitute.Received().Execute("UNLINK", new RedisKey(key));
11231123
Assert.False(collection.StateManager.Data.ContainsKey(key));
11241124
Assert.False(collection.StateManager.Snapshot.ContainsKey(key));
11251125
}

test/Redis.OM.Unit.Tests/RediSearchTests/VectorTests/VectorTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Redis.OM.Modeling;
99
using Redis.OM.Modeling.Vectors;
1010
using Redis.OM.Searching;
11+
using StackExchange.Redis;
1112
using Xunit;
1213

1314
namespace Redis.OM.Unit.Tests;
@@ -189,9 +190,9 @@ public void InsertVectors()
189190
_substitute.Execute("JSON.SET", Arg.Any<object[]>()).Returns(new RedisReply("OK"));
190191
_substitute.Set(hashObj);
191192
_substitute.Set(jsonObj);
192-
_substitute.Received().Execute("HSET", "Redis.OM.Unit.Tests.ObjectWithVectorHash:foo", "Id", "foo", "Num", "0", "SimpleHnswVector",
193+
_substitute.Received().Execute("HSET", new RedisKey("Redis.OM.Unit.Tests.ObjectWithVectorHash:foo"), "Id", "foo", "Num", "0", "SimpleHnswVector",
193194
Arg.Is<byte[]>(x=>x.SequenceEqual(simpleHnswBytes)), "SimpleVectorizedVector.Vector", Arg.Is<byte[]>(x=>x.SequenceEqual(flatVectorizedBytes)), "SimpleVectorizedVector.Value", "\"foobar\"");
194-
_substitute.Received().Execute("JSON.SET", "Redis.OM.Unit.Tests.ObjectWithVector:foo", ".", json);
195+
_substitute.Received().Execute("JSON.SET", new RedisKey("Redis.OM.Unit.Tests.ObjectWithVector:foo"), ".", json);
195196
var deseralized = JsonSerializer.Deserialize<ObjectWithVector>(json);
196197
Assert.Equal("foobar", deseralized.SimpleVectorizedVector.Value);
197198
}

0 commit comments

Comments
 (0)