简体   繁体   中英

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. 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:

  1. The code manually clears an entry. By using the Remove method of the Cache object.

  2. The cache entry expires. By using the absoluteExpiration or slidingExpiration parameters of the Add and Insert methods of the Cache object.

3.The host process ends (application or IIS reset , application pool recycle , etc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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