2017-10-30 8 views
0

APi 결과를 캐시하는 데 사용 방법을 알아 내기 위해 documentation을 찾고있었습니다.DoctrineCacheBundle : redis와 함께 사용하기위한 설명서를 밑줄 수 없습니다.

redis 또는 predis와 함께 작동하도록 구성을 설정하는 방법을 이해할 수 없습니다.

"host" is an unrecognized Doctrine cache driver.

또한

I : 나는 오류를 가지고

php bin/console debug:container doctrine 

:

나는 다음과 같은 사기꾼 형상 시도 :

doctrine_cache: 
    aliases: 
     my_cache: 'redis' 

    providers: 
     redis: 
      host: '%redis_host%' 
      port: '%redis_port%' 
      aliases: 
      - my_cache 

을하지만 tryint이었다로 내 용기를 디버깅 다음 구성을 사용해 보았습니다.

doctrine_cache: 
    aliases: 
     my_cache: 'redis' 

    providers: 
     redis: 
      type: 'redis' 
      host: '%redis_host%' 
      port: '%redis_port%' 
      aliases: 
      - my_cache 

매우 동일한 오류. 또한 설명서에 구성 옵션을 전달하는 방법이 명확하지 않습니다. 또한 there과 같이 redis와 predis 모두 기본적으로 번들과 함께 제공됩니다.

+0

코드 또는 실제 코드를 붙여 넣은 결과인지는 모르겠지만 구성에서 들여 쓰기를 위해 2 공백과 4 공백을 혼합하고 있습니다. – malarzm

답변

1

redis에 대한 첫 번째 설정 구성.

<?php 

namespace AppBundle\Service; 

use Doctrine\Common\Cache\Cache; 

class RedisService 
{ 
    private $cache; 
    /** 
    * RedisService constructor. 
    * @param Cache $cache 
    */ 
    public function __construct(Cache $cache) 
    { 
     $this->cache = $cache; 
    } 

    public function insert($key, $value, $lifetime = null) 
    { 
     return $this->cache->save($key, $value, $lifetime); 
    } 

    public function get($key) 
    { 
     return $this->cache->fetch($key); 
    } 

    public function delete($key) 
    { 
     return $this->cache->delete($key); 
    } 

} 

이 라인

redis_service: 
    class: AppBundle\Service\RedisService 
    arguments: ["@doctrine_cache.providers.redis_cache"] 

을 services.yml 추가하고 사용할 수 있습니다 : 나는 RedisService을 만들어

cache_provider: array_cache 
redis_cache_host: localhost 
redis_cache_port: 6379 
redis_cache_keyspace: [your_keyspace] 

:

doctrine_cache: 
    aliases: 
     cache: "%cache_provider%" 
    providers: 
     redis_cache: 
      namespace: "%redis_cache_keyspace%" 
      redis: 
       host: "%redis_cache_host%" 
       port: "%redis_cache_port%" 
     array_cache: 
      type: array 

그런 다음, parameters.yml 설정 그것 사방에. 견본;

<?php 

namespace AppBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\Routing\Annotation\Route; 

/** 
* @package AppBundle\Controller 
* @Route("/") 
*/ 
class RedisApiController extends Controller 
{ 

    /** 
    * @return object 
    */ 
    public function getRedisService() 
    { 
     return $this->get('redis.service'); 
    } 

    /** 
    * @Route("/insert", name="insert") 
    */ 
    public function insertAction(){ 
     $this->getRedisService()->insert('website', 'http://mertblog.net', 3600); 
    } 

    /** 
    * @Route("/get", name="get") 
    */ 
    public function getAction(){ 
     $webSite = $this->getRedisService()->get('website'); 
    } 

    /** 
    * @Route("/delete", name="delete") 
    */ 
    public function deleteAction(){ 
     $this->getRedisService()->delete('website'); 
    } 
}