The examples in this article describe the usage of Yii database cache. Share it with everyone for your reference, the details are as follows:
yii Operation database cache:
1. Add
to the main.php file'dbcache'=>array( 'class'=>'system.caching.CDbCache', //数据库缓存,注意你自己的路径问题 ),
2. Set up database cache
Yii::app()->cache->set($key,$value,$outtime); //$key 唯一主键,$value 对应主键的值(可以是数组), $outtime 过期时间。
3. Get cache
Yii::app()->cache->get($key); //设置数据库缓存时的主键key
4. Delete cache
Yii::app()->cache->delete($key);//同上
5. Clear cache files
Yii::app()->cache->fulsh(); //将删除服务器上面的所有文件缓存,即cache文件夹里面的所有缓存文件
Application examples: (Many videos are not given in the list page. If cached, the list page needs to have page information. It is a little more complicated. Here is a database cache example of the list page)
The current url address: http://www.aaaa.com/news/list/gid/2/nid/3/page/1.html
First determine whether the cache exists:
if(isset($_GET['gid'])){ $gid = intval($_GET['gid']); }else{ $gid = 1; } ..........
I have omitted other judgment conditions here. Currently, the only information that needs to be judged is $gid, $nid, $pages (note that the current variable does not use $page but $pages, because if $page is used, it will An error occurred, conflicting with $page in paging)
$newsListCache = Yii::app()->cache->get("newsList$gid$nid$pages"); //可以保证其唯一性即可 if(!empty($newsListCache))//判定如果有这个文件则走这个文件 下面return 了所以后面的数据就不会再走了 return $newsListCache; 。。。。。//这里就是你的其他代码数据,不用管它 Yii::app()->cache->set("newsList$gid$nid$pages",$newsList,3600);//这里的第一个参数需要和上面的对应,第二个参数就是你的数据 , 第三个参数就是过期时间。
Readers who are interested in more Yii-related content can check out the special topics on this site: "Introduction to Yii Framework and Summary of Common Techniques", "Summary of Excellent PHP Development Framework", "Basic Tutorial for Getting Started with Smarty Templates", "php Date and Time" Usage Summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.