[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