duanmiaosi0150 2017-10-30 10:29
浏览 182
已采纳

DoctrineCacheBundle:我不能理解使用redis的文档

As I was looking the documentation in order to figure out how to use it in order to Cache APi results.

I cannot understand how to setup the configuration in order to make it work with either redis or predis.

I tried the following con figuration:

doctrine_cache:
    aliases:
      my_cache:  'redis'

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

But as I was tryint to debug my container with:

php bin/console debug:container doctrine

I got the error:

"host" is an unrecognized Doctrine cache driver.

I also tried the following configuration:

doctrine_cache:
    aliases:
      my_cache:  'redis'

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

With the very same error. Also on documentation is not very clear how to pass the configaration option. Futhermore as said there both redis and predis are natively provided with the bundle.

  • 写回答

1条回答 默认 最新

  • dongtuoji5396 2017-10-30 11:03
    关注

    First setup configuration for redis.

    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
    

    Then, set parameters.yml:

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

    I created a RedisService:

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

    Add this lines services.yml

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

    And you can use it everywhere. Sample;

    <?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');
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路