php - DoctrineCacheBundle : I cannot undersatand the documentation for using it with redis

标签 php symfony redis symfony-3.3 predis

当我在寻找 documentation为了弄清楚如何使用它来缓存 APi 结果。

我不明白如何设置配置以使其与 redis 或 predis 一起工作。

我尝试了以下配置:

doctrine_cache:
    aliases:
      my_cache:  'redis'

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

但是当我尝试用以下方法调试我的容器时:

php bin/console debug:container 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

同样的错误。同样在文档中也不是很清楚如何传递配置选项。此外如前所述there redis 和 predis 都是原生提供的。

最佳答案

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:

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

我创建了一个 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

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

而且您可以在任何地方使用它。示例;

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

关于php - DoctrineCacheBundle : I cannot undersatand the documentation for using it with redis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47013054/

相关文章:

c# - StackExchange.Redis 与 Azure Redis 速度慢得无法使用或引发超时错误

php, mysql, arrays - 如何获取行名

javascript - crud ajax jquery symfony3 无法工作

php - 如何处理包含 500 多个项目的 Symfony 表单集合

php - 在 Twig 模板中呈现自定义 formType 选项

redis - 如何在 ElasticBeanstalk 上安装和配置 Redis

node.js - hdel 里面的 hget block nodejs redis

php - 调整图像大小时它不保持比例?

php - 如何在 codeigniter 中重定向到我指定的页面

php - 如何判断一笔交易是成功还是失败?