我正在查看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
我收到了错误:
"主机"是一个无法识别的Doctrine缓存驱动程序。
我也尝试了以下配置:
doctrine_cache:
aliases:
my_cache: 'redis'
providers:
redis:
type: 'redis'
host: '%redis_host%'
port: '%redis_port%'
aliases:
- my_cache
出现同样的错误。另外在文档上还不是很清楚如何传递configaration选项。此外,如there所述,redis和predis本身都提供了捆绑包。
答案 0 :(得分:1)
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');
}
}