protected function _getCacheDriver() { $driver = new RedisCache(); $driver->setRedis($this->_redis); $driver->setNamespace($this->_namespace); return $driver; }
/** * Find latest * * @param int $num How many posts to get * @return array */ public function findLatest($num) { # init predis client $predis = new RedisCache(); $predis->setRedis(new Client()); # define cache lifetime period as 1 hour in seconds $cache_lifetime = 86400; /*$qb = $this->getQueryBilder() ->orderBy('p.createdAt','desc') ->setMaxResults($num); return $qb->getQuery()->getResult();*/ return $this->getEntityManager()->createQuery('SELECT p FROM BeluhaBlogBundle:Post p ORDER BY p.createdAt DESC')->setMaxResults($num)->setResultCacheDriver($predis)->setResultCacheLifetime($cache_lifetime)->getResult(); }
public function ListTopFivePopularPostAction() { $em = $this->getDoctrine()->getManager(); $predis = new RedisCache(); $predis->setRedis(new Client()); $cache_lifetime = 3600; $posts = $em->getRepository('AppBundle:Post')->createQueryBuilder('p')->select('p')->getQuery()->setResultCacheDriver($predis)->setResultCacheLifetime($cache_lifetime)->getResult(); $postsWithNumComments = array(); foreach ($posts as $post) { $postComents = $em->getRepository('AppBundle:Comment')->createQueryBuilder('c')->select('c')->where('c.post = :post_id')->setParameter('post_id', $post->getId())->getQuery()->setResultCacheDriver($predis)->setResultCacheLifetime($cache_lifetime)->getResult(); array_push($postsWithNumComments, array('post' => $post, 'numComments' => count($postComents))); } usort($postsWithNumComments, function ($a, $b) { return $b['numComments'] - $a['numComments']; }); $numPosts = count($postsWithNumComments); if ($numPosts > 5) { for ($i = 5; $i < $numPosts; $i++) { unset($postsWithNumComments[$i]); } } return $this->render('RedisBundle:blog:top_posts.html.twig', array('posts' => $postsWithNumComments)); }