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]"
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