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

Commit 7a05c4d

Browse files
committed
re-factor Set methods and add tests
1 parent 1481f01 commit 7a05c4d

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-11
lines changed

src/ServiceStack.Redis/RedisClient.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,20 @@ public void SetEntry(string key, string value, TimeSpan expireIn)
126126
{
127127
var bytesValue = value != null ? value.ToUtf8Bytes() : null;
128128

129-
base.Set(key, bytesValue, (int)expireIn.TotalSeconds, expireIn.Milliseconds);
129+
if (expireIn.Milliseconds > 0)
130+
base.Set(key, bytesValue, 0, (long)expireIn.TotalMilliseconds);
131+
else
132+
base.Set(key, bytesValue, (int)expireIn.TotalSeconds, 0);
130133
}
131134

132135
public void SetEntryIfExists(string key, string value, TimeSpan expireIn)
133136
{
134137
var bytesValue = value != null ? value.ToUtf8Bytes() : null;
135138

136-
base.Set(key, bytesValue, (int)expireIn.TotalSeconds, expireIn.Milliseconds, exists:true);
139+
if (expireIn.Milliseconds > 0)
140+
base.Set(key, bytesValue, 0, (long)expireIn.TotalMilliseconds, exists: true);
141+
else
142+
base.Set(key, bytesValue, (int)expireIn.TotalSeconds, 0, exists: true);
137143
}
138144

139145
public bool SetEntryIfNotExists(string key, string value)
@@ -148,7 +154,10 @@ public void SetEntryIfNotExists(string key, string value, TimeSpan expireIn)
148154
{
149155
var bytesValue = value != null ? value.ToUtf8Bytes() : null;
150156

151-
base.Set(key, bytesValue, (int)expireIn.TotalSeconds, expireIn.Milliseconds, exists: false);
157+
if (expireIn.Milliseconds > 0)
158+
base.Set(key, bytesValue, 0, (long)expireIn.TotalMilliseconds, exists: false);
159+
else
160+
base.Set(key, bytesValue, (int)expireIn.TotalSeconds, 0, exists: false);
152161
}
153162

154163
public void ChangeDb(long db)

src/ServiceStack.Redis/RedisNativeClient.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,18 +338,23 @@ public void Set(string key, byte[] value, int expirySeconds, long expiryMs = 0,
338338

339339
if (exists == null)
340340
{
341-
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value,
342-
Commands.Ex, expirySeconds.ToUtf8Bytes(),
343-
Commands.Px, expiryMs.ToUtf8Bytes());
341+
if (expirySeconds > 0)
342+
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, Commands.Ex, expirySeconds.ToUtf8Bytes());
343+
else if (expiryMs > 0)
344+
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, Commands.Px, expiryMs.ToUtf8Bytes());
345+
else
346+
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value);
344347
}
345348
else
346349
{
347350
var entryExists = exists.Value ? Commands.Xx : Commands.Nx;
348351

349-
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value,
350-
Commands.Ex, expirySeconds.ToUtf8Bytes(),
351-
Commands.Px, expiryMs.ToUtf8Bytes(),
352-
entryExists);
352+
if (expirySeconds > 0)
353+
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, Commands.Ex, expirySeconds.ToUtf8Bytes(), entryExists);
354+
else if (expiryMs > 0)
355+
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, Commands.Px, expiryMs.ToUtf8Bytes(), entryExists);
356+
else
357+
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, entryExists);
353358
}
354359
}
355360

tests/ServiceStack.Redis.Tests/RedisClientTests.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,52 @@ public void Can_change_db_at_runtime()
557557
}
558558
}
559559
}
560-
}
560+
561+
[Test]
562+
public void Can_Set_Expire_Seconds()
563+
{
564+
Redis.SetEntry("key", "val", expireIn: TimeSpan.FromSeconds(1));
565+
Assert.That(Redis.ContainsKey("key"), Is.True);
566+
Thread.Sleep(2000);
567+
Assert.That(Redis.ContainsKey("key"), Is.False);
568+
}
569+
570+
[Test]
571+
public void Can_Set_Expire_MilliSeconds()
572+
{
573+
Redis.SetEntry("key", "val", expireIn: TimeSpan.FromMilliseconds(1000));
574+
Assert.That(Redis.ContainsKey("key"), Is.True);
575+
Thread.Sleep(2000);
576+
Assert.That(Redis.ContainsKey("key"), Is.False);
577+
}
578+
579+
[Test]
580+
public void Can_Set_Expire_Seconds_if_exists()
581+
{
582+
Redis.SetEntryIfExists("key", "val", expireIn: TimeSpan.FromMilliseconds(1500));
583+
Assert.That(Redis.ContainsKey("key"), Is.False);
584+
585+
Redis.SetEntry("key", "val");
586+
Redis.SetEntryIfExists("key", "val", expireIn: TimeSpan.FromMilliseconds(1000));
587+
Assert.That(Redis.ContainsKey("key"), Is.True);
588+
589+
Thread.Sleep(2000);
590+
Assert.That(Redis.ContainsKey("key"), Is.False);
591+
}
592+
593+
[Test]
594+
public void Can_Set_Expire_Seconds_if_not_exists()
595+
{
596+
Redis.SetEntryIfNotExists("key", "val", expireIn: TimeSpan.FromMilliseconds(1000));
597+
Assert.That(Redis.ContainsKey("key"), Is.True);
598+
599+
Thread.Sleep(2000);
600+
Assert.That(Redis.ContainsKey("key"), Is.False);
601+
602+
Redis.Remove("key");
603+
Redis.SetEntryIfNotExists("key", "val", expireIn: TimeSpan.FromMilliseconds(1000));
604+
Assert.That(Redis.ContainsKey("key"), Is.False);
605+
}
606+
}
561607

562608
}

0 commit comments

Comments
 (0)