Skip to end of metadata
Go to start of metadata

<p>Zend_Cache interacts with a variety of common caching backends, including APC and Memcached, to make caching both flexible and painless.</p>

<p>But when should you use caching? The fact is that almost every website would benefit from increased caching. Let's say an affiliate marketing website generates a set of summary statistical information for its affiliate partners, and the information is presented on each page of the website. The system administration notices that the database load has spiked to unacceptable levels since the developers implemented the summary display on the website. Though the method that calculates the data is only called once per request, it produces too much load on the server, and it is decided that the affiliate partners would be happy if the data were only updated every five minutes. But how do the developers go about doing that?</p>

<p>Here we use the file caching mechanism to store the statistics in the file system cache for a lifetime of 5 minutes:</p>
<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<?php
require_once 'Zend/Cache.php';

$frontendOptions = array(
'lifeTime' => 300 // cache lifetime of 5 minutes
);

$backendOptions = array(
'cacheDir' => '/tmp/' // where to put the cache files
);

// Create an instance of Zend_Cache_Core
$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);

// ID of cached data
$id = 'stats';

// If the data do not exist in the cache
if (!$stats = $cache->load($id)) {
// Calculate the statistics and save to the cache
$stats = $userModel->getUserStats();
$cache->save($stats);
}

// Render $stats as appropriate
?>
]]></ac:plain-text-body></ac:macro>

<p>It's worth taking the time to explore the <a href="http://framework.zend.com/manual/en/zend.cache.html">documentation</a> and carefully pick the front-end and back-end that best suit your needs. If you do choose to use the file back-end, give it its own directory to work in, and consider the hashed_directory_level option. You may notice significant speed increases in a situation where there are many files present.</p>

Labels:
zend_cache zend_cache Delete
example example Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.