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

Commit 1207ec9

Browse files
committed
Add support for custom commands
1 parent 6f06e28 commit 1207ec9

File tree

8 files changed

+120
-0
lines changed

8 files changed

+120
-0
lines changed

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/RedisClient.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ public string this[string key]
9696

9797
public override void OnConnected() {}
9898

99+
public RedisText Custom(params object[] cmdWithArgs)
100+
{
101+
var data = base.RawCommand(cmdWithArgs);
102+
var ret = data.ToRedisText();
103+
return ret;
104+
}
105+
99106
public DateTime ConvertToServerDate(DateTime expiresAt)
100107
{
101108
//placeholder if we ever try to compensate for differences in server-time

src/ServiceStack.Redis/RedisDataExtensions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,29 @@ public static RedisText ToRedisText(this RedisData data)
1616

1717
return to;
1818
}
19+
20+
public static string GetResult(this RedisText from)
21+
{
22+
return from.Text;
23+
}
24+
25+
public static T GetResult<T>(this RedisText from)
26+
{
27+
return from.Text.FromJson<T>();
28+
}
29+
30+
public static List<string> GetResults(this RedisText from)
31+
{
32+
return from.Children == null
33+
? new List<string>()
34+
: from.Children.ConvertAll(x => x.Text);
35+
}
36+
37+
public static List<T> GetResults<T>(this RedisText from)
38+
{
39+
return from.Children == null
40+
? new List<T>()
41+
: from.Children.ConvertAll(x => x.Text.FromJson<T>());
42+
}
1943
}
2044
}

src/ServiceStack.Redis/RedisNativeClient.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,44 @@ public string ServerVersion
218218
}
219219
}
220220

221+
public RedisData RawCommand(params object[] cmdWithArgs)
222+
{
223+
var byteArgs = new List<byte[]>();
224+
225+
foreach (var arg in cmdWithArgs)
226+
{
227+
if (arg == null)
228+
{
229+
byteArgs.Add(new byte[0]);
230+
continue;
231+
}
232+
233+
var bytes = arg as byte[];
234+
if (bytes != null)
235+
{
236+
byteArgs.Add(bytes);
237+
}
238+
else if (arg.GetType().IsUserType())
239+
{
240+
var json = arg.ToJson();
241+
byteArgs.Add(json.ToUtf8Bytes());
242+
}
243+
else
244+
{
245+
var str = arg.ToString();
246+
byteArgs.Add(str.ToUtf8Bytes());
247+
}
248+
}
249+
250+
var data = SendExpectComplexResponse(byteArgs.ToArray());
251+
return data;
252+
}
253+
254+
public RedisData RawCommand(params byte[][] cmdWithBinaryArgs)
255+
{
256+
return SendExpectComplexResponse(cmdWithBinaryArgs);
257+
}
258+
221259
public bool Ping()
222260
{
223261
return SendExpectCode(Commands.Ping) == "PONG";
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading;
4+
using NUnit.Framework;
5+
using ServiceStack.Text;
6+
7+
namespace ServiceStack.Redis
8+
{
9+
[TestFixture]
10+
public class CustomCommandTests
11+
{
12+
[Test]
13+
public void Can_send_custom_commands()
14+
{
15+
var redis = RedisClient.New();
16+
redis.FlushAll();
17+
18+
RedisText ret;
19+
20+
ret = redis.Custom("SET", "foo", 1);
21+
Assert.That(ret.Text, Is.EqualTo("OK"));
22+
ret = redis.Custom(Commands.Set, "bar", "b");
23+
24+
ret = redis.Custom("GET", "foo");
25+
Assert.That(ret.Text, Is.EqualTo("1"));
26+
ret = redis.Custom(Commands.Get, "bar");
27+
Assert.That(ret.Text, Is.EqualTo("b"));
28+
29+
ret = redis.Custom(Commands.Keys, "*");
30+
var keys = ret.GetResults();
31+
Assert.That(keys, Is.EquivalentTo(new[] { "foo", "bar" }));
32+
33+
ret = redis.Custom("MGET", "foo", "bar");
34+
var values = ret.GetResults();
35+
Assert.That(values, Is.EquivalentTo(new[] { "1", "b" }));
36+
37+
Enum.GetNames(typeof(DayOfWeek)).ToList()
38+
.ForEach(x => redis.Custom("RPUSH", "DaysOfWeek", x));
39+
40+
ret = redis.Custom("LRANGE", "DaysOfWeek", 1, -2);
41+
42+
var weekDays = ret.GetResults();
43+
Assert.That(weekDays, Is.EquivalentTo(
44+
new[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" }));
45+
46+
ret.PrintDump();
47+
}
48+
49+
}
50+
}

tests/ServiceStack.Redis.Tests/ServiceStack.Redis.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
<ItemGroup>
179179
<Compile Include="AdhocClientTests.cs" />
180180
<Compile Include="ConfigTests.cs" />
181+
<Compile Include="CustomCommandTests.cs" />
181182
<Compile Include="RedisManagerPoolTests.cs" />
182183
<Compile Include="DiagnosticTests.cs" />
183184
<Compile Include="Examples\ServiceStack_Redis_UseCase.cs" />

0 commit comments

Comments
 (0)