RSS Feed
^__^

Symfony Cache Component with SncRedisBundle

,

Since version 3.1 there is a Cache component with a PSR-6 implementation integrated in Symfony.
So we do not need to add a 3rd party lib as Doctrine Cache anymore when we need caching these days.

Doctrine Cache does the job but by implementing a standard the Symfony Cache seems to have another advantage besides its delievered anyway. So i checked it out.

Documentation is still a bit sparse and the best resource so far is this blogpost:
https://symfony.com/blog/new-in-symfony-3-1-cache-component

I wont go into detail on how you use the Cache but rather how you integrate it with Redis, which is my favourite Cache backend.

You can create a Cache service via the FrameworkBundle in your config.yml and you can also create a default Redis client to use with your Cache service.

framework:
    default_locale: de
    secret: ThisTokenIsNotSoSecretChangeIt
    assets: ~
    templating:
        engines: ['twig']
    cache:
	default_redis_provider: "redis://%cache.redis_host%:%cache.redis_port%/%cache:redis_db%"
        pools:
           sf_cache:
             adapter: cache.adapter.redis
             public: true
             default_lifetime: 1200
             provider: cache.default_redis_provider

When the Redis Extension is present it will use the extension elsewards it will use Predis, which is plain PHP.
You can also pass any other client. F.e. if you have already created a Predis client service in your app then just pass this as provider.

    cache:
        pools:
           sf_cache:
             adapter: cache.adapter.redis
             public: true
             default_lifetime: 1200
             provider: my.predis.client.service.id

I recently discovered SncRedisBundle and i have to say i like it :)
I used to create Predis clients directly because its very easy and thought i wouldnt need a bundle for this.
But with SncRedisBundle you get Logging and WebProfiler support additionally, which is great for development.
Also you get some more easy symfony integrations for other stuff as Doctrine, Mailings or Sessions which i dont use but maybe you do.
So check it out.

So my best move is to use SncRedisBundle to create a Redis client and integrate it with the Cache Component.

snc_redis:
    clients:
        sf_cache:
            type: predis
            alias: sf_cache
            dsn: "redis://%cache.redis_host%:%cache.redis_port%/0"
            logging: "%kernel.debug%"

framework:
    default_locale: de
    secret: ThisTokenIsNotSoSecretChangeIt
    assets: ~
    templating:
        engines: ['twig']
    cache:
        pools:
           sf_cache:
             adapter: cache.adapter.redis
             public: true
             default_lifetime: 1200
             provider: snc_redis.sf_cache

Bildschirmfoto vom 2016-08-23 09-37-43

Now you have integrated caching with webprofiler appearance and logging, nice!

Leave a Reply

Your email address will not be published. Required fields are marked *