<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>4's symfony blog &#187; 缓存</title>
	<atom:link href="http://www.foolbirds.com/t/%e7%bc%93%e5%ad%98/feed" rel="self" type="application/rss+xml" />
	<link>http://www.foolbirds.com</link>
	<description>all about symfony</description>
	<lastBuildDate>Tue, 17 Aug 2010 01:22:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>symfony缓存(cache)机制简介</title>
		<link>http://www.foolbirds.com/symfony-cache.html</link>
		<comments>http://www.foolbirds.com/symfony-cache.html#comments</comments>
		<pubDate>Fri, 26 Dec 2008 02:32:35 +0000</pubDate>
		<dc:creator>kthiz2006</dc:creator>
				<category><![CDATA[symfony]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[缓存]]></category>

		<guid isPermaLink="false">http://symfony.bobhero.net/?p=235</guid>
		<description><![CDATA[关于缓存
symfony可以处理三种不同类型的html缓存：

动作的缓存（包含或不包含布局）
局部模板，组件或组件槽的缓存
模板片段的缓存

全局缓存设置
在项目的配置目录下：
例：
dev:
.settings:
cache: on
首先缓存一个动作：
例如在fontend/modules/shop/config/cache.yml(如果该文件不存在，就创建它)里写如下
list:
enabled: on        #开启缓存
with_layout: false #设置是否布局一起被缓存
lifetime: 86400    #设置缓存时间
这时就可以在dev下测试缓存的设置。
如果动作里的参数不一样，则在缓存中会根据参数的不同存入相应的记录。
缓存局部模板，组件或组件槽
例如在fontend/modules/shop/config/cache.yml里下如下
_my_partial：
contextual: true #如果把每个调用它的模板都保存一个版本，则设为true
enable： on
模板缓存片段
例如:
&#60;!&#8211; Code executed each time &#8211;&#62;
&#60;?php echo link_to('last accessed user', 'user/show?id='.$last_accessed_user_id) ?&#62;
&#60;!&#8211; Cached code &#8211;&#62;
&#60;?php if (!cache('users')):
foreach ($users as $user):
echo $user-&#62;getName() ;
endforeach;
cache_save() ;
 endif; ?&#62;
动态配置缓存
$context-&#62;getViewCacheManager()-&#62;addCache('article', 'show', array(
'withLayout' =&#62; true,
'lifeTime'   =&#62; 3600,
));
删除缓存的命令：
1.删除整个缓存
symfony clear:cache (简写 php symfony cc)
2.仅删除前台应用的缓存
php symfony cache:clear &#8211;app=frontend
3.仅删除前台应用的HTML缓存
php symfony cache:clear &#8211;app=frontend &#8211;type=template
4.仅删除前台应用的配置缓存
php symfony cache:clear [...]]]></description>
			<content:encoded><![CDATA[<p>关于缓存</p>
<p>symfony可以处理三种不同类型的html缓存：</p>
<ol>
<li>动作的缓存（包含或不包含布局）</li>
<li>局部模板，组件或组件槽的缓存</li>
<li>模板片段的缓存</li>
</ol>
<p>全局缓存设置<br />
在项目的配置目录下：<br />
例：</p>
<pre class="php" name="code">dev:
.settings:
cache: on</pre>
<p>首先缓存一个动作：<br />
例如在fontend/modules/shop/config/cache.yml(如果该文件不存在，就创建它)里写如下</p>
<pre class="php" name="code">list:
enabled: on        #开启缓存
with_layout: false #设置是否布局一起被缓存
lifetime: 86400    #设置缓存时间</pre>
<p>这时就可以在dev下测试缓存的设置。</p>
<p>如果动作里的参数不一样，则在缓存中会根据参数的不同存入相应的记录。</p>
<p>缓存局部模板，组件或组件槽<br />
例如在fontend/modules/shop/config/cache.yml里下如下</p>
<pre class="php" name="code">_my_partial：
contextual: true #如果把每个调用它的模板都保存一个版本，则设为true
enable： on</pre>
<p>模板缓存片段<br />
例如:<br />
&lt;!&#8211; Code executed each time &#8211;&gt;</p>
<pre class="php" name="code">&lt;?php echo link_to('last accessed user', 'user/show?id='.$last_accessed_user_id) ?&gt;</pre>
<p>&lt;!&#8211; Cached code &#8211;&gt;</p>
<pre class="php" name="code">&lt;?php if (!cache('users')):
foreach ($users as $user):
echo $user-&gt;getName() ;
endforeach;
cache_save() ;
 endif; ?&gt;</pre>
<p>动态配置缓存</p>
<pre class="php" name="code">$context-&gt;getViewCacheManager()-&gt;addCache('article', 'show', array(
'withLayout' =&gt; true,
'lifeTime'   =&gt; 3600,
));</pre>
<p>删除缓存的命令：<br />
1.删除整个缓存<br />
symfony clear:cache (简写 php symfony cc)<br />
2.仅删除前台应用的缓存<br />
php symfony cache:clear &#8211;app=frontend<br />
3.仅删除前台应用的HTML缓存<br />
php symfony cache:clear &#8211;app=frontend &#8211;type=template<br />
4.仅删除前台应用的配置缓存<br />
php symfony cache:clear &#8211;app=frontend &#8211;type=config<br />
5.仅删除前台应用的发布模式下的配置缓存<br />
php symfony cache:clear &#8211;app=frontend &#8211;type=config &#8211;env=prod</p>
<p>用$this-&gt;getContext()-&gt;getViewCacheManager()-&gt;remove()删除指定缓存.<br />
用sfToolkit::clearGlob()方法删除指定缓存所在的目录。<br />
例如：</p>
<pre class="php" name="code">&lt;?php
$sf_root_cache_dir = sfConfig::get('sf_cache_dir');
$cache_dir = $sf_root_cache_dir.'/frontend/cache/*';#清楚前台缓存目录
sfToolkit::clearGlob($cache_dir);
?&gt;</pre>
<p>下面举例个例子：</p>
<p>首先拷贝一个frontend_dev.php改名为frontend_cache.php,里面内容改为如下：</p>
<pre class="php" name="code">&lt;?php
require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');

$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'cache', true);
sfContext::createInstance($configuration)-&gt;dispatch();
?&gt;</pre>
<p>然后在apps/frontend/config/settings.yml里设置</p>
<pre class="php" name="code">cache:
.settings:
error_reporting: &lt;?php echo (E_ALL | E_STRICT)."n" ?&gt;
web_debug:       on
cache:           on
etag:            off</pre>
<p>接着在config/databases.yml里写</p>
<pre class="php" name="code">cache:
propel:
class: sfPropelDatabase
param:
classname: DebugPDO</pre>
<p>然后清楚缓存。</p>
<p>接着在apps/frontend/config/cache.yml下写</p>
<pre class="php" name="code">default:
enabled:     off
with_layout: false
lifetime:    86400</pre>
<p>然后在apps/frontend/modules/shop/config/cache.yml(注：config目录如果不存在则创建它)下写</p>
<pre class="php" name="code">index:
enabled:     on
with_layout: true</pre>
<p>清除缓存</p>
<p>这样刷新页面，就可以在shop的index页面看到红色边框，和蓝色的小框，刷新页面，蓝色变成黄色。可以看时间感受一下缓存后的速度。</p>
<p>这时如果想删除前台的整个缓存可以使用如下方法</p>
<pre class="php" name="code">&lt;?php
$sf_root_cache_dir = sfConfig::get('sf_cache_dir');
$cache_dir = $sf_root_cache_dir.'/frontend/cache/*';#清楚前台缓存目录
sfToolkit::clearGlob($cache_dir);
?&gt;</pre>
<p>也可以用</p>
<pre class="php" name="code">&lt;?php
$sf_root_cache_dir = sfConfig::get('sf_cache_dir');
$cache_dir = $sf_root_cache_dir.'/frontend'; #清楚前台缓存目录
sfToolkit::clearDirectory($cache_dir);
?&gt;</pre>
<p>这样缓存就被清除了。</p>
<p>HTTP1.1与客户端缓存<br />
目前浏览器都支持http1.1(即使不支持也没关系)<br />
增加ETag头信息来避免发送重复的内容(作用是当开启该功能后，服务器会增加一个含有响应本身签名的专门的头部。用户的浏览器会把它保存下来，代下次请求<br />
的时候把这个签名一起发出去。如果与新签名一致，则服务器不会发出响应，而是发出一个没有修改的头信息。这样就节省了服务器的CPU时间和带宽，还有客户端的时间)<br />
例如：在settings.yml里设置开启ETag功能.</p>
<pre class="php" name="code">all:
.settings:
etag: on
(注：默认是开启的)</pre>
<p>增加Last-Modified头信息避免发送仍然有效的内容<br />
服务器向浏览器发送响应的时候，会增加一个头部说明页面的数据的最后修改时间。<br />
浏览器理解这个信息，当再次请求这个页面的时候，会对应的加上If-Modified<br />
这样服务器可以比较客户端和应用程序返回的这个值，如果相同，则返回没有修改的头信息。这样可以节约带宽和CPU时间。<br />
当然在symfony可以手动设置时间<br />
例如:</p>
<pre class="php" name="code">&lt;?php $this-&gt;getResponse()-&gt;setHttpHeader('Last-Modified', $this-&gt;getResponse()-&gt;getDate($timestamp));?&gt;</pre>
<p>通过增加Vary头信息保存一个页面的多个缓存版本，但这会增加缓存的大小，不过，服务器收到匹配的头信息的时候，就可以直接从缓存里取出响应而无须处理。<br />
例如:</p>
<pre class="php" name="code">&lt;?php
$this-&gt;getResponse()-&gt;addVaryHttpHeader('Cookie');
$this-&gt;getResponse()-&gt;addVaryHttpHeader('User-Agent');
$this-&gt;getResponse()-&gt;addVaryHttpHeader('Accept-Language');
?&gt;</pre>
<p>通过增加Cache-Control头信息来允许客户端缓存<br />
例如:<br />
可以定义缓存的时间</p>
<pre class="php" name="code">&lt;?php $this-&gt;getResponse()-&gt;addCacheControlHttpHeader('max_age=60');?&gt;</pre>
<p>指定页面的缓存的条件</p>
<pre class="php" name="code">&lt;?php $this-&gt;getResponse()-&gt;addCacheControlHttpHeader('private=True');?&gt;</pre>
<p>设置过期时间</p>
<pre class="php" name="code">&lt;?php $this-&gt;getResponse()-&gt;setHttpHeader('Expires', $this-&gt;getResponse()-&gt;getDate($timestamp));?&gt;
</pre>
<p>使用数据库存储系统进行缓存（好处是当应用程序的缓存压力非常大时，模板的缓存文件最终会分散在很深的文件结构中，在这种情况下用sqlite存储会更快。清除缓存过程将只是一个简单的文件删除）<br />
首先查看php的扩展是否支持sqlite,如果不支持，请安装它.<br />
安装好后,在应用程序的config里的factories.yml的all下面添加如下:</p>
<pre class="php" name="code">&lt;?php
all:
  view_cache:
    class: sfSQLiteCache
    param:
      database: %SF_TEMPLATE_CACHE_DIR%/cache.db
?&gt;</pre>
<p>最后清缓存.<br />
这样，就在你为应用程序的开启缓存的模式下启用了。</p>
<ul class="related_post"><li><a href="http://www.foolbirds.com/use_symfony_filter_to_filteredurl.html" title="使用SYMFONY Filter 过滤URL">使用SYMFONY Filter 过滤URL</a></li><li><a href="http://www.foolbirds.com/%e4%bb%bfsymfony%e6%9c%ba%e5%88%b6%e5%ae%9e%e7%8e%b0%e4%b8%8d%e7%94%a8require%e6%88%96%e8%80%85include%e6%9d%a5%e5%ae%9e%e4%be%8b%e5%8c%96%e7%b1%bb.html" title="仿symfony机制实现不用require或者include来实例化类">仿symfony机制实现不用require或者include来实例化类</a></li><li><a href="http://www.foolbirds.com/use-datetime-in-php5-1-x-with-symfony.html" title="symfony1.4 DateTime对于PHP低版本的兼容问题">symfony1.4 DateTime对于PHP低版本的兼容问题</a></li><li><a href="http://www.foolbirds.com/%e5%a6%82%e4%bd%95%e5%9c%a8fixtures-yml%e5%86%99%e5%be%aa%e7%8e%af%e6%b7%bb%e5%8a%a0%e6%95%b0%e6%8d%ae.html" title="如何在fixtures.yml写循环添加数据">如何在fixtures.yml写循环添加数据</a></li><li><a href="http://www.foolbirds.com/symfony-1-4-database-utf8.html" title="symfony 1.4 数据库 utf8设置">symfony 1.4 数据库 utf8设置</a></li><li><a href="http://www.foolbirds.com/symfony-cheat-sheet.html" title="symfony cheat sheet">symfony cheat sheet</a></li><li><a href="http://www.foolbirds.com/cheat-sheets.html" title="Cheat Sheets!">Cheat Sheets!</a></li><li><a href="http://www.foolbirds.com/how-to-use-swift-to-send-mail-in-symfon.html" title="如何使用swift发送邮件">如何使用swift发送邮件</a></li><li><a href="http://www.foolbirds.com/batch-in-symfony12.html" title="symfony1.2下的命令行程序(batch)">symfony1.2下的命令行程序(batch)</a></li><li><a href="http://www.foolbirds.com/generate-admin-templates-structure.html" title="symfony1.2的后台(generate-admin)模板结构">symfony1.2的后台(generate-admin)模板结构</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.foolbirds.com/symfony-cache.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
