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.