Question

How do you think is it a good practice to cache objects without expiration like this:

HttpContext.Current.Cache.Insert(
    key,
    value,
    null,
    System.Web.Caching.Cache.NoAbsoluteExpiration,
    System.Web.Caching.Cache.NoSlidingExpiration);

When I get entity from database, I put it to the Cache and if object changed I deleted cache item. And I don't need expiration, I think it will be good for performance.

Was it helpful?

Solution

If you never want the items to expire you can use a ConcurrentDictionary instead and put it in a static variable. It would also work for scenarios when the HttpContext is not available.

But you can never count on an item being in the Cache (or ConcurrentDictionary) since IIS recycles the application pool when it feels like it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top