Cache cache file dependency and programming method to set output cache expiration

Source: Internet
Author: User

You can cache a file or a group of files on the page and set dependencies. However, when a file is changed, the cache becomes invalid.
Aspx is written in the header
<% @ Outputcache varybyparam = "NONE" Duration = "9999" %>
The call time can be used to determine whether to cache
<% = Datetime. Now. tostring ("T") %>
Protected void page_load (Object sender, eventargs E)
{
Response. addfiledependency (mappath ("Luca. xml "));

}
Bind XML with gridview
If the modified XML cache is invalid

Another programming method is to set cache failure.
If a page displays jobs data, the page sets the cache. When I add data to another page, I will remove the data cache that displays jobs.
Add a page
<% @ Outputcache varybyparam = "NONE" Duration = "9999" %>
This page is called jobs. aspx.
Get the value of jobs
I will not write it if I bind it with the gridview.
Add a link to the add jobs page
<A href = "addjobs. aspx"> addjobs. aspx </a>

On the addjobs page
After the add operation is completed, write as follows:Code

string SQL = "insert into jobs values ('luca', 30,50)";
sqlcommand cmd = new sqlcommand (SQL, con);
con. open ();
cmd. executenonquery ();
con. close (); httpresponse. removeoutputcacheitem (page. resolveurl ("~ /Jobs. aspx ");
response. Redirect ("~ /Jobs. aspx ");
}

There is another situation.
If multiple pages call jobs data and the data is cached
The above httpresponse. removeoutputcacheitem can only remove one page at a time. We need to use the cache
In jobs. aspx
Protected void page_load (Object sender, eventargs E)
{
Cache. insert ("Jobs", datetime. Now );
Response. addcacheitemdependency ("Jobs ");
}
Then, after adding an operation in addjobs
Cache. Remove ("Jobs ");
OK.
If another page such as jobsdetails. aspx
Jobs data is also cached.
Also, you only need to write in page_load.
Response. addcacheitemdependency ("Jobs ");
The cache will also be lost.

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.