我正在尝试将Redis与我的Symfony API集成,我找到了这个包:RedisBundle
我已安装并配置,以便我可以缓存Doctrine,就像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
在我的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类,但我现在如何在查询中使用它?对于Symfony&amp ;;我在Google上找不到多少Redis的。
更新:
当我使用redis-cli并输入MONITOR时,我得到这个输出:
1454337749.112055 [0 127.0.0.1:60435] "GET" "[BooksApi\\BookBundle\\Entity\\BooksEntity$CLASSMETADATA][1]"
答案 0 :(得分:1)
您的Redis配置看起来不错。 您正在使用Redis缓存Meatada(收集有关实体映射的Doctrine等)和Query(DQL to SQL)。
要将Redis用作结果缓存,您必须编写自定义查询并定义它是可缓存的。请按照主题说明http://docs.doctrine-project.org/en/latest/reference/caching.html#result-cache和此GitHub问题https://github.com/snc/SncRedisBundle/issues/77
进行操作