简体   繁体   中英

C# Cache expiration not working

I am adding to the following 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 .

我不知道为什么你的代码不起作用,但如果你不想使值过期,那么你可以使用HttpContext.Current.Application.Add("ClientID", value)

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