How to cache in Symfony 2?

后端 未结 5 2273
既然无缘
既然无缘 2020-11-29 18:20

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

5条回答
  •  甜味超标
    2020-11-29 18:38

    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));
    }
    

提交回复
热议问题