Yii cache segment Cache

Source: Internet
Author: User
Tags php foreach website performance

When we access the website, some information is stable for a relatively period of time, and we put this information into the cache, so that every time a user accesses the website, it accesses the cache information, for websites with large loads, the website performance can be significantly improved.


Yii cache type:


Cache category:

Fragment cache a page is divided into several regions, which are relatively independent. When designing the cache, you can develop and prepare the cache for each region independently.

Page cache: cache the entire page

Dynamic Cache

Data Cache


Use the clip cache step:

1. Set cache in the program

<Form name = "compareform" Action = "compare. PHP "method =" Post "onsubmit =" Return comparegoods (this); "> <Div class =" Clearfix goodsbox "style =" border: Medium none; padding: 11px 0pt 10px 5px; "> <! -- Segment cache implementation --> <? PHP/* if ($ this-> begincache ('cache name ')) {* duration setting expiration time * varybyparam cache change * dependency cache dependency */if ($ this-> begincache ('goods ', array ('duration' => 3600, 'varybyparam' => array ('page'), 'dependency '=> array ('class' => 'System. caching. dependencies. cdbcachedependency ',' SQL '=> 'select sum (goods_price) from {goods}',) {?> <? PHP foreach ($ goods_infos as $ _ v) {?> <Div class = "goodsitem"> <a href = "./index. php? R = goods/detail & id = <? PHP echo $ _ v-> goods_id?> "Target =" _ blank ">  goods_big_img;?> "Alt =" <? PHP echo $ _ v-> goods_name?> "Class =" goodsimg "> </a> <br/> <p> <a href =" # "Title =" Nokia n85 "> <? PHP echo $ _ v-> goods_name?> </A> </P> <font class = "market_s" >¥ <? PHP echo $ _ v-> goods_price?> RMB </font> <br/> <font class = "shop_s" >¥ <? PHP echo $ _ v-> goods_price?> RMB </font> <br/> <a href = "#">  Goumai.gif "> </a> <a href =" # ">  Shoucang.gif "> </a> </div> <? PHP }?> <? PHP $ this-> endcache () ;}?> </Div> </form>


2. Configure the cache component in the main. php file of the master configuration file (in this case, decide which cache to use). Our cache is the File Cache, And the cached information is stored in the directory runtime.



Set cache file expiration time

Duration ()

We set the expiration time for the cache file. When it does not expire, the system will use the cache file. If it has expired, the system will regenerate the cache file.


Cache changes:

Our product list implements segment caching, so we also access this cache page when accessing other page information, because the routes of each page information are the same (goods/category ), the system will mistakenly believe that the page accessed by the current system is a page. At that time, the page parameters on the page were different.

Therefore, we need to generate different cache files based on parameter changes.


Different cache files are generated based on different page numbers:



Cache dependency:

You can customize the expiration time for our product information cache. Based on the specific requirements of the business type, if the product information has changed before expiration (the database is changed ), at this time, the cache file has not expired. We need to update the cache file in time (even if it has not expired) to avoid misleading customers. In this case, we can implement this through cache dependencies.

 

Cache dependency condition: the condition on which we depend is the total commodity price. If the total price changes, the commodity information may increase or decrease, or the commodity price may be modified.



Where are duration, varybyparam, and dependency attributes?

System. Web. Widgets. coutputcache. php



Yii cache segment 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.