1

我是 Zend 框架的新手。我正在使用 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;

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

4

2 回答 2

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.

于 2012-11-15T15:39:38.433 回答
0

从缓存中恢复的分页器,做它应该做的——执行查询。每次从缓存中加载它时它都会执行此操作。

您应该保存最终结果,而不是分页器。

于 2012-11-10T15:47:31.897 回答