[英]Redis Expire does not work
I use ServiceStack.Redis (build from the latest sources: https://github.com/ServiceStack/ServiceStack.Redis/tree/master/src ). 我使用ServiceStack.Redis(从最新的源代码构建: 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));
我尝试使用
Console.WriteLine(cacheRecords.GetTimeToLive(p_sParentKey));
进行调试Console.WriteLine(cacheRecords.GetTimeToLive(p_sParentKey));
which returns -00:00:01
. 返回
-00:00:01
。 it doesnt matter which value i assign to validityPeriodInMinutes. 我分配给validityPeriodInMinutes的值是无关紧要的。
I tried aswell Expire
, ExpireAt
, ExpireEntryAt
, ExpireEntryIn
. 我试着藏汉
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);
}
}
}
}
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: 我对Expire,ExpireAt操作进行了一些测试,下面是一些可以更好地演示如何使用它的测试:
https://github.com/ServiceStack/ServiceStack.Redis/blob/master/tests/ServiceStack.Redis.Tests/RedisClientTests.cs 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: 使用类型化客户端时,最终存储在Redis中的密钥如下所示:
'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. 因此,有两种方法可以获取Redis中使用的urn键 。 If you have the entity you can use the ToUrn() Extension method, eg
如果你有实体,你可以使用ToUrn()扩展方法,例如
var cacheKey = foundKey.ToUrn();
Otherwise if you just have the 'Id', you can create the urn key like: 否则,如果你只有'Id',你可以创建urn键,如:
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. 虽然我理解这是不直观的,所以我将在未来的版本中添加一个RedisTypedClient.ExpiryIn方法,它将自动为您执行此操作。 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 我在最新版本的Redis Client(v2.07)中添加了上述方法,您可以在此处下载: https : //github.com/ServiceStack/ServiceStack.Redis/downloads
With tests using your CacheRecord . 使用您的CacheRecord进行测试 。
BTW: the Redis Client doesn't actually need [Serializer] attribute. 顺便说一句:Redis客户端实际上并不需要[Serializer]属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.