如何在 Symfony 2 中缓存? [英] How to cache in Symfony 2?
本文介绍了如何在 Symfony 2 中缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用 Symfony 2 的缓存系统缓存一些特定于应用程序的数据,以便我可以运行 cache:clear
来清除它.所有缓存都依赖于 app/cache
但我实际上如何缓存数据?
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
我看到的唯一主题是关于使用 Varnish 缓存 HTML.
The only topic I see is about HTML caching with Varnish.
推荐答案
如果你已经在使用 Doctrine 就使用那些缓存类.
If you are using Doctrine already just use those cache classes.
向 config.yml
添加一个服务:
services:
cache:
class: DoctrineCommonCacheApcCache
并在您的控制器中使用它:
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));
}
这篇关于如何在 Symfony 2 中缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文