【问题标题】:Redis Expire does not workRedis 过期不起作用
【发布时间】:2011-07-17 14:08:34
【问题描述】:

我使用 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)); 返回 -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<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);
                }
            }
        }
    }

【问题讨论】:

  • 顺便说一句。我在 windows x64 上使用 redis server 2.2.2。

标签: c# redis servicestack


【解决方案1】:

抱歉,我不能很好地阅读您的代码,以便了解您是否/您做错了什么。

我对 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);
}

如果您仍然有问题,请您发布一个可运行的代码 sn-p 或 gist,以便我更好地阅读您的代码。

编辑:回答代码示例

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

'urn:CacheRecord:' + p_sParentKey

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

redisClient.Expire(p_sParentKey, validityPeriodInMinutes * 60);

所以有几种方法可以获取 Redis 中使用的 urn key。如果你有实体,你可以使用 ToUrn() 扩展方法,例如

var cacheKey = foundKey.ToUrn();

否则,如果您只有“Id”,则可以创建 urn 密钥,例如:

var cacheKey = IdUtils.CreateUrn<CacheRecord>(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

With tests using your CacheRecord.

顺便说一句:Redis 客户端实际上不需要 [Serializer] 属性。

【讨论】:

  • 谢谢!您的单元测试有效!但我不明白为什么它在我的代码中不起作用,请参见上文。
  • 我同意史努比的观点,这也完全帮助了我。伟大的支持有神话。一路走来
猜你喜欢
  • 2020-10-20
  • 2020-09-20
  • 1970-01-01
  • 2017-05-04
  • 1970-01-01
  • 2012-05-23
  • 1970-01-01
  • 2016-05-20
相关资源
最近更新 更多