如何获得缓存项到期日? [英] How to get expiry date for cached item?

查看:143
本文介绍了如何获得缓存项到期日?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我存储一个对象中的MemoryCache:

I store an object in MemoryCache:

void foo()
{
  ObjectCache cache = MemoryCache.Default;
  SomeClass obj = cache["CACHE_KEY"] as SomeClass;
  if (null == obj )
  {
     obj = new SomeClass(); ....
     CacheItemPolicy policy = new CacheItemPolicy();
     //update
     policy.AbsoluteExpiration = DateTime.Now+TimeSpan.FromMinutes(1);
     cache.Set("CACHE_KEY", obj, policy);
  }
  else
  {
     //get expiry date 
  }
  .....
}

时有可能得到有效期限不知何故,如果缓存包含对象?

Is it possible to get expiration date somehow if cache contains the object ?

推荐答案

由于要设置滑动过期,是不是总是从你访问它的时间为10分钟呢?如果对象为null,缓存条目已过期,如果没有,到期(在上面的code)始终为10分钟,从你检查的时间?

Since you are setting the sliding expiration, isn't it always 10 minutes from the time you accessed it? if the object is null, the cache entry has expired and if not, the expiration (in the code above) is always 10 minutes from the time you checked?

或者你可以有一个基本对象(即所有缓存对象从继承)的到期时间为被设置在您添加到高速缓存中的时间属性。当您提取对象,你检查的财产,你有到期时间来计算的差异。仅仅是一个想法。

Or you could have a base object (that all your cacheable objects inherits from) with the expiry time as a property that is set at the time you add to cache. When you extract the object, you check for the property and you have the expiration time to calculate the difference. Just a thought.

这篇关于如何获得缓存项到期日?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆