Redis过期不起作用

发布于 2024-10-22 12:18:02 字数 3165 浏览 6 评论 0原文

我使用 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。 我分配给validationPeriodInMinutes 的值并不重要。

我也尝试过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);
                }
            }
        }
    }

I use ServiceStack.Redis (build from the latest sources: https://github.com/ServiceStack/ServiceStack.Redis/tree/master/src).

I do something like this:

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

i tried to debug using
Console.WriteLine(cacheRecords.GetTimeToLive(p_sParentKey)); which returns -00:00:01.
it doesnt matter which value i assign to validityPeriodInMinutes.

I tried aswell Expire, ExpireAt, ExpireEntryAt, ExpireEntryIn. I also tried something like this:

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

any idea?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-10-29 12:18:02

抱歉,但我无法很好地阅读您的代码,以便知道您是否做错了。

我对 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 中使用的strong>urn key。如果您有实体,则可以使用 ToUrn() 扩展方法,例如,

var cacheKey = foundKey.ToUrn();

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

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] 属性。

Sorry but I can't really read your code very well in order to know if/what you're doing wrong.

I have a fair few tests for Expire, ExpireAt operations, here are some below which may better demonstrate how to use it:

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

If you're still having a problem, can you please post a runnable code snippet or gist so I can better read your code.

EDIT: Answer to code example

When you use a typed client, the key that ultimately gets stored in Redis looks like:

'urn:CacheRecord:' + p_sParentKey

Which is a different key to what you're using in your example:

redisClient.Expire(p_sParentKey, validityPeriodInMinutes * 60);

So there are a couple of ways to get the urn key that's used in Redis. If you have the entity you can use the ToUrn() Extension method, e.g.

var cacheKey = foundKey.ToUrn();

Otherwise if you just have the 'Id', you can create the urn key like:

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

From there you can expire your entry:

redisClient.Expire(cacheKey, validityPeriodInMinutes * 60);

Although I understand how this is not intuitive, so I will look to add a RedisTypedClient.ExpiryIn method in a future version which would do this automatically for you. this should then let you do something like:

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

EDIT: Added method overloads:

I've added the method above in the latest version of the Redis Client (v2.07) which you can download here:
https://github.com/ServiceStack/ServiceStack.Redis/downloads

With tests using your CacheRecord.

BTW: the Redis Client doesn't actually need [Serializer] attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文