Home > Backend Development > PHP Tutorial > Yii database cache instance analysis, yii database instance analysis_PHP tutorial

Yii database cache instance analysis, yii database instance analysis_PHP tutorial

WBOY
Release: 2016-07-12 08:55:12
Original
877 people have browsed it

Yii database cache instance analysis, yii database instance analysis

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',
  //数据库缓存,注意你自己的路径问题
),

Copy after login

2. Set up database cache

Yii::app()->cache->set($key,$value,$outtime);
//$key 唯一主键,$value 对应主键的值(可以是数组), $outtime 过期时间。

Copy after login

3. Get cache

Yii::app()->cache->get($key);
//设置数据库缓存时的主键key

Copy after login

4. Delete cache

Yii::app()->cache->delete($key);//同上

Copy after login

5. Clear cache files

Yii::app()->cache->fulsh();
//将删除服务器上面的所有文件缓存,即cache文件夹里面的所有缓存文件

Copy after login

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;
}
..........

Copy after login

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);//这里的第一个参数需要和上面的对应,第二个参数就是你的数据 , 第三个参数就是过期时间。
Copy after login

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.

Articles you may be interested in:

  • YII Framework filter usage analysis
  • Introduction to some advanced usage of caching in PHP's Yii framework
  • In-depth analysis of the caching function in PHP's Yii framework
  • Advanced use of View in PHP's Yii framework
  • Study tutorial on Model model in PHP's Yii framework
  • Detailed explanation Controller controller in PHP's Yii framework
  • Yii's method of turning on fragment caching
  • Detailed explanation of attribute injection and method injection of component behavior in PHP's Yii framework
  • Detailed explanation in PHP Methods of using Behaviors in Yii Framework
  • YII Framework learning request and response usage (based on CHttpRequest response)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1117088.htmlTechArticleYii database cache instance analysis, yii database instance analysis This article describes the usage of Yii database cache with examples. Share it with everyone for your reference, the details are as follows: yii operation database cache:...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template