Attention! Helicon Tech Blog has moved to

Wednesday, January 21, 2009

Guide: Example of mod_cache application

In the previous articles we told you what cache is and how it works in Helicon Ape. Now it’s time to use obtained knowledge in practice. Today we gonna apply caching for PHP application called qdig that helps organize images web-gallery. Read how to register PHP on IIS7 in our article about WordPress.

Creating online photo album

Let’s create photos folder in site root and fill it with our photos. Now we are downloading qdig. To make it simpler we’ll extract only one index.php file and put it into the same directory.

The gallery is already working: http://localhost/photos/index.php

Measuring performance

To measure request rate we’ll use ab.exe application:
ab.exe -n 200 -c 2 "http://localhost/photos/index.php?Qwd=.&Qif=DSC00410.JPG&Qiv=name&Qis=M"
The result is a bit more than 16 requests per second.

Switching on mod_cache and mod_expires

To enable necessary modules, let’s uncomment the following lines in Helicon Ape httpd.conf file:
LoadModule expires_module    modules/
LoadModule cache_module modules/

Analyzing cached request

To make mod_cache cache not all requests but only unique ones, let’s figure out what qdig request parameters mean and how request uniqueness depends on them:
  • Qwd - folder where image files reside - AFFECTS request uniqueness;
  • Qif - file name - AFFECTS request uniqueness;
  • Qiv - mode of file names representation - AFFECTS request uniqueness;
  • Qis - image size - DOESN’T AFFECT request uniqueness;
  • Qtmp - representation mode - DOESN’T AFFECT request uniqueness;
Thus, cache key will use only Qwd, Qif and Qiv parameters.
The piece of config for mod_cache will look like:
<Files index.php>
CacheEnable mem
CacheVaryByParams Qwd Qif Qiv

Expiration time

index.php script does not set Cache-Control and Expires headers, but, as we already know, they are really important for successful caching. So we’ll set these headers by ourselves. And for that purpose we’ll use mod_expires functionality:
ExpiresActive On
ExpiresByType text/html "access 1 hour"
Above directives set expiration time to 1 hour.
The resulting .htaccess is as follows:

Measuring performance once again

ab.exe -n 200 -c 2 "http://localhost/photos/index.php?Qwd=.&Qif=DSC00410.JPG&Qiv=name&Qis=M"
And now the result is about 94 requests per second!

That’s all you need to do to achieve sixfold performance growth.
This example clearly demonstrates the ease and efficiency of Helicon Ape caching feature.

No comments:

Post a Comment