Store Arbitrary Data in the ASP.NET Cache

Problem

You want to use caching, but you can’t cache an entire page because it includes some code that must run or some content that must be generated dynamically.

Solution

Use the built-in Cache object to store most .NET data types.

Discussion

The Cache object allows you to store almost any .NET object using a string key, with the expiration policy you define. ASP.NET maintains the cache automatically, evicting objects when they expire or when memory becomes scarce.

There are two types of expiration policies that you can use when storing data in the cache. Absolute expiration invalidates cached items after a fixed period of time, much like output caching. Absolute expiration is the best approach if you need ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.