4

Is there a way to caching resultsets in Zend_db? For example, I want to run a select query using Zend_db and want this query to be cached to be able to run it faster later.

Cagatay Gurturk
  • 7,186
  • 3
  • 34
  • 44

2 Answers2

4

My advice is that create a initialization method in Bootstrap.php with prefix "_init". for exaple :

/**
 * 
 * @return Zend_Cache_Manager 
 */
public function _initCache()
{
    $cacheManager = new Zend_Cache_Manager();
    $frontendOptions = array(
        'lifetime' => 7200, // cache lifetime of 2 hours
        'automatic_serialization' => true
    );
    $backendOptions = array(
        'cache_dir' => APPLICATION_PATH . '/cache/zend_cache'
    );
    $coreCache = Zend_Cache::factory(
                'Core', 
                'File', 
                $frontendOptions, 
                $backendOptions
            );
    $cacheManager->setCache('coreCache', $coreCache);
    $pageCache = Zend_Cache::factory(
            'Page', 
            'File', 
            $frontendOptions, 
            $backendOptions
    );
    $cacheManager->setCache('pageCache', $pageCache);

    Zend_Registry::set('cacheMan', $cacheManager);
    return $cacheManager;
}

By this way, you have created and injected your cache manager with the caches which you need in your app. Now you can use this cache object where you want to use. For instance, in your controller or where else :

/**
 *
 * @return boolean |SimplePie
 */
public function getDayPosts() 
{
    $cacheManager =  Zend_Registry::get('cacheMan');
    $cache = $cacheManager->getCache('coreCache');
    $cacheID = 'getDayPosts';

    if (false === ($blog = $cache->load($cacheID))) {
        $blog = Blog::find(array('order' => 'rand()', 'limit' => 1));
        $cache->save($blog, $cacheID);
    }
    // do what you want to do with the daya you fetched.
}
wise
  • 49
  • 1
  • 5
2

You can use Zend_Cache when you want to save result-sets.

Zend_Db doesn't do any result-set caching itself. It's left for you to do it an application-specific way, because the framework has no way of knowing which result-sets need to be cached for performance reasons, versus those that can't be cached because you need them to be absolutely current. Those are criteria only you as the application developer know.

Just googling for "zend_db cache results" the first match is this blog showing how to use a Zend_Cache object to save a db query result: Zend Framework:: Caching the database query results

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • 1
    And one way to keep your controller lean and clean is push all this caching business down into a repository object (designed to be both db-aware and cache-aware). – David Weinraub Feb 05 '12 at 03:55
  • @David Weinraub Now you know you have define "repository object" for us neophytes. :) At least toss us a link. – RockyFord Feb 05 '12 at 07:10
  • This can expand into a much broader discussion of modeling, mappers, db-access layers, repositories, services, and even dependency injection. But I view a repository as a place that "seems" to hold my data. Typically, my repo object will (1) expose a well-defined interface for accessing the data, and (2) accept as constructor params a db-adapter and a cache object. Internally, each data access method in the defined interface checks the cache first before hitting the db. Then the controller interacts only with the repository object. – David Weinraub Feb 05 '12 at 08:28
  • @David Weinraub Thank you, I think I get it (some at least). In this context I would put the data caching in a model layer where i perform queries or aggregate them (depending on complexity). I realize I'm being rather simplistic but I'm not yet a computer software engineer. – RockyFord Feb 05 '12 at 12:06
  • @RockyFord: For more reading see my answer to [Is MVC + Service Layer common in zend or PHP?](http://stackoverflow.com/questions/3744402/is-mvc-service-layer-common-in-zend-or-php/3744448#3744448) or also [Domain-Driven Design Quickly](http://www.infoq.com/minibooks/domain-driven-design-quickly). – Bill Karwin Feb 05 '12 at 16:50