Share via


The Effects of the Caching Handler

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

The example application benefits from the caching of method return values through configuration of the Policy Injection Application Block; no additional code is required provided that all types used as method input parameters correctly override the GetHashCode method. The policy defined in the Web.config file adds the caching handler to the handler pipeline, with an expiration time of 20 seconds. To demonstrate this, Figure 1 illustrates the result of executing the GetCustomerName method. The trace information, below the customer name that the method returns, shows that code in the ASP.NET page instantiated the CustomerModel class and then called the GetCustomerName method.

Ff650232.a00a1781-f953-42d6-a1bc-16f25b1cad58(en-us,PandP.10).png

Figure 1
Executing the GetCustomerName method, showing ASP.NET trace information

Clicking the same button within 20 seconds, to re-execute the method, results in the trace output shown in Figure 2. This time, the ASP.NET code instantiates the target class (it has to do this to create the handler pipeline), but it does not call the GetCustomerName method. This is because the Caching Handler detects that it has the return value cached, and it returns this instead of calling the method. This can significantly improve application efficiency and reduce response times for methods that require considerable processing or resources.

Ff650232.4579a67f-d10f-44db-a96c-ba9d80e203cd(en-us,PandP.10).png

Figure 2
Re-executing the GetCustomerName method; the return value comes from the cache