Heim  >  Artikel  >  Backend-Entwicklung  >  symfony2怎么使用文件缓存?

symfony2怎么使用文件缓存?

WBOY
WBOYOriginal
2016-06-06 20:48:401073Durchsuche

网站主菜单数组,获取比较复杂,而且每个页面都要调用,想获取一次后缓存起来,有什么比较简单的方法?

不用框架的话,会像下面这样把菜单存在文本里

<code class="lang-javascript">if (is_file('menu.txt')) {
    $mainMenuArr = unserialize(file_get_contnets('menu.txt'));
} else {
    $mainMenuArr = $this->getMainMenu();
    file_put_contents('menu.txt', serialize($mainMenuArr ));
}
</code>

而symfony2,看文档里关于缓存的部分,只有HTTP Cache,这些对于小项目不怎么适用 http://symfony.com/doc/current/book/http_cache.html

回复内容:

网站主菜单数组,获取比较复杂,而且每个页面都要调用,想获取一次后缓存起来,有什么比较简单的方法?

不用框架的话,会像下面这样把菜单存在文本里

<code class="lang-javascript">if (is_file('menu.txt')) {
    $mainMenuArr = unserialize(file_get_contnets('menu.txt'));
} else {
    $mainMenuArr = $this->getMainMenu();
    file_put_contents('menu.txt', serialize($mainMenuArr ));
}
</code>

而symfony2,看文档里关于缓存的部分,只有HTTP Cache,这些对于小项目不怎么适用 http://symfony.com/doc/current/book/http_cache.html

用Doctrine的Cache就可以,你是看文档得出的只有那些内存缓存的结论的吧?文档其实不全 :)

看源代码吧,什么都有: https://github.com/doctrine/cache/tree/master/lib/Doctrine/Common/Cache

如FileCache: https://github.com/doctrine/cache/blob/master/lib/Doctrine/Common/Cache/FileCache.php

基本缓存就这样吧 memcached 或者Nosql数据库。

文件缓存的话,可以直接存html文件~

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn