Class Act

Back to the shelf

C# · Method · ASP.net

Cache insert with an absolute expiration

C# code to store an object in ASP.NET's server-side cache until a specific clock time expires.

Example

How to use Cache insert with an absolute expiration:
Paste the code into the class or page, then call it from the method that needs the result.

Notes

Absolute expiration expires at a fixed time. Sliding expiration resets each time the value is read. The cache is wiped when the app pool restarts.

Code

// using System.Web;
// using System.Web.Caching;
HttpRuntime.Cache.Insert(
    "item-list",
    value,
    null,
    DateTime.UtcNow.AddMinutes(10),
    Cache.NoSlidingExpiration);