【问题标题】:Zend framework caching pagination results for different queriesZend 框架缓存不同查询的分页结果
【发布时间】:2010-07-06 02:36:00
【问题描述】:

使用 Zend Paginator 和分页器缓存可以正常工作,但所有内容都返回相同的缓存页面。 IE。我首先查看文章列表,当我查看类别时返回文章列表。如何告诉分页器我正在寻找哪个结果集?

另外,如何在不重新查询分页器的情况下清除分页结果。 IE。我更新了一篇新闻文章,因此需要清除分页。

谢谢

【问题讨论】:

    标签: php zend-framework caching


    【解决方案1】:

    Zend_Paginator 使用两种方法来定义缓存 ID:_getCacheId 和 _getCacheInternalId。第二个函数是根据两个参数计算缓存 ID:每页的项目数和适配器对象的特殊哈希。第一个函数(_getCacheId)是使用 _getCacheInternalId 和当前页面的结果计算缓存 ID。

    因此,如果您使用具有 3 个相同内部参数的两个不同的分页器对象:适配器、当前页码和每页的项目数,那么这两个对象的缓存 ID 将是相同的。

    所以我看到的唯一方法是定义您自己的从 Zend_Paginator 继承的分页器类,并重新定义这两个内部函数之一以向缓存 ID 添加盐。 像这样的:

    class My_Paginator extends Zend_Paginator {
    
        protected $_cacheSalt = '';
    
        public static function factory($data, $adapter = self::INTERNAL_ADAPTER, array $prefixPaths = null) {
        $paginator = parent::factory($data, $adapter, $prefixPaths);
        return new self($paginator->getAdapter());
    }
    
        public function setCacheSalt($salt) {
            $this->_cacheSalt = $salt;
            return $this;
        }
    
        public function getCacheSalt() {
            return $this->_cacheSalt;
        }
    
        protected function _getCacheId($page = null) {
            $cacheSalt = $this->getCacheSalt();
            if ($cacheSalt != '') {
                $cacheSalt = '_' . $cacheSalt;
            }
            return parent::_getCacheId($page) . $cacheSalt;
        }
    }
    
    $articlesPaginator = My_Paginator::factory($articlesSelect, 'DbSelect');
    $articlesPaginator->setCacheSalt('articles');
    
    $categoriesSelect = My_Paginator::factory($categoriesSelect, 'DbSelect');
    $articlesPaginator->setCacheSalt('categories');
    

    【讨论】:

    • 这似乎对我不起作用,部分原因是它正在运行父级中不存在的函数(静态类问题?).. Zend_Paginator::setCacheSalt() 不存在。无论如何,我可以使用公共变量来解决它。但是 _getCacheId 没有被调用? Zend 肯定考虑过这一点 - 为什么我不使用相同的 fetch 方法和每页计数的项目?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多