简体   繁体   English

HttpContext.Current.Cache 在一段时间后清除

[英]HttpContext.Current.Cache clears after while

I'm using HttpContext cache in a console application and after a while(2 or 3 hours) HttpContext cache is removed automatically.我在控制台应用程序中使用HttpContext缓存,一段时间(2 或 3 小时)后, HttpContext缓存会自动删除。 How to stop clearing cache??如何停止清除缓存?

private List<Model.CacheModel> _CompleteList = null;
public List<Model.CacheModel> CompleteList
{
   get
   {
       if (_CompleteList == null)
       {
           _CompleteList = (HttpContext.Current.Cache["CompleteList"] as List<Model.CacheModel>);
            if (_CompleteList == null)
            {
              _CompleteList = new List<Model.CacheModel>();
               HttpContext.Current.Cache.Insert("CompleteList", _CompleteList);
            }
       }

       return _CompleteList;
    }

    set
    {
       HttpContext.Current.Cache.Insert("CompleteList", _CompleteList);
    }
 }

This is where I use this property这是我使用此属性的地方

public void GetControl(List<Model.CacheModel> List)
{

        var CahcedList = (HttpContext.Current.Cache["CompleteList"] as List<Model.CacheModel>);
        if (CahcedList == null)
        {
            HttpContext.Current.Cache["CompleteList"] = List;
        }
        else
        {
            if (CahcedList.LastOrDefault().Time != List.LastOrDefault().Time)
            {
               CahcedList.Remove(CahcedList.FirstOrDefault());
               CahcedList.Add(List.LastOrDefault());        
               Clients.Others.broadcastAll(
                  JsonConvert.SerializeObject(List.LastOrDefault()));
            }
        }
}

According to MSDN, the cache will be cleared on the following scenarios:根据 MSDN,缓存将在以下情况下被清除:

  1. The code manually clears an entry.该代码手动清除条目。 By using the Remove method of the Cache object.通过使用Cache对象的Remove方法。

  2. The cache entry expires.缓存条目过期。 By using the absoluteExpiration or slidingExpiration parameters of the Add and Insert methods of the Cache object.通过使用Cache对象的Add 和Insert方法的absoluteExpirationslidingExpiration参数。

3.The host process ends (application or IIS reset , application pool recycle , etc). 3.宿主进程结束(应用程序或IIS resetapplication pool recycle等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM