2013-08-31 29 views
1

嗨,我想緩存查詢的結果。我已經:symfony2教條緩存查詢與時間內

$now = time(); 
    $lastMonth = 30*24*3600; 

    $period = $now-$lastMonth; 

    $qb=$this->_em->createQueryBuilder() 
      ->select('s') 
      ->addSelect('u') 
      ->addSelect('COUNT(s.id) AS suggestedCount') 
      ->from('WallBundle:Status', 's') 
      ->innerJoin('s.user', 'u') 
      ->where('s.time >= :period') 
      ->andWhere('s.suggested_status = true') 
      ->groupBy('s.user') 
      ->setParameter('period', $period) 
      ->orderBy('suggestedCount', 'DESC') 
      ->setMaxResults(10) 
      ; 

    $query=$qb->getQuery(); 
    $query->useResultCache(true,30800,'elite10'); 
    $query->useQueryCache(true); 

    return $query->getResult(); 

但它仍然不緩存。當我刪除

"->where('s.time >= :period')" 

->setParameter('period', $period) 

它的工作...所以...哪裏出了問題?

回答

1

您正在使用查詢緩存($query->useQueryCache(true);),但是您還正在構建一個查詢,該查詢使用每隔一毫秒更改一次的參數($period = $now-$lastMonth;)。因此沒有兩個查詢是相同的並且不能被緩存。如果您的需要降低,請將參數舍入爲x小時。