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

Commit 72101c2

Browse files
committed
Implement new GetTimeToLive API to return TimeSpan?
1 parent 607ccaa commit 72101c2

File tree

5 files changed

+51
-9
lines changed

5 files changed

+51
-9
lines changed

src/ServiceStack.Redis/RedisClient.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Collections.Generic;
1616
using System.Linq;
1717
using System.Text;
18+
using ServiceStack.Caching;
1819
using ServiceStack.Redis.Generic;
1920
using ServiceStack.Redis.Pipeline;
2021
using ServiceStack.Text;
@@ -357,9 +358,16 @@ public bool ExpireEntryAt(string key, DateTime expireAt)
357358
}
358359
}
359360

360-
public TimeSpan GetTimeToLive(string key)
361+
public TimeSpan? GetTimeToLive(string key)
361362
{
362-
return TimeSpan.FromSeconds(Ttl(key));
363+
var ttlSecs = Ttl(key);
364+
if (ttlSecs == -1)
365+
return TimeSpan.MaxValue; //no expiry set
366+
367+
if (ttlSecs == -2)
368+
return null; //key does not exist
369+
370+
return TimeSpan.FromSeconds(ttlSecs);
363371
}
364372

365373
public IRedisTypedClient<T> As<T>()

src/ServiceStack.Redis/RedisClientManagerCacheClient.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace ServiceStack.Redis
1212
/// This works well for master-slave replication scenarios where you have
1313
/// 1 master that replicates to multiple read slaves.
1414
/// </summary>
15-
public class RedisClientManagerCacheClient : ICacheClient, IRemoveByPattern
15+
public class RedisClientManagerCacheClient : ICacheClient, IRemoveByPattern, ICacheClientExtended
1616
{
1717
private readonly IRedisClientsManager redisManager;
1818

@@ -194,5 +194,18 @@ public void RemoveByRegex(string pattern)
194194
{
195195
RemoveByPattern(pattern.Replace(".*", "*").Replace(".+", "?"));
196196
}
197+
198+
public TimeSpan? GetTimeToLive(string key)
199+
{
200+
using (var client = GetClient())
201+
{
202+
var redisClient = client as RedisClient;
203+
if (redisClient != null)
204+
{
205+
return redisClient.GetTimeToLive(key);
206+
}
207+
}
208+
return null;
209+
}
197210
}
198211
}

tests/ServiceStack.Redis.Tests/RedisCacheClientTests.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using NUnit.Framework;
23
using ServiceStack.Caching;
34
using ServiceStack.Common.Tests.Models;
@@ -7,7 +8,7 @@ namespace ServiceStack.Redis.Tests
78
[TestFixture]
89
public class RedisCacheClientTests
910
{
10-
private ICacheClient cacheClient;
11+
private ICacheClientExtended cacheClient;
1112

1213
[SetUp]
1314
public void OnBeforeEachTest()
@@ -103,5 +104,25 @@ public void Can_Replace_By_Pattern()
103104
result2 = cacheClient.Get<string>("string1");
104105
Assert.That(result2, Is.Null);
105106
}
107+
108+
[Test]
109+
public void Can_GetTimeToLive()
110+
{
111+
var model = ModelWithIdAndName.Create(1);
112+
string key = "model:" + model.CreateUrn();
113+
cacheClient.Add(key, model);
114+
115+
var ttl = cacheClient.GetTimeToLive(key);
116+
Assert.That(ttl, Is.EqualTo(TimeSpan.MaxValue));
117+
118+
cacheClient.Set(key, model, expiresIn: TimeSpan.FromSeconds(10));
119+
ttl = cacheClient.GetTimeToLive(key);
120+
Assert.That(ttl.Value, Is.GreaterThanOrEqualTo(TimeSpan.FromSeconds(9)));
121+
Assert.That(ttl.Value, Is.LessThanOrEqualTo(TimeSpan.FromSeconds(10)));
122+
123+
cacheClient.Remove(key);
124+
ttl = cacheClient.GetTimeToLive(key);
125+
Assert.That(ttl, Is.Null);
126+
}
106127
}
107128
}

tests/ServiceStack.Redis.Tests/RedisClientTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,11 @@ public void Can_GetTimeToLive()
203203
Redis.Expire("key", 10);
204204

205205
var ttl = Redis.GetTimeToLive("key");
206-
Assert.That(ttl.TotalSeconds, Is.GreaterThanOrEqualTo(9));
206+
Assert.That(ttl.Value.TotalSeconds, Is.GreaterThanOrEqualTo(9));
207207
Thread.Sleep(1700);
208208

209209
ttl = Redis.GetTimeToLive("key");
210-
Assert.That(ttl.TotalSeconds, Is.LessThanOrEqualTo(9));
210+
Assert.That(ttl.Value.TotalSeconds, Is.LessThanOrEqualTo(9));
211211
}
212212

213213
[Test]

tests/ServiceStack.Redis.Tests/RedisTransactionTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ public void Can_set_Expiry_on_key_in_transaction()
288288
Assert.That(Redis.Get<string>(key), Is.EqualTo("Foo"));
289289
Assert.That(Redis.Get<string>(keyWithTtl), Is.EqualTo("Bar"));
290290

291-
Assert.That(Redis.GetTimeToLive(key).TotalSeconds, Is.EqualTo(-1));
292-
Assert.That(Redis.GetTimeToLive(keyWithTtl).TotalSeconds, Is.GreaterThan(1));
291+
Assert.That(Redis.GetTimeToLive(key), Is.EqualTo(TimeSpan.MaxValue));
292+
Assert.That(Redis.GetTimeToLive(keyWithTtl).Value.TotalSeconds, Is.GreaterThan(1));
293293
}
294294

295295
[Test]
@@ -309,7 +309,7 @@ public void Does_not_set_Expiry_on_existing_key_in_transaction()
309309
}
310310

311311
Assert.That(Redis.Get<string>(key), Is.EqualTo("Foo"));
312-
Assert.That(Redis.GetTimeToLive(key).TotalSeconds, Is.EqualTo(-1));
312+
Assert.That(Redis.GetTimeToLive(key), Is.EqualTo(TimeSpan.MaxValue));
313313
}
314314
}
315315
}

0 commit comments

Comments
 (0)