如何使用分页缓存数据?

时间:2013-12-25 04:50:38

标签: php pagination zend-framework2

我想将数据从db保存到缓存并使用分页查看chached数据。 我在模型中尝试这个代码:

public function fetchAll(Select $select = null)
{       
    if (null === $select)
        $select = new Select ();

    $select->from ( $this->table );
    $resultSet = $this->tableGateway->selectWith ( $select );
    $resultSet->buffer ();
    return $resultSet;
}

将此代码用于paginate并传递数据以查看:

use Zend\Db\Sql\Select;
use Zend\Paginator\Paginator;
use Zend\Paginator\Adapter\Iterator as paginatorIterator;

public function indexAction()
{
    $select = new Select ();

    $page = $this->params ()->fromRoute ( 'page' ) ? ( int ) $this->params ()->fromRoute ( 'page' ) : 1;
    $select->order ( 'my_table.'.$order_by . ' ' . $order_sort );

    $items = $myModel->fetchAll ( $select );
    $paginator = new Paginator ( new paginatorIterator ( $items ) );
    $paginator->setCurrentPageNumber ( $page );

    $view = new ViewModel (array (
        'page' => $page,
        'paginator' => $paginator,
    ) );

    $view->setTemplate ( 'path/to/phtml' );
    return $view;

}

怎么做?

1 个答案:

答案 0 :(得分:0)

通过为分页提供内置缓存功能,ZF2可以轻松实现。这是文档:http://framework.zend.com/manual/2.2/en/modules/zend.paginator.advanced.html#caching-features

我能够在大约5分钟内在我的一个项目中进行设置。