Data and page caching
Jun 17, 2009
Caching is important in the sense that caching frequently accessed data can bring a major improvement to the responsiveness of a site, as the processing of the page does not rely each time on a database query. By caching the HTML generated by a page request, the response time can be reduced close to zero, because the cached page need not be processed at all. Although both PHP and ASP.NET provide support for a few caching alternatives, with PHP providing the basic tools for caching, ASP.NET brings some other methods for caching and data management, and this allows developers to choose the most adequate method for their development needs.
PHP does not have native support for page caching, but this can be done through third party packages or programmatically. In PHP, page caching is usually done on the server in several ways, such as writing the output of the page to a separate file that is then re-served on subsequent requests.
In ASP.NET this can be done at both low-level, through the OutputCache API, and high-level through an OutputCache directive. The basic point is that, when output caching is enabled, an output cache entry is created on the first "Get" request to the page. Other "Get" or "Head" requests will be served from the output cache entry until the cached data expires.
The output caching technique is rather simple. The output cache respects the expiration and validation policies for pages, meaning that if a page is in the output cache and has an expiration policy that renders the cache obsolete after 60 min from the time it was created, then the page will be removed from the output cache after 60 min. if another request is made after this time has passed, the page code is executed and the page can be cached again. This method of expiration policy is called absolute expiration, in the sense that a page is valid within a time limit.
ASP.NET provides an easy way for the developer to cache just certain parts of a page, also known as fragment caching. The developer outlines specific areas of a page by using a user control, and can mark them with the OutputCache directive. This directive will specify the duration (in seconds) in which the output content of the user control should be cached on the server, as well as other optional conditions that allow developers to further tweak the cache behavior.
Just as above, there are different ways to cache query results in PHP, but none of them are natively supported in the development language. Building data caching classes or systems in PHP can be performed either in an easy manner for small amounts of information, through Session variables, or in a more complicated way for larger amounts of information. The downside of working with large types of complex data in PHP is that the work can be inefficient, and prone to errors.
ASP.NET, on the other hand, can offer a stable system-wide method for caching data (DataSets, arrays, collections, XML objects and others) by using the "Page.Cache" object. For sophisticated application functionality, ASP.NET caching supports three different ways of caching: expiration, scavenging, and file and key dependencies.
The expiration method allows developers to control when exactly a cached item expires. This is done either as a specific time (say 03:00), or it can be contingent on the item's last use (like 30 minutes after it's last request). After the item has expired, it will be removed from the cache and future requests will return null values until the item is reinserted in the cache.
The scavenging method is more a space saver, because it tries to remove rarely used or unimportant items, when there is little memory left. Programmers are enabled to control how and when the scavenging takes place and they are able to provide the scavenger tool with hints when items are inserted into the cache about the relative cost of creating the item in question and the relative rate the item must be accessed in order to remain useful.
The file and key dependencies method enables the validity of a cached item to depend on an external file or another cache item. If the state of dependency changes, the cached item is invalidated and removed from cache. Example: when using a large report that is periodically updated from a file, the application will process the data in the file, generate the report and cache the data with a dependency on the file from which the respective data was read. When the file is updated, the data will be removed from the cache so that the application can reread it and reinsert the updated copy of the data.
1. Migrating from PHP to ASP.NET - Introduction
2. Feature comparison
3. Architecture and object oriented programming capabilities
5. Data and page caching