The Theory of Caching

There are three key concepts in Zend_Cache. One is the unique identifier (a string) that is used to identify cache records. The second one is the ‘lifetime’ directive as seen in the examples; it defines for how long the cached resource is considered ‘fresh’. The third key concept is conditional execution so that parts of your code can be skipped entirely, boosting performance. The main frontend function (e.g. Zend_Cache_Core::get()) is always designed to return FALSE for a cache miss if that makes sense for the nature of a frontend. That enables end-users to wrap parts of the code they would like to cache (and skip) in if(){ ... } statements where the condition is a Zend_Cache method itself. On the end if these blocks you must save what you’ve generated, however (e.g. Zend_Cache_Core::save()).

Note

The conditional execution design of your generating code is not necessary in some frontends (Function, for an example) when the whole logic is implemented inside the frontend.

Note

‘Cache hit’ is a term for a condition when a cache record is found, is valid and is ‘fresh’ (in other words hasn’t expired yet). ‘Cache miss’ is everything else. When a cache miss happens, you must generate your data (as you would normally do) and have it cached. When you have a cache hit, on the other hand, the backend automatically fetches the record from cache transparently.

The Zend_Cache Factory Method

A good way to build a usable instance of a Zend_Cache Frontend is given in the following example :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// We choose a backend (for example 'File' or 'Sqlite'...)
$backendName = '[...]';

// We choose a frontend (for example 'Core', 'Output', 'Page'...)
$frontendName = '[...]';

// We set an array of options for the chosen frontend
$frontendOptions = array([...]);

// We set an array of options for the chosen backend
$backendOptions = array([...]);

// We create an instance of Zend_Cache
// (of course, the two last arguments are optional)
$cache = Zend_Cache::factory($frontendName,
                             $backendName,
                             $frontendOptions,
                             $backendOptions);

In the following examples we will assume that the $cache variable holds a valid, instantiated frontend as shown and that you understand how to pass parameters to your chosen backends.

Note

Always use Zend_Cache::factory() to get frontend instances. Instantiating frontends and backends yourself will not work as expected.

Tagging Records

Tags are a way to categorize cache records. When you save a cache with the save() method, you can set an array of tags to apply for this record. Then you will be able to clean all cache records tagged with a given tag (or tags):

1
$cache->save($huge_data, 'myUniqueID', array('tagA', 'tagB', 'tagC'));

Note

note than the save() method accepts an optional fourth argument: $specificLifetime (if != FALSE, it sets a specific lifetime for this particular cache record)

Cleaning the Cache

To remove or invalidate in particular cache id, you can use the remove() method :

1
$cache->remove('idToRemove');

To remove or invalidate several cache ids in one operation, you can use the clean() method. For example to remove all cache records :

1
2
3
4
5
// clean all records
$cache->clean(Zend_Cache::CLEANING_MODE_ALL);

// clean only outdated
$cache->clean(Zend_Cache::CLEANING_MODE_OLD);

If you want to remove cache entries matching the tags ‘tagA’ and ‘tagC’:

1
2
3
4
$cache->clean(
    Zend_Cache::CLEANING_MODE_MATCHING_TAG,
    array('tagA', 'tagC')
);

If you want to remove cache entries not matching the tags ‘tagA’ or ‘tagC’:

1
2
3
4
$cache->clean(
    Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG,
    array('tagA', 'tagC')
);

If you want to remove cache entries matching the tags ‘tagA’ or ‘tagC’:

1
2
3
4
$cache->clean(
    Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
    array('tagA', 'tagC')
);

Available cleaning modes are: CLEANING_MODE_ALL, CLEANING_MODE_OLD, CLEANING_MODE_MATCHING_TAG, CLEANING_MODE_NOT_MATCHING_TAG and CLEANING_MODE_MATCHING_ANY_TAG. The latter are, as their names suggest, combined with an array of tags in cleaning operations.

Project Versions

Table Of Contents

Previous topic

Zend\Barcode Renderers

Next topic

Introduction

This Page

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Go to The Theory of Caching on GitHub.
  2. Edit file contents using GitHub's text editor in your web browser
  3. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  4. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  5. Your changes are now queued for review under project's Pull requests tab on GitHub.