Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
290 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 AppBundleService;

use DoctrineCommonCacheCache;

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: AppBundleServiceRedisService
    arguments: ["@doctrine_cache.providers.redis_cache"]

And you can use it everywhere. Sample;

<?php

namespace AppBundleController;

use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;

/**
* @package AppBundleController
* @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');
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...