symfony2缓存查询结果

时间:2016-01-12 12:21:54

标签: php postgresql symfony caching

我想缓存我的查询结果,因为我做了很多查询,但它每小时只更改一次。因为它是一个将用于这么多人的应用程序,我想查询这个结果,但调试symfony栏上的查询字段每个都显示所有查询。 这是我的代码。

配置文件:

orm:
    auto_generate_proxy_classes: "%kernel.debug%"
    naming_strategy: doctrine.orm.naming_strategy.underscore
    auto_mapping: true
    default_entity_manager: default
    query_cache_driver: apc
    metadata_cache_driver: apc
    query_cache_driver: apc
    result_cache_driver: apc

    dql:
        datetime_functions:
            date:           Oro\ORM\Query\AST\Functions\SimpleFunction
            time:           Oro\ORM\Query\AST\Functions\SimpleFunction
            timestamp:      Oro\ORM\Query\AST\Functions\SimpleFunction
            convert_tz:     Oro\ORM\Query\AST\Functions\DateTime\ConvertTz
        numeric_functions:
            timestampdiff:  Oro\ORM\Query\AST\Functions\Numeric\TimestampDiff
            dayofyear:      Oro\ORM\Query\AST\Functions\SimpleFunction
            dayofmonth:     Oro\ORM\Query\AST\Functions\SimpleFunction
            dayofweek:      Oro\ORM\Query\AST\Functions\SimpleFunction
            week:           Oro\ORM\Query\AST\Functions\SimpleFunction
            day:            Oro\ORM\Query\AST\Functions\SimpleFunction
            hour:           Oro\ORM\Query\AST\Functions\SimpleFunction
            minute:         Oro\ORM\Query\AST\Functions\SimpleFunction
            month:          Oro\ORM\Query\AST\Functions\SimpleFunction
            quarter:        Oro\ORM\Query\AST\Functions\SimpleFunction
            second:         Oro\ORM\Query\AST\Functions\SimpleFunction
            year:           Oro\ORM\Query\AST\Functions\SimpleFunction
            sign:           Oro\ORM\Query\AST\Functions\Numeric\Sign
            pow:            Oro\ORM\Query\AST\Functions\Numeric\Pow
        string_functions:
            group_concat:   Oro\ORM\Query\AST\Functions\String\GroupConcat
            concat_ws:      Oro\ORM\Query\AST\Functions\String\ConcatWs
            cast:           Oro\ORM\Query\AST\Functions\Cast

这是我要缓存的查询:

public function answerBySiteWhereHour($voteIndicator,$site,$hour,$day=null){
    if($day==null){
      $day = new \DateTime();
    }

    $em = $this->getEntityManager();
    $site= $em->getRepository('AppBundle:Site')->findOneBySiteId($site);
    $repository = $em
        ->getRepository('AppBundle:Answer')
        ->createQueryBuilder('a')
        ->select('a as answer, COUNT(vote.id) as voteN')
        ->leftJoin('a.vote','vote', 'WITH','vote.voteIndicator= :voteIndicator AND DATE(vote.date) = DATE( :today) AND HOUR(vote.date) =  :hour')
        ->leftJoin('vote.site','site','WITH','site.id = :site')
        ->setParameter('site',$site->getId())
        ->groupBy('a')
        ->orderBy('a.id','ASC')
        ->setParameter('today', $day)
        ->setParameter('voteIndicator', $voteIndicator)
        ->setParameter('hour', $hour)
    ;
    $query = $repository->getQuery()
        ->useQueryCache(true)
        ->useResultCache(true,3600);

    return $query->getResult();
}

public function answerSatisfactionBySiteByHour($site, $day=null){
    $result = [];
    for($i=1; $i<=24; $i++){
        $result [$i] =array(1=>$this->answerBySiteWhereHour(1,$site,$i,$day),2=>$this->answerBySiteWhereHour(2,$site,$i,$day),3=>$this->answerBySiteWhereHour(3,$site,$i,$day));
    }
    return $result;
}

感谢您的帮助,问候。

0 个答案:

没有答案