Redis过期时间不起作用。

5
我使用 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 服务器 2.2.2。 - NickD
1个回答

16

抱歉,我无法很好地读懂你的代码,以便知道你是否/何处出错。

我有很多对过期和过期时间操作的测试,以下是一些示例,这些示例可能更好地演示如何使用它:

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

如果您仍然遇到问题,请发布一个可运行的代码片段或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 客户端(v2.07)中添加了上述方法,您可以在此处下载: https://github.com/ServiceStack/ServiceStack.Redis/downloads

使用您的 CacheRecord 进行测试

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


谢谢!你的单元测试有效了!但是我不明白为什么它在我的代码中不起作用,请看上面。 - NickD
我同意Snoopy的说法,那对我也非常有帮助。神话,你提供了很好的支持,干得好! - sacha barber

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接