Storing and Retrieving Data in Cache with RedisAdapter and Symfony

Upon discovering that Symfony 1.4 does not take user session into account when caching templates, I realized that this presents a problem for several of my templates that rely on user session for certain sections of code. As a solution, one could opt to specify specific pages to be added to the cache through the filter.


Currently, my aim is to cache certain values upon the page’s initial loading. For this purpose, I have implemented the subsequent code.

$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 {

The caching process is being carried out by a file that is not associated with
Symfony controller
. To clarify, it is a separate and independent file.

First problem with the code above,

  • Continuous accessibility to the


    condition seems unnecessary as the values are already stored. (refer here)

The other issue is related to the controller identified as
function in a symfony

public function GenerateMenuItemsAction()
    $redisConnection = new Client('tcp://redis:6379');
    $cache           = new RedisAdapter($redisConnection);
    $menu            = $cache->getItem('mmi_menus');
    if ($menu->isHit()) {
        return $this->render(
            ['menu' => $menu->get()]

The absence of


constantly generates an exception from Symfony.

an exception has been thrown during the rendering of a template
error message appears, indicating a missing return statement in the controller code. The
controller must return a response
is not specified, leading to this error.


As far as I know, I am not utilizing any TTL. However, it is possible that a default TTL has been set up. Here is how the section appears on



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

What I am missing here? Any ideas?


my config.yml looks like that:

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

This enables me to effortlessly perform a task in my Controller by using the following method:


Alternatively, a Service can be passed ‘redis_pool’ as a parameter during injection.

I have no requirement for additional Connection information or configuration since everything is configured in the config.yml file and is accessible as a Service throughout the application.

Frequently Asked Questions

Posted in Php