Recipe 9.3. Improving Site Performance


Problem

You want to decrease page load time and eliminate unnecessary hits on your web server.

Solution

Employ caching mechanisms to indicate to browsers in that web pages, images, and other site elements can be displayed from locally saved versions, and that must be refreshed from a newer version on the server. With Apache's mod_expires module, you can control caching by file type and directory. PHP's header( ) function and HTML <meta> tags provide caching control for individual pages.

Discussion

Cached files can speed the display of a web page. But lacking an expiration date for cached resources, a browser must still send a request to the server to check the validity of each file needed to render a page. Even when the server agrees that a file can be loaded from the cache, rather than downloading it again, the roundtrip between browser and server unnecessarily delays the page's load time and demands extra server processing cycles to complete. When the web server sends an explicit expiration time for each file, caching can eliminate the request-and-response routine the browser would otherwise need to validate its cache.

Apache's mod_expires module sets an expiration date for a resource and sends it to the browser in the Expires http response header for each request. The Expires header is a date and time after which a site file should not be cached. If you have access to your Apache configuration file (typically, httpd.conf) you can check to see if mod_expires has been installed for your web server, and enable the module if it was. Look for two lines similar to the following in adjacent sections of the configuration file, and uncomment them if necessary:

 LoadModule expires_module     modules/mod_expires.so AddModule mod_expires.c 

You need to restart Apache for any configuration change to take effect.


Then, in an .htaccess file, you can use mod_expires directives to set expiration dates for all files in a directory (and its subdirectories) or for specific types of files. For example, if you have a directory of images for your site navigation and logo, this code will enable caching for those files:

 ExpiresActive On ExpiresByType image/gif "access plus 1 month" ExpiresByType image/jpg "access plus 1 month" ExpiresByType image/png "access plus 1 month" 

The first line activates the Expires header for files sent from the directory. Then for each possible web site image type (GIFs, JPGs, and PNGs), the directive command access plus 1 month ensures that images will remain in the client's cache for a month after they were requested. Cache durations can be specified either from the current time (access) or from the file's last modified time (as in modification plus 1 month) in multiples of years, months, weeks, days, hours, minutes, and seconds.

If you have carefully arranged your site files by type and function (see Recipe 2.3), then you can devise a sophisticated caching strategy for other files, such as JavaScript libraries and stylesheets, on a per-directory basis.


You also can send headers programmatically from your PHP scripts. Say, for example, that you have a dynamic page that displays a different FAQ from a database every day. To minimize load on the web server and the database on subsequent requests for the page by the same visitor on the same day, set an Expires header for midnight the following day:

 <?   header('Expires: ' . gmdate('D, d M Y', time()+24*60*60) . ' 00:00:00 -0500  GMT'); ?> 

Calls to the header( ) function must be placed at the top of your PHP scripts, before any other PHP code or the <html> element that marks the beginning of the visible part of the page. As used above, the header( ) function will output Expires: Sat, 16 Jul 2005 00:00:00 -0500 GMT. Only after midnight Central Time the following day will the page be refreshed from the browser and a new FAQ displayed.

HTML <meta> tags also can control page caching. To set an expiration date for a page this way, add a line like this to the <head> section of your web page code:

 <meta http-equiv="Expires" content="Sat, 15 Jul 2006 00:00:00 -0500 GMT"> 

Setting the expiration date to some time in the future will cause the page to be saved in the user's browser cache until she requests the page again after that date or manually clears her browser cache. In contrast with dynamic PHP pages, there's no easy way to constantly update a <meta> tag expiration date in a static page, so using a future date works best on pages that you know will remain unchanged for a long time.

Often, <meta> tag expirations are set to a past date to force browsers to delete their cached version and request a fresh copy from the server:

 <meta http-equiv="Expires" content="Mon, 15 Jul 1996 00:00:00 -0500 GMT"> 

Or, to ensure that both old and new browsers never cache a frequently updated page, there are two other <meta> tags to use. For HTTP 1.0-compliant browsers, use:

 <meta HTTP-EQUIV="Pragma" content="no-cache"> 

And for HTTP 1.1. compliant browsers, use:

 <meta HTTP-EQUIV="Cache-Control" content="no-cache" > 

See Also

Recipe 2.3 provides more information on where certain files on your site should go. For an overview of mod_expires, see http://httpd.apache.org/docs/mod/mod_expires.html.



Web Site Cookbook.
Web Site Cookbook: Solutions & Examples for Building and Administering Your Web Site (Cookbooks (OReilly))
ISBN: 0596101090
EAN: 2147483647
Year: N/A
Pages: 144
Authors: Doug Addison

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net