1

I am trying to store some values on cache the first time I load a page. This is the code I am using:

$cached_items = [
    'main_nav'   => $main_nav,
    'sub_nav'    => $sub_nav,
    'footer_nav' => $footer_nav,
    'view_as'    => $view_as,
];

$redisConnection = new Client('tcp://redis:6379');
$cache           = new RedisAdapter($redisConnection);
$menu            = $cache->getItem('mmi_menus');

if ($menu->isHit()) {
    return $menu->get();
} else {
    $menu->set($cached_items);
    $cache->save($menu);
}

This caching is being done from a non Symfony controller - let's say it's a standalone file.

First problem with the code above,

  • the else condition is reach out all the time and I think it should not be since values are stored. (check here)

Second problem, having this function in a Symfony controller:

public function GenerateMenuItemsAction()
{
    $redisConnection = new Client('tcp://redis:6379');
    $cache           = new RedisAdapter($redisConnection);
    $menu            = $cache->getItem('mmi_menus');

    if ($menu->isHit()) {
        return $this->render(
            'CommonBundle:Layout:menu.html.twig',
            ['menu' => $menu->get()]
        );
    }
}

$menu->isHit() is null so all the time I am getting this exception from Symfony:

An exception has been thrown during the rendering of a template ("The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?").

Update

I am not using any TTL afaik maybe somehow a default one is setup but this is how the section looks like on the config.yml:

framework:
    cache:
        app: cache.adapter.redis
        default_redis_provider: "redis://%redis_host%"
        pools:
            cache.pool1:
                public: true

What I am missing here? Any ideas?

4
  • Are you setting cache ttl? Commented Mar 6, 2017 at 17:12
  • @mdma I am not, see the update on the OP Commented Mar 6, 2017 at 19:05
  • somehow strange that you define redis cache in config.yml but instantiate it with 'new' in your classes. it looks to me you aren't using the configured one but a new one every time - and for those it could be that you don't have a correct ttl defined. Commented Mar 6, 2017 at 21:58
  • @LBA I think that could help I mean how I should instantiate the cache if I have configured at config.yml? So far the issue is connectivity between the containers (is a docker-compose stack) as far as symfony logs shows but your suggestion is interesting, could you leave me an example of recommended usage? Commented Mar 6, 2017 at 22:03

1 Answer 1

1

my config.yml looks like that:

framework:
  cache:
    system: cache.adapter.apcu
    default_redis_provider: redis://%redis_password%@%redis_host%:%redis_port%
    pools:
        redis_pool:
            adapter: cache.adapter.redis
            public: true
            default_lifetime: 0
            provider: cache.default_redis_provider

So I can easily (in my Controller) do something like:

$this->get('redis_pool')->getItem('myitem');

Or you can inject 'redis_pool' as an argument to a Service.

I don't need any 'new' or extra Connection information/configuration - anything is done in config.yml and available as a Service across the application.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.