Search code examples
phpzend-frameworkcachingzend-paginatorzend-cache

Zend Cache with pagination is not working


I'm new with Zend Framework. I'm using Zend cache, I pretty much copied code from the documentation, here is my code:

 $frontendOptions = array(
                'lifetime' => 3600, // cache lifetime of 1 hour
                'automatic_serialization' => true
        );

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

        // getting a Zend_Cache_Core object
        $cache = Zend_Cache::factory('Core',
                'File',
                $frontendOptions,
                $backendOptions);


        // see if a cache already exists:
        if( ($paginator = $cache->load('index' . $page)) === false ) {


            $table = new self;
            $query = $table->select()->setIntegrityCheck(false);
            $query->from('feeds', array('feeds.blog_id', 'feeds.date_created', 'feeds.feed_id', 'feeds.post_content', 'feeds.post_link', 'feeds.post_title', 'feeds.post_thumb'));      

            $query->where('feeds.date_created <= NOW()');

            $query->order('feeds.date_created DESC');


            $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($query));
            $paginator->setItemCountPerPage(10);
            $paginator->setCurrentPageNumber($page);

            $cache->save($paginator, 'index' . $page);

        }

            return $paginator;

So after I go to the website that runs this query, I see the file being created, it's also a 19 or 20kb file, no matter which page im caching but the page is loading the same speed as before, also, if I update something in the database, I see the updated version, not the cache, I thought cache wouldnt show any updates for the lifetime of the cache. Any suggestions? Thanks


Solution

  • The paginator itself isn't the object you like to cache - you have to cache the results from db which will be done inside the paginator.

    To do that there is a method to attach your cache object to the paginator

    Zend_Paginator::setCache($cache);
    $paginator->setCacheEnabled(true);
    

    and the paginator will handle caching for you.