Wednesday 24 January 2007 5:05:00 pm
The static caching feature is available in eZ Publish 3.6 and higher. It uses Apache rewrite rules to check if a static file exists, then serves it straight from disk (and thus avoids using the PHP processor). This means that the complete page can be stored as an XHTML file. In the case that the file does not exist, the request is simply sent through to eZ Publish, where there could still be cached elements in the template file as described above. Using the static cache can lead to greatly improved performance, but it restricts some functionality. It is normal to run the static cache in combination with fully dynamic pages. You can run the static cache on part of your site if:
Since most eZ Publish sites use one or all of the features above for at least part of the site, you can benefit by using the static cache for those parts. A typical scenario is:
In this situation, there is one siteaccess (in this example, ez.no) for which we want to implement static caching. First, modify the Apache VHOST configuration to include the rewrite rules below. You should only add rewrite rules for the VHOSTs for which you want to have static caching. In reality, this means that for each siteaccess that you want to cache, you need a different VHOST block in the Apache configuration.
RewriteEngine On RewriteCond /dat/ez.no/static/index.html -f RewriteRule ^/$ /static/index.html [L] RewriteCond /dat/ez.no/static/index.html -f RewriteRule ^$ /static/index.html [L] RewriteCond %{REQUEST_METHOD} !^POST$ RewriteCond /dat/ez.no/static$1/index.html -f RewriteRule ^(.*)$ /static$1/index.html [L] RewriteRule !\.(gif|css|jpg|png|jar|ico|js)$ /index.php
You will need to change /dat/ez.no to the path corresponding to the root location of your eZ Publish installation. In settings/override/site.ini.append.php, add the following settings to enable the static caching process:
[ContentSettings] StaticCache=enabled
In settings/override/staticcache.ini.append.php, configure the details about the host and the pages to cache:
[CacheSettings] HostName=tequila:1400 StaticStorageDir=static MaxCacheDepth=4 # A list of url's to cache CachedURLArray[]=/*
HostName is the host where the pages are viewed. The static cache feature uses this to retrieve the generated content to store as cache files. StaticStorageDir is the directory where the static cache files are stored. It is relative to the root directory of your eZ Publish installation. This needs to match the part between /dat/ez.no/ and $1/index.html in the rewrite rules above. MaxCacheDepth is the maximum number of directory levels to cache, as seen from the root of your installation. CachedURLArray specifies the parts of your site that are allowed to be statically cached. Use / or /products to cache only one page (in this example, the front page and the /products page). You can also use wildcards; for example, with "/products*" all URLs below /products are cached.
In this example there are two site languages: English and French. The siteaccesses are called news_en and news_fr. In this case, we use the following rewrite rules, where the root directory of the eZ Publish installation is /home/httpd/ez-3.6:
RewriteEngine On RewriteLog /tmp/rewrite RewriteLogLevel 4 RewriteCond /home/httpd/ez-3.6/static/news_en/index.html -f RewriteRule ^/$ /static/news_en/index.html [L] RewriteCond /home/httpd/ez-3.6/static/news_en/index.html -f RewriteRule ^$ /static/news_en/index.html [L] RewriteCond /home/httpd/ez-3.6/static/news_fr/index.html -f RewriteRule ^/$ /static/news_fr/index.html [L] RewriteCond /home/httpd/ez-3.6/static/news_fr/index.html -f RewriteRule ^$ /static/news_fr/index.html [L] RewriteCond %{REQUEST_METHOD} !^POST$ RewriteCond /home/httpd/ez-3.6/static$1/index.html -f RewriteRule ^(.*)$ /static$1/index.html [L] RewriteRule !\.(gif|css|jpg|png|jar|ico|js)$ /index.php
The following settings need to be specified: In settings/override/site.ini.append.php:
[ContentSettings] StaticCache=enabled
In settings/siteaccess/news_en/staticcache.ini.append.php:
[CacheSettings] StaticStorageDir=static/news_en
In settings/siteaccess/news_fr/staticcache.ini.append.php:
[CacheSettings] StaticStorageDir=static/news_fr
Make sure that the part after static is the same as the name of the siteaccess! You can of course change static to something else, but make sure it is the same as in the rewrite rules. In settings/override/staticcache.ini.append.php, we then configure the static cache mechanism:
[CacheSettings] HostName=localhost MaxCacheDepth=4 # A list of url's to cache CachedURLArray[] CachedURLArray[]=/news* CachedURLArray[]=/weblog* CachedSiteAccesses[] CachedSiteAccesses[]=news_en CachedSiteAccesses[]=news_fr
This caches the /news and /weblog subtrees of the site on the host localhost with a maximum folder depth of 4.
Static cache files are created in two ways:
The bin/php/makestaticcache.php script generates all the static cache files for a specific siteaccess. In our example with ez.no, use the following command to generate the cache files:
php bin/php/makestaticcache.php -s ez.no
If you want to re-create all cache files, even the ones that already exist, use the -f parameter to force the generation of all static cache files. The command below regenerates all static cache files for both siteaccesses from our second example:
php bin/php/makestaticcache.php -f