Studyon Minte9.com
ZF 1.0




Memcached



Implementing Memcached to improve your application's performance can be done fairly quickly without using
 Zend Framework. If you're just wanting Memcached, you don't necessarily need Zend Framework. On the 
other hand, if you're using Zend Framework but you're not using Memcached, this will help you get things 
going. But whatever your situation: If you're not already using Memcached, start tomorrow.


<?php
// configure caching backend / frontend strategy
$oBackend = new Zend_Cache_Backend_Memcached(
    array(
        'servers' => array( array(
            'host' => 'localhost', //'127.0.0.1',
            'port' => '11211'
        ) ),
        'compression' => true
) );
$oFrontend = new Zend_Cache_Core(
    array(
        'caching' => true,
        'cache_id_prefix' => 'myApp',
        'logging' => true,
        'logger'  => $oCacheLog,
        'write_control' => true,
        'automatic_serialization' => true,
        'ignore_user_abort' => true,
        'lifetime' => 60, // cache lifetime of 1 minute
    ) );

// build a caching object
$oCache = Zend_Cache::factory( $oFrontend, $oBackend );

// add in Registry to use anyware
Zend_Registry::set('cacheCore', $oCache);


// do caching
// frontendController.php (example)
$oCache = Zend_Registry::get('cacheCore');

$sCacheId = 'LargeDataSet';

if ( ! $oCache->test( $sCacheId ) ) {

    //cache miss, so we have to get the data the hard way
    $aDataSet = new stdClass;
    $aDataSet->a = 'test';
    $aDataSet->b = 123;

    $oCache->save( $aDataSet, $sCacheId );

    // saved cached
    echo "Save cache:";
    print_r($aDataSet);
    // display: Save cache: stdClass Object ( [a] => test [b] => 123 ) 

} else {

    //cache hit, load from memcache
    $aDataSet = $oCache->load( $sCacheId );

    // data from cache
    echo "From cache: ";
    print_r($aDataSet);
    // display: From cache: stdClass Object ( [a] => test [b] => 123 ) 
}


http://devzone.zend.com/article/4127
http://ajbrown.org/blog/2008/12/24/memcached-in-php-made-easy-with-zend-framework.html
http://zendframework.com/manual/1.11/en/zend.cache.introduction.html