How to cache in Symfony 2?

42,518

Solution 1

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

Add a service to config.yml:

services:
    cache:
        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));
}

Solution 2

Simple way use Doctrine cache providers. At first, register service(sample in config.yml):

services:
    memcached:
        class: Memcached
        calls:
            - [ addServer, ['localhost', 11211] ]
    memcached_cache:
        class: Doctrine\Common\Cache\MemcachedCache
        calls:
            - [ setMemcached, [@memcached] ]

Then to use get service, for example in controler:

$cache = $this->get('memcached_cache');

to send in another service use calls:

calls:
    - [ setCacheProvider, [@memcached_cache] ]

or arguments:

arguments:
    - @memcached_cache

In the same way, you can use other interfaces of Doctrine Cache package. Doctrine Cache provides a very simple interface for which several out of the box implementations are provided:

  • ApcCache (requires ext/apc)
  • ArrayCache (in memory, lifetime of the request)
  • FilesystemCache (not optimal for high concurrency)
  • MemcacheCache (requires ext/memcache)
  • MemcachedCache (requires ext/memcached)
  • PhpFileCache (not optimal for high concurrency)
  • RedisCache.php (requires ext/phpredis)
  • WinCacheCache.php (requires ext/wincache)
  • XcacheCache.php (requires ext/xcache)
  • ZendDataCache.php (requires Zend Server Platform)

If you do not already use Doctrine, you may require Common Library for Doctrine projects: php composer.phar require doctrine/common or require only Caching library offering an object-oriented API for many cache backends: php composer.phar require doctrine/cache

How to use Doctrine Caching you can read in Doctrine Common documentation on Doctrine Project web site

Solution 3

Symfony 3.1 provide a new Cache component.

Solution 4

Symfony2 does not provide any component for application layer caching.

Like you were already told, you can use the Doctrine Common caching library http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/caching.html

If you want something more advanced, you can also use one of the cache bundle provided by the community. For instance, the https://github.com/TheBigBrainsCompany/TbbcCacheBundle#cachebundle which provides tools for a good caching strategy.

Share:
42,518
Tower
Author by

Tower

Updated on July 09, 2022

Comments

  • Tower
    Tower almost 2 years

    I need to cache some application specific data using Symfony 2's caching system so that I can run cache:clear to clear it. All the cache relies under app/cache but how do I actually go about caching data?

    http://symfony.com/doc/current/cookbook/index.html

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

  • Tower
    Tower over 12 years
    I'm interested in caching app specific data, like computationally expensive results, etc. I'm already caching them on APC, but what's the point of having 2 different cache systems? I also need to clear two caches now, although I did make one command of my own which clears both, but still.
  • Tower
    Tower over 12 years
    What if I'm not using Doctrine? Why is this part of Doctrine?
  • ChocoDeveloper
    ChocoDeveloper over 11 years
    @Tower Still no answer to this?
  • ChocoDeveloper
    ChocoDeveloper over 11 years
    @Tower The answer seems to be that Doctrine needed this component, and created a damn good one so Symfony2 is not gonna reinvent it, but they didn't separate it into a different repository, so here we are.
  • Peter
    Peter about 11 years
    This is part of Doctrine\Common. Don't confuse this with ORM, DBAL etc. "The Doctrine Common project is a library that provides extensions to core PHP functionality."
  • unairoldan
    unairoldan about 11 years
    @Tower without use Doctrine: stackoverflow.com/questions/13407790/…
  • Kris Wallsmith
    Kris Wallsmith over 10 years
    The Doctrine cache classes are now available via the doctrine/cache package if you use Composer.
  • Francis Gonzales
    Francis Gonzales about 10 years
    Is there a way for set it a time?
  • Dimitry K
    Dimitry K over 9 years
    If you're not using whole Doctrine ORM, you can still install just part doctrine/cache packagist.org/packages/doctrine/doctrine-cache-bundle which is decoupled from the ORM part of the doctrine
  • forsberg
    forsberg almost 9 years
    It's said APC should not be used for caching data (like from DB) when it's used as opcode cache, because it affects its performance. There's a point why to use separate cache for custom data storing.
  • Lg102
    Lg102 over 8 years
    @FrancisGonzales The save method takes a third lifetime parameter: doctrine-project.org/api/common/2.0/…
  • ThomasP1988
    ThomasP1988 over 8 years
    thank you very much for this answer, it finally works, I just had a little error with YAML, it seems that @memcached require quotes - [ setMemcached, ['@memcached'] ] (I'm using symfony 3.0)
  • chris342423
    chris342423 over 7 years
    PS: Doctrine offers a lot of different cache implementations: github.com/doctrine/cache/tree/master/lib/Doctrine/Common/Ca‌​che
  • famas23
    famas23 over 5 years
    Hey thank you for your solution I have tow question does The class still the same with symfony4 ? no improvement ? and what command do we need to delete the cache?