Yii database cache instance analysis

Source: Internet
Author: User
Tags delete cache
This article mainly introduces the usage of the Yii database cache, and analyzes the usage steps and implementation code of the Yii database cache based on the instance form, for more information about Yii database cache usage, see the following example. We will share this with you for your reference. The details are as follows:

Yii operation database cache:

1. add

'Dbcache' => array ('class' => 'system. caching. cdbcache', // Database cache, pay attention to your path ),

2. Set Database cache

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

3. obtain the cache

Yii: app ()-> cache-> get ($ key); // you can specify the primary key of the database cache.

4. delete cache

Yii: app ()-> cache-> delete ($ key); // same as above

5. clear cache files

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

Application example: (a lot of videos are not provided on the list page. if the list page is cached, the list page must have page information. this is a little more complex. the following shows a list page Database cache instance)

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

Determine whether the cache exists first:

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

I have omitted other criteria. Currently, only $ gid, $ nid, and $ pages are required. (note that the current variable uses $ pages instead of $ page, because if $ page is used, an error is returned, which is in conflict with $ page in the page)

$ NewsListCache = Yii: app ()-> cache-> get ("newsList $ gid $ nid $ pages"); // you can ensure its uniqueness if (! Empty ($ newsListCache) // determines that if a file exists, return is taken under the file, so the subsequent data will not go through return $ newsListCache ;..... // Here is your other code data, regardless of its Yii: app ()-> cache-> set ("newsList $ gid $ nid $ pages", $ newsList, 3600); // The first parameter must correspond to the above parameter. The second parameter is your data, and the third parameter is the expiration time.

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.