Example analysis _php of yii Database Cache instance

Source: Internet
Author: User
Tags php programming delete cache smarty template yii

This example describes the use of Yii database caching. Share to everyone for your reference, specific as follows:

Yii Operations Database Cache:

1, in the main.php main file to add

' Dbcache ' =>array (
  ' class ' => ' System.caching.CDbCache ',
  //database caching, pay attention to your own path issues
),

2, set up the database cache

Yii::app ()->cache->set ($key, $value, $outtime);
$key a unique primary key, $value the value of the corresponding primary key (which can be an array), $outtime the expiration time.

3. Get Cache

Yii::app ()->cache->get ($key);
Primary key keys when setting the database cache

4. Delete Cache

Yii::app ()->cache->delete ($key);/ibid.

5, empty the cache file

Yii::app ()->cache->fulsh ();
All file caches above the server will be deleted, that is, all cached files in the cache folder

Application Example: (A lot of video is not given in the list page if you do caching, the list page needs to have page information, slightly more complex, the following gives a list page of the database Cache instance)

Current URL address: http://www.aaaa.com/news/list/gid/2/nid/3/page/1.html

First determine if the cache exists:

if (isset ($_get[' gid ')) {
     $gid = intval ($_get[' gid '));
else{
     $gid = 1;
}
..........

I have omitted the other judgment condition here, the information that only needs to decide now has $gid, $nid, $pages (note, the current variable does not use $page but use $pages, because if use $page error, and paging $page conflict)

$newsListCache = Yii::app ()->cache->get ("Newslist$gid$nid$pages");
can guarantee its uniqueness can be
if (!empty ($newsListCache))//To determine if there is this file to go under this file return, so the following data will not go back to
$newsListCache;
。。 Here is your other code data, don't worry about it
Yii::app ()->cache->set ("Newslist$gid$nid$pages", $newsList, 3600); The first parameter here needs to correspond to the above, the second parameter is your data, and the third parameter is the expiration time.

More about Yii related content readers can view the site topics: "Yii framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Tutorial", "PHP date and Time usage summary", "PHP object-oriented Programming Program", " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

I hope this article will help you with your PHP programming based on the YII framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.