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

Commit 747cef8

Browse files
committed
Split set method into different API's with NX/XX returning bool response
1 parent c936928 commit 747cef8

File tree

4 files changed

+53
-36
lines changed

4 files changed

+53
-36
lines changed

src/ServiceStack.Redis/RedisClient.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,32 +162,38 @@ public void SetEntry(string key, string value, TimeSpan expireIn)
162162
}
163163
}
164164

165-
public void SetEntryIfExists(string key, string value, TimeSpan expireIn)
165+
public bool SetEntryIfExists(string key, string value)
166+
{
167+
var bytesValue = value != null ? value.ToUtf8Bytes() : null;
168+
169+
return base.Set(key, bytesValue, exists:true);
170+
}
171+
172+
public bool SetEntryIfExists(string key, string value, TimeSpan expireIn)
166173
{
167174
var bytesValue = value != null ? value.ToUtf8Bytes() : null;
168175

169176
if (expireIn.Milliseconds > 0)
170-
base.Set(key, bytesValue, 0, (long)expireIn.TotalMilliseconds, exists: true);
177+
return base.Set(key, bytesValue, exists:true, expiryMs:(long)expireIn.TotalMilliseconds);
171178
else
172-
base.Set(key, bytesValue, (int)expireIn.TotalSeconds, 0, exists: true);
179+
return base.Set(key, bytesValue, exists:true, expirySeconds:(int)expireIn.TotalSeconds);
173180
}
174181

175182
public bool SetEntryIfNotExists(string key, string value)
176183
{
177-
if (value == null)
178-
throw new ArgumentNullException("value");
184+
var bytesValue = value != null ? value.ToUtf8Bytes() : null;
179185

180-
return SetNX(key, value.ToUtf8Bytes()) == Success;
186+
return base.Set(key, bytesValue, exists: false);
181187
}
182188

183-
public void SetEntryIfNotExists(string key, string value, TimeSpan expireIn)
189+
public bool SetEntryIfNotExists(string key, string value, TimeSpan expireIn)
184190
{
185191
var bytesValue = value != null ? value.ToUtf8Bytes() : null;
186192

187193
if (expireIn.Milliseconds > 0)
188-
base.Set(key, bytesValue, 0, (long)expireIn.TotalMilliseconds, exists: false);
194+
return base.Set(key, bytesValue, exists: false, expiryMs: (long)expireIn.TotalMilliseconds);
189195
else
190-
base.Set(key, bytesValue, (int)expireIn.TotalSeconds, 0, exists: false);
196+
return base.Set(key, bytesValue, exists: false, expirySeconds: (int)expireIn.TotalSeconds);
191197
}
192198

193199
public void ChangeDb(long db)
@@ -386,6 +392,12 @@ public IRedisTransaction CreateTransaction()
386392
return new RedisTransaction(this);
387393
}
388394

395+
public void AssertNotInTransaction()
396+
{
397+
if (Transaction != null)
398+
throw new NotSupportedException("Only atomic redis-server operations are supported in a transaction");
399+
}
400+
389401
public IRedisPipeline CreatePipeline()
390402
{
391403
return new RedisAllPurposePipeline(this);

src/ServiceStack.Redis/RedisNativeClient.cs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ public void Set(string key, byte[] value)
394394
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value);
395395
}
396396

397-
public void Set(string key, byte[] value, int expirySeconds, long expiryMs = 0, bool? exists = null)
397+
public void Set(string key, byte[] value, int expirySeconds, long expiryMs = 0)
398398
{
399399
if (key == null)
400400
throw new ArgumentNullException("key");
@@ -403,26 +403,24 @@ public void Set(string key, byte[] value, int expirySeconds, long expiryMs = 0,
403403
if (value.Length > OneGb)
404404
throw new ArgumentException("value exceeds 1G", "value");
405405

406-
if (exists == null)
407-
{
408-
if (expirySeconds > 0)
409-
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, Commands.Ex, expirySeconds.ToUtf8Bytes());
410-
else if (expiryMs > 0)
411-
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, Commands.Px, expiryMs.ToUtf8Bytes());
412-
else
413-
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value);
414-
}
406+
if (expirySeconds > 0)
407+
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, Commands.Ex, expirySeconds.ToUtf8Bytes());
408+
else if (expiryMs > 0)
409+
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, Commands.Px, expiryMs.ToUtf8Bytes());
415410
else
416-
{
417-
var entryExists = exists.Value ? Commands.Xx : Commands.Nx;
418-
419-
if (expirySeconds > 0)
420-
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, Commands.Ex, expirySeconds.ToUtf8Bytes(), entryExists);
421-
else if (expiryMs > 0)
422-
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, Commands.Px, expiryMs.ToUtf8Bytes(), entryExists);
423-
else
424-
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, entryExists);
425-
}
411+
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value);
412+
}
413+
414+
public bool Set(string key, byte[] value, bool exists, int expirySeconds = 0, long expiryMs = 0)
415+
{
416+
var entryExists = exists ? Commands.Xx : Commands.Nx;
417+
418+
if (expirySeconds > 0)
419+
return SendExpectString(Commands.Set, key.ToUtf8Bytes(), value, Commands.Ex, expirySeconds.ToUtf8Bytes(), entryExists) == OK;
420+
if (expiryMs > 0)
421+
return SendExpectString(Commands.Set, key.ToUtf8Bytes(), value, Commands.Px, expiryMs.ToUtf8Bytes(), entryExists) == OK;
422+
423+
return SendExpectString(Commands.Set, key.ToUtf8Bytes(), value, entryExists) == OK;
426424
}
427425

428426
public void SetEx(string key, int expireInSeconds, byte[] value)

src/ServiceStack.Redis/RedisNativeClient_Utils.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ namespace ServiceStack.Redis
2727
{
2828
public partial class RedisNativeClient
2929
{
30+
private const string OK = "OK";
31+
private const string QUEUED = "QUEUED";
3032
private static Timer UsageTimer;
3133
private static int __requestsPerHour = 0;
3234
private const int Unknown = -1;
@@ -644,12 +646,12 @@ private string ExpectCode()
644646

645647
internal void ExpectOk()
646648
{
647-
ExpectWord("OK");
649+
ExpectWord(OK);
648650
}
649651

650652
internal void ExpectQueued()
651653
{
652-
ExpectWord("QUEUED");
654+
ExpectWord(QUEUED);
653655
}
654656

655657
public long ReadInt()
@@ -744,7 +746,7 @@ private byte[] ParseSingleLine(string r)
744746
throw CreateResponseError("Invalid length");
745747
}
746748

747-
if (c == ':')
749+
if (c == ':' || c == '+')
748750
{
749751
//match the return value
750752
return r.Substring(1).ToUtf8Bytes();

tests/ServiceStack.Redis.Tests/RedisClientTests.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public class RedisClientTests
1717
{
1818
const string Value = "Value";
1919

20-
2120
public override void OnBeforeEachTest()
2221
{
2322
base.OnBeforeEachTest();
@@ -607,11 +606,13 @@ public void Can_Set_Expire_MilliSeconds()
607606
[Test]
608607
public void Can_Set_Expire_Seconds_if_exists()
609608
{
610-
Redis.SetEntryIfExists("key", "val", expireIn: TimeSpan.FromMilliseconds(1500));
609+
Assert.That(Redis.SetEntryIfExists("key", "val", expireIn: TimeSpan.FromMilliseconds(1500)),
610+
Is.False);
611611
Assert.That(Redis.ContainsKey("key"), Is.False);
612612

613613
Redis.SetEntry("key", "val");
614-
Redis.SetEntryIfExists("key", "val", expireIn: TimeSpan.FromMilliseconds(1000));
614+
Assert.That(Redis.SetEntryIfExists("key", "val", expireIn: TimeSpan.FromMilliseconds(1000)),
615+
Is.True);
615616
Assert.That(Redis.ContainsKey("key"), Is.True);
616617

617618
Thread.Sleep(2000);
@@ -621,9 +622,13 @@ public void Can_Set_Expire_Seconds_if_exists()
621622
[Test]
622623
public void Can_Set_Expire_Seconds_if_not_exists()
623624
{
624-
Redis.SetEntryIfNotExists("key", "val", expireIn: TimeSpan.FromMilliseconds(1000));
625+
Assert.That(Redis.SetEntryIfNotExists("key", "val", expireIn: TimeSpan.FromMilliseconds(1000)),
626+
Is.True);
625627
Assert.That(Redis.ContainsKey("key"), Is.True);
626628

629+
Assert.That(Redis.SetEntryIfNotExists("key", "val", expireIn: TimeSpan.FromMilliseconds(1000)),
630+
Is.False);
631+
627632
Thread.Sleep(2000);
628633
Assert.That(Redis.ContainsKey("key"), Is.False);
629634

0 commit comments

Comments
 (0)