Redis Expire不起作用

我使用ServiceStack.Redis(从最新的源代码构建: https : //github.com/ServiceStack/ServiceStack.Redis/tree/master/src )。

我做这样的事情:

CacheRecord foundKey = cacheRecords.GetById(p_sParentKey); ... CacheRecord cacheRecord = cacheRecords.Store(foundKey); ... redisClient.Expire(p_sParentKey, validityPeriodInMinutes * 60); 

我尝试使用Console.WriteLine(cacheRecords.GetTimeToLive(p_sParentKey));进行调试Console.WriteLine(cacheRecords.GetTimeToLive(p_sParentKey)); 返回-00:00:01 。 我分配给validityPeriodInMinutes的值是无关紧要的。

我试过ExpireExpireAtExpireEntryAtExpireEntryIn 。 我也试过这样的事情:

 int epoch = (int)(DateTime.UtcNow.AddSeconds(validityPeriodInMinutes) - new DateTime(1970, 1, 1)).TotalSeconds; redisClient.ExpireAt(p_sParentKey, epoch); 

任何的想法?

 [Serializable] public class CacheRecord { public CacheRecord() { this.Children = new List(); } public string Id { get; set; } public List Children { get; set; } } #endregion #region public class CacheRecordChild [Serializable] public class CacheRecordChild { public string Id { get; set; } public string Data { get; set; } } public void AddKey(string p_sParentKey, string p_sChildKey, string p_sData, int validityPeriodInMinutes) { using (ServiceStack.Redis.RedisClient redisClient = new ServiceStack.Redis.RedisClient()) { using (var cacheRecords = redisClient.GetTypedClient()) { CacheRecord foundKey = cacheRecords.GetById(p_sParentKey); if (foundKey == null) { foundKey = new CacheRecord(); foundKey.Id = p_sParentKey; CacheRecordChild child = new CacheRecordChild(); child.Id = p_sChildKey; child.Data = p_sData; foundKey.Children.Add(child); CacheRecord cacheRecord = cacheRecords.Store(foundKey); //redisClient.Expire(p_sParentKey, validityPeriodInMinutes); redisClient.Expire(p_sParentKey, validityPeriodInMinutes * 60); } else { CacheRecordChild child = new CacheRecordChild(); child.Id = p_sChildKey; child.Data = p_sData; foundKey.Children.Add(child); CacheRecord cacheRecord = cacheRecords.Store(foundKey); // redisClient.Expire(p_sParentKey, validityPeriodInMinutes); // redisClient.Expire(p_sParentKey, validityPeriodInMinutes * 60); // int epoch = (int)(DateTime.UtcNow.AddSeconds(validityPeriodInMinutes) - new DateTime(1970, 1, 1)).TotalSeconds; redisClient.Expire(p_sParentKey, validityPeriodInMinutes * 60); } } } } 

对不起,但我不能很好地阅读你的代码,以便知道你做错了什么/你做错了什么。

我对Expire,ExpireAt操作进行了一些测试,下面是一些可以更好地演示如何使用它的测试:

https://github.com/ServiceStack/ServiceStack.Redis/blob/master/tests/ServiceStack.Redis.Tests/RedisClientTests.cs

 [Test] public void Can_Expire() { Redis.SetEntry("key", "val"); Redis.Expire("key", 1); Assert.That(Redis.ContainsKey("key"), Is.True); Thread.Sleep(2000); Assert.That(Redis.ContainsKey("key"), Is.False); } [Test] public void Can_ExpireAt() { Redis.SetEntry("key", "val"); var unixNow = DateTime.Now.ToUnixTime(); var in1Sec = unixNow + 1; Redis.ExpireAt("key", in1Sec); Assert.That(Redis.ContainsKey("key"), Is.True); Thread.Sleep(2000); Assert.That(Redis.ContainsKey("key"), Is.False); } 

如果您仍有问题,可以发布一个可运行的代码段或要点,以便我可以更好地阅读您的代码。

编辑:回答代码示例

使用类型化客户端时,最终存储在Redis中的密钥如下所示:

 'urn:CacheRecord:' + p_sParentKey 

这与您在示例中使用的内容不同:

 redisClient.Expire(p_sParentKey, validityPeriodInMinutes * 60); 

因此,有两种方法可以获取Redis中使用的urn键 。 如果你有实体,你可以使用ToUrn()扩展方法,例如

 var cacheKey = foundKey.ToUrn(); 

否则,如果你只有’Id’,你可以创建urn键,如:

 var cacheKey = IdUtils.CreateUrn(p_sParentKey); 

从那里你可以使你的参赛作品到期:

 redisClient.Expire(cacheKey, validityPeriodInMinutes * 60); 

虽然我理解这是不直观的,所以我将在未来的版本中添加一个RedisTypedClient.ExpiryIn方法,它将自动为您执行此操作。 这应该让你做一些像:

 cacheRecords.ExpireIn(p_sParentKey, TimeSpan.FromMinutes(validityPeriodInMinutes)); 

编辑:添加方法重载:

我在最新版本的Redis Client(v2.07)中添加了上述方法,您可以在此处下载: https : //github.com/ServiceStack/ServiceStack.Redis/downloads

使用您的CacheRecord进行测试 。

BTW:Redis客户端实际上并不需要[Serializer]属性。