Yii data cache

Source: Internet
Author: User
[Data cache]
Specifically, variable information can be cached.
Variable Cache Usage


Settings: yii: APP ()-> cache-> set (name, value, expiration time );
Use: yii: APP ()-> cache-> get (name );
Delete: yii: APP ()-> cache-> Delete (name );

Clear: yii: APP ()-> cache-> flush ();

Simple Example:

Function actionhuan1 () {// set the variable cache yii: APP ()-> cache-> set ('username', 'zhangsan', 3600); yii: APP () -> cache-> set ('useradd', 'beijing', 3600); yii: APP ()-> cache-> set ('hobby', 'lanqiu ', 3600); echo "set cache is OK";} function actionhuan2 () {// use the variable to cache echo yii: APP () -> cache-> get ('username'), "<br/>"; echo yii: APP ()-> cache-> get ('useradd '), "<br/>"; echo yii: APP ()-> cache-> get ('hobby'), "<br/> "; echo "use cache is OK";} function actionhuan3 () {// Delete cache variable // yii: APP ()-> cache-> Delete ('username '); // clear the cache variable. You can also delete the fragment cache or File Cache yii: APP ()-> cache-> flush ();}

Example:

SQL query in the Model

Function getgoodsinfobypk ($ id) {// Save the obtained detailed item information to the cache, and then obtain the information again. read $ info = yii: APP () -> cache-> get ('goods _ info '. $ id); // obtain the cache information // determine whether the cache information has an IF (! Empty ($ info) return $ Info; $ SQL = "select * from {goods} Where goods_id = '$ id '"; $ goods_info = $ this-> findbysql ($ SQL); // sets the cache yii: APP ()-> cache-> set ('goods _ info '. $ id, $ goods_info, 3600); return $ goods_info ;}



Yii data cache

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.