2.3. Zend_Cache_Core

2.3.1. Introduction

Zend_Cache_Core is a special frontend because it's actually the core of the module. All frontends inherit from this class. So most of methods and options described below are also available for other frontends.

2.3.2. Available options (for this frontend in Zend_Cache factory)

Table 2.1. Available options

Option Data Type Default Value Description
caching boolean true enable / disable caching (can be very usefull for the debug of cached scripts)
lifeTime int 3600 cache lifetime (in seconds), if set to null, the cache is valid forever.
logging boolean false if set to true, logging thow Zend_Log is activated (but the system is slower)
writeControl boolean true Enable / disable write control (the cache is read just after writing to detect corrupt entries), enabling writeControl 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)
automaticSerialization boolean false Enable / disable automatic serialization, it can be used to save directly datas which aren't strings (but it's slower)
automaticCleaningFactor int 0 Disable / Tune the automatic cleaning process (garbage collector) : 0 means no automatic cache cleaning, 1 means systematic cache cleaning and x (integer) > 1 means automatic cleaning randomly 1 times on x cache write.

2.3.3. Examples

A first example is given in the tutorial at the very beginning.

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

<?php  
     	    
// [...] // require, configuration and factory

$id = 'foo'; // cache id of "what we want to cache"

if (!($data = $cache->get($id))) {
	// cache missed
	
	$data = '';
	for ($i=0;$i<10000;$i++) {
		$data = $data . $i;
	}
	
    $cache->save($data);
    
} 

// [...] do something with $data (for example "echo $data;")
     	    
?>       

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

<?php  
     	    
// [...] // require, configuration and factory

$id1 = 'foo'; // cache id of block1
$id1 = 'foo'; // cache id of block2

// BLOCK1
if (!($data = $cache->get($id1))) {
	// cache missed
	
	$data = '';
	for ($i=0;$i<10000;$i++) {
		$data = $data . $i;
	}
	
    $cache->save($data);
    
} 
echo($data);

// NEVER CACHED BLOCK
echo('NEVER CACHED !');

// BLOCK2
if (!($data = $cache->get($id2))) {
	// cache missed
	
	$data = '';
	for ($i=0;$i<10000;$i++) {
		$data = $data . '!';
	}
	
    $cache->save($data);
    
} 
echo($data);


?>       

2.3.4. clean the cache

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

<?php 
     	    
// [...]
$cache->remove('idToRemove');
// [...]
    		
?>       

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

<?php 
     	    
// [...]
$cache->clean(Zend_Cache::CLEANING_MODE_ALL);
// [...]
    		
?>       

If you want to remove only outdated cache records :

<?php 
     	    
// [...]
$cache->clean(Zend_Cache::CLEANING_MODE_OLD);
// [...]
    		
?>       

[...]