Caching Page Output with Cache Key Dependencies
When you add pages to the output cache, the page will be removed when the amount of time you specify in the expiration policy has passed. There are times when you want a page, or versions of a page, to be removed from the output cache before it has expired. For example, if your page displays volatile data, such as a stock price or a temperature, the page will display incorrect information if the data is updated before the page expires.
To address this issue, ASP.NET provides the HttpResponse.AddCacheItemDependency and HttpResponse.AddCacheItemDependencies methods, which allow you to cache page output that is dependent upon an item in the Cache object (associated with the application that the page belongs to).
Note You can explicitly remove any page from the output cache by calling the HttpResponse.RemoveOutputCacheItem method. You can do this from the global.asax file, a custom ASP.NET server control you have created, or from a page, depending upon the needs of your application.
The AddCacheItemDependency method allows you to create a relationship between the page and a single item in the Cache, while the AddCacheItemDependencies method allows you to create relationship between the page and an array of Cache items. When any of the items upon which the page is dependent change or are removed from the application Cache, the page output is invalidated and removed from the output cache.
Note You cannot use these methods from a Web Forms user control.
To make cached page output dependent upon a Cache item
- Specify the settings for caching page output either declaratively or programmatically. For more information, see Setting Expirations for Page Caching, Setting the Cacheability of a Page, and Caching Multiple Versions of a Page.
- In the code-declaration block or the code-behind file for the page, add an item to your Web application's Cache object using the Cache.Item property, the Cache.Add method, or the Cache.Insert method.
- In the code-declaration block or the code-behind file for the page, use Response object syntax to call the AddCacheItemDependency or AddCacheItemDependencies method, specifying the cached item or items upon which the page is dependent.
The following example assumes that an application contains a temperature component. The component uses the Cache.Insert method to place a temperature into the application Cache.
Cache.Insert("Temperature.CurrentTemperature", currentTemperature); [Visual Basic] Cache.Insert("Temperature.CurrentTemperature", currentTemperature)
The following page code, found in a code-declaration block or code-behind file, gets the current temperature stored in the Cache, converts it to a string, and then displays it. It then makes its output cache version dependent upon the
Temperature.CurrentTemperature key. From this point forward, when the temperature component updates the temperature, the page version is flushed from the output cache. The page will be placed in the output cache again when it is subsequently requested.
int temperature = (int) Cache.Get("Temperature.CurrentTemperature"); LabelTemperature.Text = temperature.ToString(); Response.AddCacheItemDependency("Temperature.CurrentTemperature"); [Visual Basic] Dim temperature as Integer temperature = Cache.Get("Temperature.CurrentTemperature") LabelTemperature.Text = temperature.ToString() Response.AddCacheItemDependency("Temperature.CurrentTemperature")