dsceme82487 2016-02-01 12:40
浏览 68
已采纳

集成Symfony2和Redis

I am trying to integrate Redis with my Symfony API, I found this bundle : RedisBundle

I installed it and configured so that I can cache Doctrine like so config.yml:

# Doctrine Configuration
doctrine:
    dbal:
        driver:   pdo_mysql
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

        metadata_cache_driver: redis
        # enable query caching
        query_cache_driver: redis

snc_redis:
# configure predis as client
clients:
    default:
        type: predis
        alias: default
        dsn: redis://localhost
    doctrine:
        type: predis
        alias: doctrine
        dsn: redis://localhost
# configure doctrine caching
doctrine:
    metadata_cache:
        client: doctrine
        entity_manager: default
        document_manager: default
    result_cache:
        client: doctrine
        entity_manager: [default]
    query_cache:
        client: doctrine
        entity_manager: default

In my FetchBookRepository I am trying to use RedisCache there:

<?php

namespace BooksApi\BookBundle\Repositories;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query\QueryException;

use Snc\RedisBundle\Doctrine\Cache\RedisCache;
use Predis\Client;

class FetchBookRepository
{
    /**
     * @var EntityManager
     */
    public $em;

    /**
     * @param EntityManager $entityManager
     */
    public function __construct(
        EntityManager $entityManager
    ){
        $this->em = $entityManager;
    }

    /**
     * @param $id
     * @return null|object
     * @throws QueryException
     */
    public function fetchBook($id)
    {

        $predis = new RedisCache();
        $predis->setRedis(new Client);
        $cache_lifetime= 3600;

        try {
            $book = $this->em->getRepository('BooksApiBookBundle:BooksEntity')
                ->find($id);

        } catch (\Exception $ex) {
            $this->em->close();
            throw new QueryException('003', 502);
        }

        return $book;
    }
}

I called the RedisCahce and Client classes but how do I now use it with my query..? I cant really find much on Google in regards to Symfony & Redis.

UPDATE:

When I use redis-cli and type in MONITOR i get this output:

1454337749.112055 [0 127.0.0.1:60435] "GET" "[BooksApi\\BookBundle\\Entity\\BooksEntity$CLASSMETADATA][1]"
  • 写回答

1条回答 默认 最新

  • donglin4636 2016-02-01 13:03
    关注

    Your Redis config looks OK. You are using Redis to cache Meatada (Doctrine collected about Entities mappings, etc) and Query (DQL to SQL).

    To use Redis as Cache for Results you must write custom Query and define that it is cacheable. Please follow the Doctrine manual http://docs.doctrine-project.org/en/latest/reference/caching.html#result-cache and this GitHub issue https://github.com/snc/SncRedisBundle/issues/77

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度