All the examples in this chapter so far have cached pages or parts of pages wrapped in user controls. But ASP.NET allows you much more caching flexibility. You can use object caching to place any object in the cache. The object can be of almost any type: a data type, a web control, a class, a data set, etc.
Unlike output caching, which stores its data on a hard drive somewhere, the object cache is stored in server memory. As such, it is a limited resource and the careful developer will husband that resource carefully. That said, it is an easy way to buy significant performance benefits when used wisely.
Suppose you are developing a retail shopping catalogue web application. Many of the page requests contain queries against the same database to return a relatively static price list and descriptive data. Instead of your control requerying the database each time the data is requested, the data set is cached, so that subsequent requests for the data will be satisfied from high speed cache rather than the slow and expensive regeneration of the data. You might want to set the cache to expire every minute, hourly, or daily, depending on the needs of the application and the frequency with which the data is likely to change.
Object caching is implemented by the Cache class. One instance of this class is created automatically per application domain when the application starts. The class remains valid for the life of the application. The Cache class uses syntax very similar ...