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_Cache_Core

Introduction

Zend_Cache_Core is a special frontend because it is the core of the module. It is a generic cache frontend and is extended by other classes.

Note

All frontends inherit from Zend_Cache_Core so that its methods and options (described below) would also be available in other frontends, therefore they won't be documented there.

Available options

These options are passed to the factory method as demonstrated in previous examples.

Table 20. Core Frontend Options

Option Data Type Default Value Description
caching Boolean TRUE enable / disable caching (can be very useful for the debug of cached scripts)
cache_id_prefix String NULL A prefix for all cache ids, if set to NULL, no cache id prefix will be used. The cache id prefix essentially creates a namespace in the cache, allowing multiple applications or websites to use a shared cache. Each application or website can use a different cache id prefix so specific cache ids can be used more than once.
lifetime Integer 3600 cache lifetime (in seconds), if set to NULL, the cache is valid forever.
logging Boolean FALSE if set to TRUE, logging through Zend_Log is activated (but the system is slower)
write_control Boolean TRUE Enable / disable write control (the cache is read just after writing to detect corrupt entries), enabling write_control will lightly slow the cache writing but not the cache reading (it can detect some corrupt cache files but it's not a perfect control)
automatic_serialization Boolean FALSE Enable / disable automatic serialization, it can be used to save directly datas which aren't strings (but it's slower)
automatic_cleaning_factor Integer 10 Disable / Tune the automatic cleaning process (garbage collector): 0 means no automatic cache cleaning, 1 means systematic cache cleaning and x > 1 means automatic random cleaning 1 times in x write operations.
ignore_user_abort Boolean FALSE if set to TRUE, the core will set the ignore_user_abort PHP flag inside the save() method to avoid cache corruptions in some cases

Examples

An example is given in the manual at the very beginning.

If you store only strings into cache (because with "automatic_serialization" option, it's possible to store some booleans), you can use a more compact construction like:

<?php
// we assume you already have $cache

$id 'myBigLoop'// cache id of "what we want to cache"

if ( ($data $cache->load($id)) === false ) {
    
// cache miss

    
$data '';
    for (
$i 0$i 10000$i++) {
        
$data $data $i;
    }

    
$cache->save($data);

}

// [...] do something with $data (echo it, pass it on etc.)

If you want to cache multiple blocks or data instances, the idea is the same:

<?php
// make sure you use unique identifiers:
$id1 'foo';
$id2 'bar';

// block 1
if ( ($data $cache->load($id1)) === false ) {
    
// cache missed

    
$data '';
    for (
$i=0;$i<10000;$i++) {
        
$data $data $i;
    }

    
$cache->save($data);

}
echo(
$data);

// this isn't affected by caching
echo('NEVER CACHED! ');

// block 2
if ( ($data $cache->load($id2)) === false ) {
    
// cache missed

    
$data '';
    for (
$i=0;$i<10000;$i++) {
        
$data $data '!';
    }

    
$cache->save($data);

}
echo(
$data);

If you want to cache special values (boolean with "automatic_serialization" option) or empty strings you can't use the compact construction given above. You have to test formally the cache record.

<?php
// the compact construction
// (not good if you cache empty strings and/or booleans)
if ( ($data $cache->load($id)) === false ) {

    
// cache missed

    // [...] we make $data

    
$cache->save($data);

}

// we do something with $data

// [...]

// the complete construction (works in any case)
if (!($cache->test($id))) {

    
// cache missed

    // [...] we make $data

    
$cache->save($data);

} else {

    
// cache hit

    
$data $cache->load($id);

}

// we do something with $data

Zend Framework