问题描述
我用ServiceStack.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哪个值。
我试过藏汉过期
, ExpireAt
, ExpireEntryAt
, ExpireEntryIn
。我也尝试过这样的事情:
INT划时代=(INT)(DateTime.UtcNow.AddSeconds(validityPeriodInMinutes) - 新的日期时间( 1970年,1,1))TotalSeconds。
redisClient.ExpireAt(p_sParentKey,纪元);
任何想法?
[Serializable接口]
公共类CacheRecord
{
公共CacheRecord()
{
this.Children =新的List< CacheRecordChild>();
}
公共字符串ID {搞定;组; }
公开名单< CacheRecordChild>儿童{搞定;组; }
}
#endregion
#地区的公共类CacheRecordChild
[Serializable接口]
公共类CacheRecordChild
{
公共字符串标识{搞定;组; }
公共字符串数据{搞定;组; }
}
使用公共无效AddKey(字符串p_sParentKey,串p_sChildKey,串p_sData,诠释validityPeriodInMinutes)
{
(ServiceStack.Redis.RedisClient redisClient =新ServiceStack .Redis.RedisClient())
{使用
(VAR cacheRecords = redisClient.GetTypedClient< CacheRecord>())
{
CacheRecord foundKey = cacheRecords.GetById(p_sParentKey);
如果(foundKey == NULL)
{
foundKey =新CacheRecord();
foundKey.Id = p_sParentKey;
CacheRecordChild孩子=新CacheRecordChild();
child.Id = p_sChildKey;
child.Data = p_sData;
foundKey.Children.Add(小孩);
CacheRecord cacheRecord = cacheRecords.Store(foundKey);
//redisClient.Expire(p_sParentKey,validityPeriodInMinutes);
redisClient.Expire(p_sParentKey,validityPeriodInMinutes * 60);
}
,否则
{
CacheRecordChild孩子=新CacheRecordChild();
child.Id = p_sChildKey;
child.Data = p_sData;
foundKey.Children.Add(小孩);
CacheRecord cacheRecord = cacheRecords.Store(foundKey);
// redisClient.Expire(p_sParentKey,validityPeriodInMinutes);
// redisClient.Expire(p_sParentKey,validityPeriodInMinutes * 60);
// INT划时代=(INT)(DateTime.UtcNow.AddSeconds(validityPeriodInMinutes) - 新的日期时间(1970年,1,1))TotalSeconds。
redisClient.Expire(p_sParentKey,validityPeriodInMinutes * 60);
}
}
}
}
很抱歉,但我真的不能看你的代码非常好,才能知道/你在做什么错了。
我对过期,ExpireAt操作的公平一些测试,这里有一些低于可以更好地演示了如何使用它:
的
[测试]
公共无效Can_Expire()
$ { b $ b Redis.SetEntry(钥匙,VAL);
Redis.Expire(钥匙,1);
Assert.That(Redis.ContainsKey(钥匙),Is.True);
Thread.sleep代码(2000);
Assert.That(Redis.ContainsKey(钥匙),Is.False);
}
[测试]
公共无效Can_ExpireAt()
{
Redis.SetEntry(钥匙,VAL);
VAR unixNow = DateTime.Now.ToUnixTime();
VAR in1Sec = unixNow + 1;
Redis.ExpireAt(钥匙,in1Sec);
Assert.That(Redis.ContainsKey(钥匙),Is.True);
Thread.sleep代码(2000);
Assert.That(Redis.ContainsKey(钥匙),Is.False);
}
如果你仍然有问题,你能不能请张贴执行的代码。片段或要点,所以我可以更好地阅读你的代码
编辑:答案为代码示例
当您使用一个类型的客户,最终被存储在Redis的关键是这样的:
骨灰盒:CacheRecord:'+ p_sParentKey
这是一个不同的密钥,以你使用在你的榜样是什么:
redisClient.Expire(p_sParentKey,validityPeriodInMinutes * 60);
因此,有几种方法来获得的瓮键就是使用在Redis的。如果你有实体,你可以使用ToUrn()扩展方法,例如
VAR cacheKey = foundKey.ToUrn();
否则,如果你只是有'ID',你可以创建在瓮键这样的:
VAR cacheKey = IdUtils.CreateUrn< CacheRecord>(p_sParentKey);
从那里,你可以终止你的条目:
redisClient.Expire(cacheKey,validityPeriodInMinutes * 60);
虽然我明白这是怎么不直观,所以我会添加一个 RedisTypedClient。 ExpiryIn 方式在未来的版本这将自动为您做到这一点。那么这应该让你做这样的事情:
cacheRecords.ExpireIn(p_sParentKey,TimeSpan.FromMinutes(validityPeriodInMinutes));
编辑:添加方法重载:
我已经添加上述方法在Redis的客户端(v2.07),你可以在这里下载最新版本:
的
的。
BTW :Redis的客户实际上并不需要[串行]属性。
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 usingConsole.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);
}
}
}
}
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:
[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.
这篇关于Redis的到期不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!