Today I taught myself how to use Zend_Cache and implemented it within 20 minutes. It’s super easy and very effective. Take a look at the code sample below and you’ll be up and running in no time.
Step 1: Setup the Cache
$frontendOptions = array(
‘lifetime’ => 180, // Cache for 3 minutes
‘automatic_serialization’ => true
$backendOptions = array(‘cache_dir’ => dirname(__FILE__) . ‘/cache/’);
$cache = Zend_Cache::factory(
Step 2: Use the Cache
$data = null;
if(!$data = $cache->load(‘data’))
$service = new Service(API_KEY);
$result = $service->generateReport();
$data = $service->getReport();
The page load time went from about 9 seconds to 0.5 seconds! 18x faster and it only took a few lines of code. Awesome.
My main motivation for caching the data ($data in the code example) was actually to reduce the load on the web service which provides the data. We have a good relationship with the company providing the service but there’s a good chance they would become annoyed if we hammered their system to get the exact same data over and over. The load time improvement was a good side effect, though!