开发者

C# Cache expiration not working

开发者 https://www.devze.com 2023-04-01 02:16 出处:网络
I am adding to the foll开发者_StackOverflowowing value to the cache: HttpContext.Cache.Insert( \"ClientId\", clientid, null,

I am adding to the foll开发者_StackOverflowowing value to the cache:

HttpContext.Cache.Insert(
    "ClientId", clientid, null,
    DateTime.UtcNow.AddYears(1), TimeSpan.Zero
);

I don't want the cache to expire so I set the date 1 year from now (DateTime.UtcNow.AddYears(1)) however after 30 minutes the cached value is no longer there.


You need to use the Application collection if you want items to stay forever not the Cache. Cache is not guaranteed to keep the item for the time you specify if there is memory pressure. Also judging by the 30 minutes you've mentioned there is a possibility that your application domain gets recycled if there is no user activity on the site.


From MSDN:

If you are using absolute expiration, the slidingExpiration parameter must be NoSlidingExpiration.


I don't know why your code doesn't work but if you don't want to expire the value, then you can use the HttpContext.Current.Application.Add("ClientID", value)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号