简体   繁体   English

带分页的 Zend Cache 不起作用

[英]Zend Cache with pagination is not working

I'm new with Zend Framework.我是 Zend 框架的新手。 I'm using Zend cache, I pretty much copied code from the documentation, here is my code:我正在使用 Zend 缓存,我几乎从文档中复制了代码,这是我的代码:

 $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.因此,在我转到运行此查询的网站后,我看到正在创建的文件,它也是一个 19 或 20kb 的文件,无论我缓存哪个页面,但该页面的加载速度与以前相同,如果我更新某些内容在数据库中,我看到了更新的版本,而不是缓存,我认为缓存在缓存的生命周期内不会显示任何更新。 Any suggestions?有什么建议? Thanks谢谢

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.分页器本身不是您喜欢缓存的对象 - 您必须缓存来自 db 的结果,这将在分页器内完成。

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.分页器将为您处理缓存。

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.您应该保存最终结果,而不是分页器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM