最近在用Yii框架开发项目中遇到一个缓存问题,先前只是考虑到把内容页面整体缓存了下来,最后发现由于有用户状态的切换,所以有时候其他页面用户状态改变了,但是这个页面由于没有过期状态还是没有改变回来,就给用户的体验不好,于是想了个笨方法
1.作一个钩子,每次在访问页面成功之后去判断用户状态,作一个js的回调函数,结果我想多了
2.Yii之所以强大在于专注了各种逻辑实现封装而你只需要考虑业务的开展
以下我只梳理下我的页面缓存与局部动态缓存的实现其他的略过
yii 中缓存大概有这样几种 数据、片段、整体页面、局部动态这几种吧
废话少说,先贴代码
引入组件 main.php
'cache' => array ( 'class' => 'system.caching.CFileCache', 'directoryLevel' => 2, 'cachePath'=>'./cache', 'cacheFileSuffix'=>'.weige' ),
控制器中siteController.php
public function filters() { /** * varyByRoute: 这个参数指定缓存内容是否 随着请求的路由(控制器与动作)而变化。 * varyByParam: 这个参数指定了一个GET参数 的名字列表并使用相应的值来决定缓存数据的版本。 * varyBySession: 这个参数指定缓存内容是 否随着用户会话而变化。 * varyByExpression: 这个参数指定缓存内容 是否随着指定的PHP表达式的结果而变化。 * * */ return array( array( 'COutputCache+rss',//要缓存的组件和方法 默认控制器的全部方法 'duration'=>100,//过期时间秒杀 默认60s //'varyByParam'=>array('id'), //'varyBySession'=>true, //'varyByExpression'=>'$user->isGuest', //'cacheID'=>'cache',// main.php 引入的组件别名 // array( // 'class'=>'CGlobalStateCacheDependency', // 'stateName'=>'user_id',//自定义的sateName相当于key // ), ), ); } public function getTimer() { return date("Y-m-d H:i:s"); } public function getHeader() { if(Yii::app()->session['wei'] =='wei') { return 'i am wei ge'.$this->timer; } else { return 'i am leo.yan'.$this->timer; } } public function actionRss() { $this->render('rss'); }
视图 rss.php
<?php echo"<pre>"; print_r($_SERVER); ?> ssssssssssssssssssssssssssssssss ssssssssssssssssssssssssssssssss ssssssssssssssssssssssssssssssss <div style='color:blue;height:80px;width:500px;'> <?php echo '我是威哥'.$this->timer;?> </div> <div style='color:red;height:50px;width:500px;'> <?php $this->renderDynamic('getHeader'); ?> </div> ttttttttttttttttttttttttttttttttt ttttttttttttttttttttttttttttttttt ttttttttttttttttttttttttttttttttt ttttttttttttttttttttttttttttttttt ttttttttttttttttttttttttttttttttt
执行如图示:我们想要的结果
第一次访问
第二次访问 请留意下面 请求时间和蓝色红色字体部分(3个箭头)
刷新其他页面 局部动态 请留意下面 请求时间和蓝色红色字体部分(3个箭头)各发生的变化
再看看缓存文件结果
存了两个文件 状态+内容 可能注意到<###dynamic-0###>这个东东 ,
细心去看 和1的原理差不多 ,不信你看代码
CController.php /** * Postprocesses the dynamic output. * This method is internally used. Do not call this method directly. * @param string $output output to be processed * @return string the processed output */ public function processDynamicOutput($output) { if($this->_dynamicOutput) { $output=preg_replace_callback('/<###dynamic-(\d+)###>/',array($this,'replaceDynamicOutput'),$output); } return $output; } /** * Renders dynamic content returned by the specified callback. * This method is used together with {@link COutputCache}. Dynamic contents * will always show as their latest state even if the content surrounding them is being cached. * This is especially useful when caching pages that are mostly static but contain some small * dynamic regions, such as username or current time. * We can use this method to render these dynamic regions to ensure they are always up-to-date. * * The first parameter to this method should be a valid PHP callback, while the rest parameters * will be passed to the callback. * * Note, the callback and its parameter values will be serialized and saved in cache. * Make sure they are serializable. * * @param callback $callback a PHP callback which returns the needed dynamic content. * When the callback is specified as a string, it will be first assumed to be a method of the current * controller class. If the method does not exist, it is assumed to be a global PHP function. * Note, the callback should return the dynamic content instead of echoing it. */ public function renderDynamic($callback) { $n=count($this->_dynamicOutput); echo "<###dynamic-$n###>"; $params=func_get_args(); array_shift($params); $this->renderDynamicInternal($callback,$params); }
所以说,没有你做不到,只有你想不到,哎,就到这里吧,脱光,洗澡,睡觉了吧~