2011-03-17 59 views
5

我使用ServiceStack.Redis(从最新消息来源编译:https://github.com/ServiceStack/ServiceStack.Redis/tree/master/src)。Redis过期不起作用

我做这样的事情:

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

我尝试使用 Console.WriteLine(cacheRecords.GetTimeToLive(p_sParentKey));返回-00:00:01调试。 它不关心我分配给validityPeriodInMinutes的值。

我也试过Expire,ExpireAt,ExpireEntryAt,ExpireEntryIn。我也试过这样的事情:

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<CacheRecordChild>(); 
    } 

    public string Id { get; set; } 
    public List<CacheRecordChild> 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>()) 
      { 
       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); 
       } 
      } 
     } 
    } 
+0

Btw。我使用redis服务器2.2.2 for windows x64。 – NickD 2011-03-17 17:10:12

回答

15

对不起,但我不能真正阅读你的代码,以便知道你是否做错了。

我有过期公平一些测试,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的使用瓮关键。如果您有实体,则可以使用ToUrn()扩展方法,例如

var cacheKey = foundKey.ToUrn(); 

否则,如果你只是有 '身份证',你可以创建瓮关键,如:

var cacheKey = IdUtils.CreateUrn<CacheRecord>(p_sParentKey); 

从那里你可以终止你的条目:

redisClient.Expire(cacheKey, validityPeriodInMinutes * 60); 

虽然我知道这是不直观的,所以我会在未来版本中添加一个RedisTypedClient.ExpiryIn方法这会自动为你做。那么这应该让你做这样的事情:

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

编辑:添加方法重载:

我在Redis的客户端的最新版本中加入上述方法(V2。07)你可以在这里下载: https://github.com/ServiceStack/ServiceStack.Redis/downloads

With tests using your CacheRecord

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

+0

谢谢!你的单元测试工作!但我不明白为什么它不能在我的代码中工作,请参阅上文。 – NickD 2011-03-18 09:15:24

+0

哇...我印象深刻。谢谢!!!真棒支持! – NickD 2011-03-19 13:35:30

+0

我同意史努比,也完全帮助我。那里有神话的大力支持。路要走 – sacha 2012-02-27 14:13:07