Tower Tower - 1 year ago 50
PHP Question

How to cache in Symfony 2?

I need to cache some application specific data using Symfony 2's caching system so that I can run

to clear it. All the cache relies under
but how do I actually go about caching data?

The only topic I see is about HTML caching with Varnish.

Answer Source

If you are using Doctrine already just use those cache classes.

Add a service to config.yml:

        class: Doctrine\Common\Cache\ApcCache

And use it in your controller:

if ($fooString = $this->get('cache')->fetch('foo')) {
    $foo = unserialize($fooString);
} else {
    // do the work
    $this->get('cache')->save('foo', serialize($foo));