我使用 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));
which returns进行调试-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);
}
}
}
}