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

Commit 021857c

Browse files
committed
Fix use of Add with expiry in transaction
1 parent 3ad60f7 commit 021857c

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

src/ServiceStack.Redis/RedisClient.ICacheClient.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ public bool Add<T>(string key, T value, DateTime expiresAt)
116116
});
117117
}
118118

119+
public bool Add<T>(string key, T value, TimeSpan expiresIn)
120+
{
121+
return Exec(r => {
122+
r.SetEntryIfNotExists(key, value.ToJson(), expiresIn);
123+
return true;
124+
});
125+
}
126+
119127
public bool Set<T>(string key, T value, TimeSpan expiresIn)
120128
{
121129
var bytesValue = value as byte[];
@@ -159,19 +167,6 @@ public bool Replace<T>(string key, T value, DateTime expiresAt)
159167
});
160168
}
161169

162-
public bool Add<T>(string key, T value, TimeSpan expiresIn)
163-
{
164-
return Exec(r =>
165-
{
166-
if (r.Add(key, value))
167-
{
168-
r.ExpireEntryIn(key, expiresIn);
169-
return true;
170-
}
171-
return false;
172-
});
173-
}
174-
175170
public bool Replace<T>(string key, T value, TimeSpan expiresIn)
176171
{
177172
return Exec(r =>

tests/ServiceStack.Redis.Tests/RedisTransactionTests.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ public void Transaction_can_be_replayed()
244244
Assert.That(Redis.GetValue(KeySquared), Is.EqualTo("1"));
245245
}
246246
}
247-
[Test]
247+
248+
[Test]
248249
public void Transaction_can_issue_watch()
249250
{
250251
Redis.Del(Key);
@@ -265,7 +266,50 @@ public void Transaction_can_issue_watch()
265266

266267
Assert.That(Redis.GetValue(Key), Is.EqualTo("7"));
267268
Assert.That(Redis.GetValue(KeySquared), Is.Null);
269+
}
270+
271+
[Test]
272+
public void Can_set_Expiry_on_key_in_transaction()
273+
{
274+
var expiresIn = TimeSpan.FromMinutes(15);
275+
276+
const string key = "No TTL-Transaction";
277+
var keyWithTtl = "{0}s TTL-Transaction".Fmt(expiresIn.TotalSeconds);
278+
279+
using (var trans = Redis.CreateTransaction())
280+
{
281+
trans.QueueCommand(r => r.Add(key, "Foo"));
282+
trans.QueueCommand(r => r.Add(keyWithTtl, "Bar", expiresIn));
283+
284+
if (!trans.Commit())
285+
throw new Exception("Transaction Failed");
286+
}
287+
288+
Assert.That(Redis.Get<string>(key), Is.EqualTo("Foo"));
289+
Assert.That(Redis.Get<string>(keyWithTtl), Is.EqualTo("Bar"));
290+
291+
Assert.That(Redis.GetTimeToLive(key).TotalSeconds, Is.EqualTo(-1));
292+
Assert.That(Redis.GetTimeToLive(keyWithTtl).TotalSeconds, Is.GreaterThan(1));
293+
}
294+
295+
[Test]
296+
public void Does_not_set_Expiry_on_existing_key_in_transaction()
297+
{
298+
var expiresIn = TimeSpan.FromMinutes(15);
299+
300+
var key = "Exting TTL-Transaction";
301+
Redis.Add(key, "Foo");
302+
303+
using (var trans = Redis.CreateTransaction())
304+
{
305+
trans.QueueCommand(r => r.Add(key, "Bar", expiresIn));
306+
307+
if (!trans.Commit())
308+
throw new Exception("Transaction Failed");
309+
}
268310

311+
Assert.That(Redis.Get<string>(key), Is.EqualTo("Foo"));
312+
Assert.That(Redis.GetTimeToLive(key).TotalSeconds, Is.EqualTo(-1));
269313
}
270314
}
271315
}

0 commit comments

Comments
 (0)