2011-03-17 56 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