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

Commit d6ed687

Browse files
committed
Add support for HashSet commands in Redis Transactions
1 parent bab0524 commit d6ed687

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

src/ServiceStack.Redis/Generic/RedisTypedCommandQueue.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using ServiceStack.Text;
45

56
namespace ServiceStack.Redis.Generic
@@ -256,5 +257,48 @@ public void QueueCommand(Func<IRedisTypedClient<T>, List<T>> command, Action<Lis
256257
command(RedisClient);
257258
}
258259

260+
261+
public void QueueCommand(Func<IRedisTypedClient<T>, HashSet<string>> command)
262+
{
263+
QueueCommand(command, null, null);
264+
}
265+
266+
public void QueueCommand(Func<IRedisTypedClient<T>, HashSet<string>> command, Action<HashSet<string>> onSuccessCallback)
267+
{
268+
QueueCommand(command, onSuccessCallback, null);
269+
}
270+
271+
public void QueueCommand(Func<IRedisTypedClient<T>, HashSet<string>> command, Action<HashSet<string>> onSuccessCallback, Action<Exception> onErrorCallback)
272+
{
273+
BeginQueuedCommand(new QueuedRedisTypedCommand<T>
274+
{
275+
MultiStringReturnCommand = r => command(r).ToList(),
276+
OnSuccessMultiStringCallback = list => onSuccessCallback(list.ToHashSet()),
277+
OnErrorCallback = onErrorCallback
278+
});
279+
command(RedisClient);
280+
}
281+
282+
public void QueueCommand(Func<IRedisTypedClient<T>, HashSet<T>> command)
283+
{
284+
QueueCommand(command, null, null);
285+
}
286+
287+
public void QueueCommand(Func<IRedisTypedClient<T>, HashSet<T>> command, Action<HashSet<T>> onSuccessCallback)
288+
{
289+
QueueCommand(command, onSuccessCallback, null);
290+
}
291+
292+
public void QueueCommand(Func<IRedisTypedClient<T>, HashSet<T>> command, Action<HashSet<T>> onSuccessCallback, Action<Exception> onErrorCallback)
293+
{
294+
BeginQueuedCommand(new QueuedRedisTypedCommand<T>
295+
{
296+
MultiObjectReturnCommand = r => command(r).ToList(),
297+
OnSuccessMultiTypeCallback = x => onSuccessCallback(x.ConvertAll(y => JsonSerializer.DeserializeFromString<T>(y)).ToHashSet()),
298+
OnErrorCallback = onErrorCallback
299+
});
300+
command(RedisClient);
301+
}
302+
259303
}
260304
}

src/ServiceStack.Redis/Pipeline/RedisCommandQueue.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
using System;
1414
using System.Collections.Generic;
15+
using System.Linq;
1516
using ServiceStack.Redis.Pipeline;
1617

1718
namespace ServiceStack.Redis
@@ -225,6 +226,29 @@ public virtual void QueueCommand(Func<IRedisClient, List<string>> command, Actio
225226
command(RedisClient);
226227
}
227228

229+
230+
public void QueueCommand(Func<IRedisClient, HashSet<string>> command)
231+
{
232+
QueueCommand(command, null, null);
233+
}
234+
235+
public void QueueCommand(Func<IRedisClient, HashSet<string>> command, Action<HashSet<string>> onSuccessCallback)
236+
{
237+
QueueCommand(command, onSuccessCallback, null);
238+
}
239+
240+
public virtual void QueueCommand(Func<IRedisClient, HashSet<string>> command, Action<HashSet<string>> onSuccessCallback, Action<Exception> onErrorCallback)
241+
{
242+
BeginQueuedCommand(new QueuedRedisCommand
243+
{
244+
MultiStringReturnCommand = r => command(r).ToList(),
245+
OnSuccessMultiStringCallback = list => onSuccessCallback(list.ToHashSet()),
246+
OnErrorCallback = onErrorCallback
247+
});
248+
command(RedisClient);
249+
}
250+
251+
228252
public void QueueCommand(Func<IRedisClient, Dictionary<string, string>> command)
229253
{
230254
QueueCommand(command, null, null);

tests/ServiceStack.Redis.Tests/RedisTransactionTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,5 +364,22 @@ public void Can_call_Type_in_transaction()
364364
{"zset", "zset" },
365365
}));
366366
}
367+
368+
[Test]
369+
public void Can_call_HashSet_commands_in_transaction()
370+
{
371+
Redis.AddItemToSet("set", "ITEM 1");
372+
Redis.AddItemToSet("set", "ITEM 2");
373+
HashSet<string> result = null;
374+
375+
using (var trans = Redis.CreateTransaction())
376+
{
377+
trans.QueueCommand(r => r.GetAllItemsFromSet("set"), values => result = values);
378+
379+
trans.Commit();
380+
}
381+
382+
Assert.That(result, Is.EquivalentTo(new[] { "ITEM 1", "ITEM 2" }));
383+
}
367384
}
368385
}

0 commit comments

Comments
 (0)