- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我使用 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 分配哪个值并不重要。
我还尝试了 Expire
、ExpireAt
、ExpireEntryAt
、ExpireEntryIn
。我也尝试过这样的事情:
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);
}
}
}
}
最佳答案
抱歉,但我真的无法很好地阅读您的代码,无法了解您是否/在何处做错了。
我对 Expire、ExpireAt 操作进行了相当多的测试,以下是一些可以更好地演示如何使用它的测试:
[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 中的 key 如下所示:
'urn:CacheRecord:' + p_sParentKey
这与您在示例中使用的 key 不同:
redisClient.Expire(p_sParentKey, validityPeriodInMinutes * 60);
所以有几种方法可以获取 Redis 中使用的 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
With tests using your CacheRecord .
顺便说一句:Redis 客户端实际上不需要 [Serializer] 属性。
关于c# - Redis过期不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5342609/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!