PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Zend Framework 101: Zend_Cache

Tagging Cache Records

Earlier in this article I stated that the cache should never serve stale content. The way to avoid this is to clean cache records when they become stale (even if their expiry date hasn't yet been reached).

One way of doing this is to clear the entire cache (by calling $cache->clean() with no arguments), however this will mean all data in the cache is removed. This potentially means a lot of unnecessary processing power will be devoted to re-populating the cache.

The preferred solution for this is to clear only the related records. To do this we first need to be able to identify the related records. This is achieved by tagging records when they are saved to the cache. A tag is simply a string used to identify the record. You can save any number of tags with a cache record.

As we saw earlier in this article, a record is saved to the cache using the save() method. The first argument is the data to save, while the second argument is the cache ID (which can be left blank if called after load()). The third argument to save() is an array of tags to save with the data.

Note: You can specify the tags without having to specify the cache ID - just pass null as the second argument to save().

Saving a Cache Record With Tags

The following code demonstrates saving the cache record with two tags. We will make use of these tags shortly.

Listing 7 Tagging cache records (listing-7.php)
<?php
    require_once('Zend/Cache.php');
 
    $frontendOptions = array(
        'automatic_serialization' => true
    );
 
    $backendOptions = array(
        'cache_dir' => './cache'
    );
 
    $cache = Zend_Cache::factory(
        'Core',
        'File',
        $frontendOptions,
        $backendOptions
    );
 
    $tags = array(
        'theFirstTag',
        'theSecondTag'
    );
 
    $cache->save(
        'My Cache Data',
        'someCacheId',
        $tags
    );
?>

Clearing Records Based on Tag

When calling the clear() method on the cache, you can specify that only records with certain tags are deleted. This allows to target your cache deletes, rather than clearing the entire cache.

When clearing by tag you must specify one or more tags to clear by. You must also decide if you want records with any of the tags to be removed, or if records must have all of the tags to be removed.

If records must have every single tag specified (in the second argument), pass Zend_Cache::CLEANING_MODE_MATCHING_TAG as the first argument to clean().

If a record need only have one of the tags specifeid in the second argument, pass Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG as the first argument.

The following listing would remove the record created in the previous listing.

Listing 8 Clearing records by tag (listing-8.php)
<?php
    // initialize the cache
 
    // clear records tagged "theFirstTag"
    $cache->clear(
        Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
        array('theFirstTag')
    );
?>

Real-World Example of Tagging

To demonstrate how to use tagging in practical terms, let's look at how Recite CMS uses Zend_Cache for displaying news articles.

There are two ways of displaying news articles: a list of news articles, and the details of a single news article.

When the list of articles is requested, the page is cached with a tag of news_articles.

When the article details page is requested, the page is cached with a tag of news_article_{$id} (that is, it uses the database ID of the article). For instance, if the article being viewed has an id of 123, the tag is news_article_123.

When article 123 is updated (or deleted) using the Control Panel, the following code is executed:

Listing 9 Clearing by tag in Recite CMS (listing-9.php)
<?php
    $cache->clear(
        Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
        array('news_articles', 'news_article_123')
    );
?>

Likewise, when an article is created, the cache is cleared by tag news_articles.

While this may appear obvious at first, consider exactly what is happening:

  • When any article is created, edited or deleted, the list of news articles will no longer to be cached.
  • When an article is created, edited or deleted, only its details page will be removed from the cache.

Here you have the best of both worlds - when an article is updated, only that article's details page is cleared from cache. Every other article's details page remains in the cache!

In This Article