DoctrineCacheBundle:我不能低估将它与redis一起使用的文档 [英] DoctrineCacheBundle: I cannot undersatand the documentation for using it with redis

查看:118
本文介绍了DoctrineCacheBundle:我不能低估将它与redis一起使用的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在查看文档以便弄清楚如何使用它来缓存APi结果.

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

我不明白如何设置配置以使其与Redis或predis一起使用.

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

我尝试了以下配置:

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

我得到了错误:

主机"是无法识别的Doctrine缓存驱动程序.

"host" is an unrecognized Doctrine cache driver.

我还尝试了以下配置:

doctrine_cache:
    aliases:
      my_cache:  'redis'

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

具有相同的错误.另外在文档上还不太清楚如何传递配置选项.此外,那里捆绑包本身提供了redis和predis

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.

推荐答案

redis的首次设置配置.

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

然后,设置parameters.yml:

Then, set parameters.yml:

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

我创建了一个RedisService:

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

}

添加以下行services.yml

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

这篇关于DoctrineCacheBundle:我不能低估将它与redis一起使用的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆