首頁  >  文章  >  後端開發  >  symfony2怎么使用文件缓存?

symfony2怎么使用文件缓存?

WBOY
WBOY原創
2016-06-06 20:48:401073瀏覽

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

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

<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文件~

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn