Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 16f7e65

Browse files
committed
Add Dictionary<string,string> to QueuedRedisOperation Pipeline
1 parent 2330336 commit 16f7e65

9 files changed

Lines changed: 70 additions & 12 deletions

File tree

lib/ServiceStack.Client.dll

0 Bytes
Binary file not shown.

lib/ServiceStack.Common.dll

0 Bytes
Binary file not shown.

lib/ServiceStack.Interfaces.dll

0 Bytes
Binary file not shown.

src/ServiceStack.Redis/Pipeline/QueuedRedisOperation.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ internal class QueuedRedisOperation
1717
public Func<byte[][]> MultiBytesReadCommand { get; set; }
1818
public Func<string> StringReadCommand { get; set; }
1919
public Func<List<string>> MultiStringReadCommand { get; set; }
20+
public Func<Dictionary<string, string>> DictionaryStringReadCommand { get; set; }
2021
public Func<double> DoubleReadCommand { get; set; }
2122

2223
public Action OnSuccessVoidCallback { get; set; }
@@ -27,6 +28,7 @@ internal class QueuedRedisOperation
2728
public Action<byte[][]> OnSuccessMultiBytesCallback { get; set; }
2829
public Action<string> OnSuccessStringCallback { get; set; }
2930
public Action<List<string>> OnSuccessMultiStringCallback { get; set; }
31+
public Action<Dictionary<string, string>> OnSuccessDictionaryStringCallback { get; set; }
3032
public Action<double> OnSuccessDoubleCallback { get; set; }
3133

3234
public Action<string> OnSuccessTypeCallback { get; set; }
@@ -147,12 +149,14 @@ public void ProcessResult()
147149
{
148150
OnSuccessMultiStringCallback(result != null ? result.ToStringList() : null);
149151
}
150-
151152
if (OnSuccessMultiTypeCallback != null)
152153
{
153154
OnSuccessMultiTypeCallback(result.ToStringList());
154155
}
155-
156+
if (OnSuccessDictionaryStringCallback != null)
157+
{
158+
OnSuccessDictionaryStringCallback(result.ToStringDictionary());
159+
}
156160
}
157161
else if (MultiStringReadCommand != null)
158162
{

src/ServiceStack.Redis/Pipeline/RedisCommand.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ internal class RedisCommand : QueuedRedisOperation
1919
public Func<IRedisClient, byte[][]> MultiBytesReturnCommand { get; set; }
2020
public Func<IRedisClient, string> StringReturnCommand { get; set; }
2121
public Func<IRedisClient, List<string>> MultiStringReturnCommand { get; set; }
22+
public Func<IRedisClient, Dictionary<string, string>> DictionaryStringReturnCommand { get; set; }
2223
public Func<IRedisClient, double> DoubleReturnCommand { get; set; }
2324

2425
public override void Execute(IRedisClient client)
@@ -63,7 +64,10 @@ public override void Execute(IRedisClient client)
6364
else if (MultiStringReturnCommand != null)
6465
{
6566
MultiStringReturnCommand(client);
66-
67+
}
68+
else if (DictionaryStringReturnCommand != null)
69+
{
70+
DictionaryStringReturnCommand(client);
6771
}
6872
}
6973
catch (Exception ex)

src/ServiceStack.Redis/Pipeline/RedisCommandQueue.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,26 @@ public virtual void QueueCommand(Func<IRedisClient, List<string>> command, Actio
224224
});
225225
command(RedisClient);
226226
}
227+
228+
public void QueueCommand(Func<IRedisClient, Dictionary<string, string>> command)
229+
{
230+
QueueCommand(command, null, null);
231+
}
232+
233+
public void QueueCommand(Func<IRedisClient, Dictionary<string, string>> command, Action<Dictionary<string, string>> onSuccessCallback)
234+
{
235+
QueueCommand(command, onSuccessCallback, null);
236+
}
237+
238+
public void QueueCommand(Func<IRedisClient, Dictionary<string, string>> command, Action<Dictionary<string, string>> onSuccessCallback, Action<Exception> onErrorCallback)
239+
{
240+
BeginQueuedCommand(new QueuedRedisCommand
241+
{
242+
DictionaryStringReturnCommand = command,
243+
OnSuccessDictionaryStringCallback = onSuccessCallback,
244+
OnErrorCallback = onErrorCallback
245+
});
246+
command(RedisClient);
247+
}
227248
}
228249
}

src/ServiceStack.Redis/RedisClient_Hash.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,7 @@ public List<string> GetHashValues(string hashId)
126126
public Dictionary<string, string> GetAllEntriesFromHash(string hashId)
127127
{
128128
var multiDataList = base.HGetAll(hashId);
129-
var map = new Dictionary<string, string>();
130-
131-
for (var i = 0; i < multiDataList.Length; i += 2)
132-
{
133-
var key = multiDataList[i].FromUtf8Bytes();
134-
map[key] = multiDataList[i + 1].FromUtf8Bytes();
135-
}
136-
137-
return map;
129+
return multiDataList.ToStringDictionary();
138130
}
139131

140132
public List<string> GetValuesFromHash(string hashId, params string[] keys)

src/ServiceStack.Redis/RedisExtensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,22 @@ public static List<string> ToStringList(this byte[][] multiDataList)
143143
return results;
144144
}
145145

146+
public static Dictionary<string, string> ToStringDictionary(this byte[][] multiDataList)
147+
{
148+
if (multiDataList == null)
149+
return new Dictionary<string, string>();
150+
151+
var map = new Dictionary<string, string>();
152+
153+
for (var i = 0; i < multiDataList.Length; i += 2)
154+
{
155+
var key = multiDataList[i].FromUtf8Bytes();
156+
map[key] = multiDataList[i + 1].FromUtf8Bytes();
157+
}
158+
159+
return map;
160+
}
161+
146162
private static readonly NumberFormatInfo DoubleFormatProvider = new NumberFormatInfo
147163
{
148164
PositiveInfinitySymbol = "+inf",

tests/ServiceStack.Redis.Tests/RedisTransactionTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using NUnit.Framework;
4+
using ServiceStack.Text;
45

56
namespace ServiceStack.Redis.Tests
67
{
@@ -12,6 +13,7 @@ public class RedisTransactionTests
1213
private const string ListKey = "rdtmultitest-list";
1314
private const string SetKey = "rdtmultitest-set";
1415
private const string SortedSetKey = "rdtmultitest-sortedset";
16+
private const string HashKey = "rdthashtest";
1517

1618
public override void TearDown()
1719
{
@@ -311,5 +313,24 @@ public void Does_not_set_Expiry_on_existing_key_in_transaction()
311313
Assert.That(Redis.Get<string>(key), Is.EqualTo("Foo"));
312314
Assert.That(Redis.GetTimeToLive(key), Is.EqualTo(TimeSpan.MaxValue));
313315
}
316+
317+
[Test]
318+
public void Can_call_GetAllEntriesFromHash_in_transaction()
319+
{
320+
var stringMap = new Dictionary<string, string> {
321+
{"one","a"}, {"two","b"}, {"three","c"}, {"four","d"}
322+
};
323+
stringMap.Each(x => Redis.SetEntryInHash(HashKey, x.Key, x.Value));
324+
325+
Dictionary<string, string> results = null;
326+
using (var trans = Redis.CreateTransaction())
327+
{
328+
trans.QueueCommand(r => r.GetAllEntriesFromHash(HashKey), x => results = x);
329+
330+
trans.Commit();
331+
}
332+
333+
Assert.That(results, Is.EquivalentTo(stringMap));
334+
}
314335
}
315336
}

0 commit comments

Comments
 (0)