sDima
sDima

Reputation: 396

HttpContext.Current.Cache without expiration

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.

Upvotes: 1

Views: 1095

Answers (1)

Magnus
Magnus

Reputation: 46997

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.

Upvotes: 1

Related Questions