【问题标题】:Integrating Symfony2 and Redis集成 Symfony2 和 Redis
【发布时间】:2016-02-01 12:40:45
【问题描述】:

我正在尝试将 Redis 与我的 Symfony API 集成,我发现了这个包:RedisBundle

我安装并配置了它,这样我就可以像 config.yml 一样缓存 Doctrine:

# 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

在我的 FetchBookRepository 中,我尝试在那里使用 RedisCache:

<?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;
    }
}

我调用了 RedisCahce 和 Client 类,但我现在如何在查询中使用它......?我在 Google 上找不到太多关于 Symfony 和 Redis 的信息。

更新:

当我使用 redis-cli 并输入 MONITOR 时,我得到以下输出:

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

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    您的 Redis 配置看起来不错。 您正在使用 Redis 缓存 Meatada(收集的关于实体映射等的学说)和查询(DQL 到 SQL)。

    要将 Redis 用作结果缓存,您必须编写自定义查询并定义它是可缓存的。请遵循 Doctrine 手册 http://docs.doctrine-project.org/en/latest/reference/caching.html#result-cache 和这个 GitHub 问题 https://github.com/snc/SncRedisBundle/issues/77

    【讨论】:

    • 更多,您可以通过“redis-cli”连接到Redis服务器并输入“MONITOR”命令来监控Redis中发生的事情。它将列出所有数据库上 Redis 服务器上的所有活动。您可以在开发环境中使用它,但切勿在生产环境中使用。
    • 感谢您的时间和建议,我将研究 ResultCaching,如果您查看我编辑的问题,eacly 意味着我得到的 CLI 输出是什么意思..?谢谢
    • 这是对位于数据库 #0 中的“[BooksApi\\BookBundle\\Entity\\BooksEntity$CLASSMETADATA][1] 键下的 BooksApi\BookBundle\Entity\BooksEntity 的 Metadata 缓存的请求"
    • 我听从了你的建议,并设法让它比你更有效。几乎没有其他问题出现,但将在此线程之外提出这些问题。
    猜你喜欢
    • 2013-01-09
    • 2019-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    相关资源
    最近更新 更多