asp.net instance code for output cache removal

Source: Internet
Author: User
Tags datetime tostring

Asp. NET output caching has many examples on the Internet, here is how to remove the cache in the background management.

1. Based on page caching

For pages: Default.aspx if the top of the page is added:

<%@ OutputCache duration= "No" varybyparam= "None"%>

It is easy to remove in the background management:

System.Web.HttpResponse.RemoveOutputCacheItem (Page.resolveurl ("default.aspx"));

2. Based on control

For control Webusercontrol.ascx If you add a

<%@ OutputCache duration= "a" varybyparam= "None" shared= "true"%>

In the background management to achieve a bit of trouble, in the blog Park Bo asked friends to answer, Charles provides a solution.

Implemented as follows:

(1) Add the VaryByCustom entry, the value is Cashgroupclass.

<%@ OutputCache duration= varybyparam= "None" shared= "true" varybycustom= "Cashgroupclass"%>

(2) Rewrite the GetVaryByCustomString method in Global.asax, the code is as follows:

Code

public override string GetVaryByCustomString (HttpContext context, string arg)

{

if (arg = = "Cashgroupclass")

{

Cache Objcache = Httpruntime.cache;

Object _flag = objcache["Cashgroupclass"];

if (_flag = null)

{

_flag = DateTime.Now.Ticks.ToString ();

Objcache.insert ("Cashgroupclass", _flag);

}

Return _flag. ToString ();

}

Return base. GetVaryByCustomString (context, ARG);

}

(3) in the background management of the removal page add the following code:

Cache Objcache = Httpruntime.cache;

if (objcache["Cashgroupclass"]!= null)

{

Objcache.remove ("Cashgroupclass");

}

Of course, you can also use this method to implement cache updates for controls. Yes, Charles posted in the code to use the Datacache class, is a write their own class, you can refer to the Datacache, but inside the overload parameter is not. Then add one.

Code

public static void Setcache (String CacheKey, Object Objobject, DateTime absoluteexpiration, TimeSpan slidingexpiration)

{

HttpRuntime.Cache.Insert (CacheKey, objobject, NULL, absoluteexpiration, slidingexpiration);

}

Finally, I thank my friends for their help.

Reference: (1): Caching application pages and data (i)

(2): asp.net cache

(3): Where the getvarybycustomstring function in Global.asax.cs is invoked

(4): Datacache

Related Article

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.