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

Commit a4e5ce8

Browse files
author
Sasan Yeganegi
committed
Make it support byte array
1 parent 2939228 commit a4e5ce8

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace ServiceStack.Redis
7+
{
8+
partial class RedisClient
9+
{
10+
public bool Set(byte[] key, byte[] value, TimeSpan expiresIn)
11+
{
12+
if (AssertServerVersionNumber() >= 2600)
13+
{
14+
Exec(r => r.Set(key, value, 0, expiryMs: (long)expiresIn.TotalMilliseconds));
15+
}
16+
else
17+
{
18+
Exec(r => r.SetEx(key, (int)expiresIn.TotalSeconds, value));
19+
}
20+
21+
return true;
22+
}
23+
24+
public bool Remove(byte[] key)
25+
{
26+
return Del(key) == Success;
27+
}
28+
29+
30+
public bool ExpireEntryIn(byte[] key, TimeSpan expireIn)
31+
{
32+
if (AssertServerVersionNumber() >= 2600)
33+
{
34+
if (expireIn.Milliseconds > 0)
35+
{
36+
return PExpire(key, (long)expireIn.TotalMilliseconds);
37+
}
38+
}
39+
40+
return Expire(key, (int)expireIn.TotalSeconds);
41+
}
42+
}
43+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace ServiceStack.Redis
7+
{
8+
partial class RedisNativeClient
9+
{
10+
public void Set(byte[] key, byte[] value, int expirySeconds, long expiryMs = 0)
11+
{
12+
if (key == null)
13+
throw new ArgumentNullException("key");
14+
15+
value = value ?? new byte[0];
16+
17+
if (value.Length > OneGb)
18+
throw new ArgumentException("value exceeds 1G", "value");
19+
20+
if (expirySeconds > 0)
21+
SendExpectSuccess(Commands.Set, key, value, Commands.Ex, expirySeconds.ToUtf8Bytes());
22+
else if (expiryMs > 0)
23+
SendExpectSuccess(Commands.Set, key, value, Commands.Px, expiryMs.ToUtf8Bytes());
24+
else
25+
SendExpectSuccess(Commands.Set, key, value);
26+
}
27+
28+
public void SetEx(byte[] key, int expireInSeconds, byte[] value)
29+
{
30+
if (key == null)
31+
throw new ArgumentNullException("key");
32+
value = value ?? new byte[0];
33+
34+
if (value.Length > OneGb)
35+
throw new ArgumentException("value exceeds 1G", "value");
36+
37+
SendExpectSuccess(Commands.SetEx, key, expireInSeconds.ToUtf8Bytes(), value);
38+
}
39+
40+
public byte[] Get(byte[] key)
41+
{
42+
if (key == null)
43+
throw new ArgumentNullException("key");
44+
45+
return SendExpectData(Commands.Get, key);
46+
}
47+
48+
public long HSet(byte[] hashId, byte[] key, byte[] value)
49+
{
50+
AssertHashIdAndKey(hashId, key);
51+
52+
return SendExpectLong(Commands.HSet, hashId, key, value);
53+
}
54+
55+
public byte[] HGet(byte[] hashId, byte[] key)
56+
{
57+
AssertHashIdAndKey(hashId, key);
58+
59+
return SendExpectData(Commands.HGet, hashId, key);
60+
}
61+
62+
public long Del(byte[] key)
63+
{
64+
if (key == null)
65+
throw new ArgumentNullException("key");
66+
67+
return SendExpectLong(Commands.Del, key);
68+
}
69+
70+
public long HDel(byte[] hashId, byte[] key)
71+
{
72+
AssertHashIdAndKey(hashId, key);
73+
74+
return SendExpectLong(Commands.HDel, hashId, key);
75+
}
76+
77+
78+
79+
public bool PExpire(byte[] key, long ttlMs)
80+
{
81+
if (key == null)
82+
throw new ArgumentNullException("key");
83+
84+
return SendExpectLong(Commands.PExpire, key, ttlMs.ToUtf8Bytes()) == Success;
85+
}
86+
87+
public bool Expire(byte[] key, int seconds)
88+
{
89+
if (key == null)
90+
throw new ArgumentNullException("key");
91+
92+
return SendExpectLong(Commands.Expire, key, seconds.ToUtf8Bytes()) == Success;
93+
}
94+
95+
96+
private static void AssertHashIdAndKey(byte[] hashId, byte[] key)
97+
{
98+
if (hashId == null)
99+
throw new ArgumentNullException("hashId");
100+
if (key == null)
101+
throw new ArgumentNullException("key");
102+
}
103+
}
104+
}

src/ServiceStack.Redis/ServiceStack.Redis.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@
148148
<Compile Include="BasicRedisClientManager.cs" />
149149
<Compile Include="BasicRedisClientManager.ICacheClient.cs" />
150150
<Compile Include="BasicRedisResolver.cs" />
151+
<Compile Include="RedisClient.KeyByteArray.cs" />
152+
<Compile Include="RedisNativeClient.KeyByteArray.cs" />
151153
<Compile Include="RedisResolver.cs" />
152154
<Compile Include="BufferPool.cs" />
153155
<Compile Include="Commands.cs" />

0 commit comments

Comments
 (0)