Cache Mechanism of pet shop 4.0

Source: Internet
Author: User

First, ASP is applied to the pet shop. the newly added sqlcachedependency capability in net 2.0. By setting the configuration file, framwork automatically regularly polls a monitoring table in the database, and the monitored table has a trigger, when an insert, update, or delete operation occurs, the changeid value in the monitoring table is changed. In this way, when changeid changes, sqlcachedependency will automatically invalidate the cache. When the next request arrives at the server,ProgramThe latest data will be extracted directly from the database and inserted into the cache again, so the cache does not need to be manually encoded when it expires!

But check carefullyCodeYou will find that there are a series of dataproxy classes under the app_code folder of the web project, all of which are static methods. It is not difficult to see through the code that these classes are used for data access, that is, first determine whether the required data already exists in the cache. If yes, access the cache directly. If no, access the database to return the filled data entity object and cache it. When the cache is inserted, the sqlcachedependency mechanism is used.

However, if you search for the corresponding dataproxy class in the entire solution, you will get nothing! Are these codes just put there? Are they just to show their new hammers?

I was puzzled at first, and then suddenly realized when I saw the code on the page. Two cache methods were implemented in pet shop. One is the traditional method similar to dataproxy, which caches data entities, and the other is combined with ASP. NET page cache mechanism.

View productscontrol. in the ascx file, you will find a pseudocommand statement in the header: <% @ outputcache duration = "100000" varybyparam = "page; categoryid" %> the first parameter defines the expiration time of the control, the second parameter defines the parameter for distinguishing the cache (New in 2.0), and then view productscontrol. ascx. CS file, you will find productslist in the protected void pagechanged method. datasource = product. getproductsbycategory, the protected void page_load method has a line of statement this. cachepolicy. dependency = d Ependencyfacade. getproductdependency (); this statement is used to apply sqlcachedependency to page cache expiration.

In this way, the cache is not just a data entity class, but the HTML after the entire control is rendered! In this way, the render operation for each page access is saved, which is much more efficient than the cache object class!

PS: If you want to use the Light cache object class method, delete the outputcache statement in the ascx file, comment out the expiration Policy Statement in page_load, and then set productslist. datasource = product. getproductsbycategory (categorykey); changed to productslist. datasource = productdataproxy. getproductsbycategory (categorykey); perform the same operation on other pages.

The conclusion is that petshop implements two caching methods, but it uses a more efficient method by default. Unfortunately, it's hard for beginners to understand. 22 projects are missing...

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.