Working with Caching (Page)

To access the Caching page, go to Rule Management | Caching.

 

The Page Cache is used to speed-up recurring executions of PHP scripts in your application. This is achieved by caching the PHP output (HTML) for specific URLs on first execution, to reuse the cached data for subsequent calls.
Cache behavior is defined using a flexible rule system that allows you to maintain the dynamic capabilities of your applications.

 

As opposed to other caching alternatives (Zend Server Data Cache and Zend Framework Zend Cache), the Zend Server Cache does not require any code changes and can be easily applied to existing applications. Moreover, while other caching solutions still run some code on recurring executions, the cache does not run any code to display the cached content, which results in improved performance.

 

Creating URL cache rules with Zend Server is a two-step process. In step one, you define the basic URL and conditions to apply. In step two, you define the cache duration and output options.

 

The following procedure describes how to create a cache rule. Click here to see how to create a copy of a cache rule

 

 

Instructions on how to complete a procedure

To create a Cache rule:

  1. Go to Rule Management | Caching

  2. Click add_rule.png to open the New Rule page.

  3. Name the rule.
    Make sure the name is descriptive and easy to remember: This name will appear in the main Caching page.

  4. Enter the information according to the following steps and click Save to apply the changes:

Caching Conditions

  1. Use the fields to define the URL that you want to cache.
    A URL can be an exact URL or a representation of a pattern of URLs using Regular Expressions (external link), which can be either case sensitive or case insensitive.

Usage Example

Example:

Exact URL
exct_url.png

This representation sets the Page Cache to cache the URL http://www.zend.com/index only.

 

URL pattern using Regular Expressions
regex_url.png

Note:

Using regular expressions consumes more time and CPU than using exact URLs.

This example sets the Page Cache to cache the following:

  • URL matches regex - Any URL that matches this pattern is cached.
  • Scheme - Only URLs that begin with 'http' are cached (the alternatives are 'https or 'either').
  • Host - The host name and port part (optional) of the URL. By using a regular expression, you can specify whether to cache URLs that begin (or do not begin) with "www".
    For example: (www\.)?
    - Indicates that the URL may or may not begin with 'www.\'.
  • Path - the path and query part of the URL.
    For example:
    /.* - Indicates that any string after the host name 'zend.com/articles/' is acceptable. To be precise, it represents 0 or more of any character.
  1. Click add_cache_conditions.png to create additional caching rules based on HTTP requests and session parameters.
    There is no limit to the amount of conditions that can be added.

Usage Example

Example:

How can we configure the system to use only cached content when a user is not logged in?

Usage Scenario: Websites (portals, news sites, forums...) that provide different content to premium users and non-registered users.

 

Assuming that the _SESSION 'username' parameter is used to store the name of a currently logged in user, create a rule based on this parameter, as follows:

 

condition_basic.png

 

This sets the Page Cache to cache content only when the _SESSION parameter 'username' is not set. Subsequently, users that do have the _SESSION 'username' set are presented with a live version of the page.

 

If necessary, we can extend this limit to include all users whose 'username' _SESSION parameter equals 'guest' as follows:

 

condition_advanced.png

Note:

Square brackets are not required for non-nested variables when referring to superglobal variables in caching conditions. If you do not use square brackets, the system adds them automatically after you click "Save".

 

This completes creating a Cache Rule.

Multiple Versions of Cached Pages

  1. Choose to create compressed cache copies.
    This option allows you to disable the creation of a gzip-compressed version of each cached page, as long as it is larger than 1KB. You should normally leave this option checked.
    By default, Zend Server creates a compressed version of each cached page and stores it alongside the original version. This compressed version is sent to browsers that support gzip compression (all modern browsers support this) instead of the uncompressed version. For typical HTML, XML or other text-based outputs, using the compressed version can save about 90% of the bandwidth and improve page load times. In addition, the compressed copy is saved in your cache so the CPU-intensive process of real-time compression is not required. However, if you are caching a PHP script that outputs binary data (for example JPEG or PNG images, ZIP or EXE files, PDFs, etc.) which cannot be further compressed, you should un-check this option to avoid redundant processing. For more information, see: Page Cache.

  2. Click add_param.png to create different cached copies according to specific values. This creates more than one version of the page in the cache, based on specific conditions.

Usage Example

Example:

The following example demonstrates how to create different copies of cached information, based on the _GET parameter 'language'.

cache_copy.png

This sets Caching to create a different copy for each different value of _GET 'language' (for the content that was cached based on the rules defined in steps 1 and 2).
This example demonstrates how to use Caching for multilingual pages that handle the same content in different languages.

Caching Duration

Set the cache duration in seconds. After that time, the cache is refreshed and a newer version is created.
For example, 600 seconds is ten minutes.

This completes the last step of creating a Cache Rule.

Scroll to the bottom of the page (Rule Management | Caching) and click "Save" to save the rule information and "Restart PHP" to activate the rule.

To edit a rule, go to Rule Management | Caching and click Edit next to the rule you want to edit.

To clear the information in the cache for a specific rule, go to Rule Management | Caching and click Clear next to the rule you want to edit.

Creating a Copy of a Rule

The following procedure describes how to create a new rule based on an already existing rule. This option can be used when a new rule with settings similar to an existing rule needs to be created such as for different URLs with the same caching requirements.

 

 

ref_icon.png

To copy a Cache rule:

  1. Go to Rule Management | Caching

  2. Double click on the rule name on which you want to base the new rule.

  3. Change the settings.

  4. Click "Save As"
    A new Dialog will open prompting you to enter a new name for the rule.

  5. Enter a name and click "Save".

A new rule will be added to Rule Management | Caching.

 

 

Related Links

Related Links:

Zend Page Cache - Configuration Directives

Zend Page Cache - PHP API

Caching

Edit Rule (Caching)

Page Cache

Regular Expression Reference Information - External Link