1

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

akond
  • 15,865
  • 4
  • 35
  • 55
raygo
  • 1,348
  • 5
  • 18
  • 40

2 Answers2

2

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.

mabe.berlin
  • 1,043
  • 7
  • 22
  • so do I have to use: $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($query)); or just do a $object = $table->fetchAll($query) ? Im confused with that part of the code – raygo Nov 16 '12 at 06:32
  • You already have `$cache` and `$paginator`. All you have to do is to remove the line `if( ($paginator = $cache->load('index' . $page)) === false ) {` (and the `$cache->save($paginator, 'index' . $page); }` ) and add the two described lines of code. – mabe.berlin Nov 16 '12 at 07:13
0

The paginator restored from the cache, does what it should do -- execute the query. And it does it every time you load it from the cache.

You should be saving the end result, not a paginator.

akond
  • 15,865
  • 4
  • 35
  • 55
  • so i should do a fetchall AND a paginator? I dont think i really understood. Very new at zend – raygo Nov 10 '12 at 18:07
  • You should cache the end result, whatever that is. If it is a page, it is a page. The paginator is not the end result as I understand. – akond Nov 10 '12 at 19:46
  • It is. The paginator is what I return, unless I'm not understanding what you mean – raygo Nov 10 '12 at 22:42
  • If you cache a paginator object then it will work exactly how you described it. It is a standoff. – akond Nov 10 '12 at 23:22
  • so do you have a recommendation about what should i do? – raygo Nov 10 '12 at 23:25