Symfony KnpPaginatorBundle和二级缓存

时间:2019-01-19 00:38:35

标签: symfony caching doctrine-orm second-level-cache

项目中启用了二级缓存,但是我知道如何缓存从分页器获取的数据?

Config.yml

...
    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        second_level_cache:
            enabled: true
            region_lifetime: 3600 # 1 hour
...

Controller

    public function showAction(Request $request) {
        $em    = $this->get('doctrine.orm.entity_manager');
        $dql   = "
                  SELECT c FROM AppBundle:City c
                  ";


        $query = $em->createQuery($dql);

        $paginator  = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
            $query, /* query NOT result */
            $request->query->getInt('page', 1)/*page number*/,
            25/*limit per page*/
        );



        return $this->render('city/index.html.twig', [
            'pagination' => $pagination,

        ]);
    }

Entity

...
/**
 * City
 *
 * @ORM\Table(name="cities")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\CityRepository")
 *
 * @Cache("READ_ONLY")
 *
 */
class City
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
...

0 个答案:

没有答案