Data caching mechanism of yii

Source: Internet
Author: User

The specific statement is that variable information can be cached.

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 ();

 

Application of cached data:

Used to cache data: You can customize a method in the data model to obtain the desired data and then cache it.

For example, when obtaining product details, you can customize a method to obtain product details in the Goods Model:

/** Get product details * @ $ ID: Product ID */function getgoodsinfobypk ($ id) {If (yii: APP () -> cache-> get ('goodsinfo '. $ id) {// If cache data exists, the cached data is returned. If no cache data exists, the data is retrieved from the database and saved to the cache. $ goods_info = yii: APP () -> cache-> get ('goodsinfo '. $ id);} else {$ goods_info = $ this-> findbysql ($ id); yii: APP ()-> cache-> set ('goodsinfo '. $ id, 3600);} return $ goods_info ;}

 

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.