Sample Code removed from ASP. NET output cache

Source: Internet
Author: User

There are already many examples of using the ASP. NET output cache. Here we will mainly introduce how to remove the cache from the background management.

1. Based on page Cache

For page: Default. aspx, if you add:

<% @ OutputCache Duration = "60" VaryByParam = "none" %>

In the background management, it is easy to remove:

System. Web. HttpResponse. RemoveOutputCacheItem (Page. ResolveUrl ("Default. aspx "));

2. control-based

If WebUserControl. ascx is added to the top

<% @ OutputCache Duration = "60" VaryByParam = "none" Shared = "true" %>

It is a little troublesome to implement it in the background management. In the blog garden, ask friends to answer questions. Charles provides a solution.

The implementation is as follows:

(1) Add a VaryByCustom entry with the value Cashgroupclass.

<% @ OutputCache Duration = "60" VaryByParam = "none" Shared = "true" VaryByCustom = "Cashgroupclass" %>

(2) override 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) Add the following code on the remove page of the background management:

Cache objCache = HttpRuntime. Cache;
If (objCache ["Cashgroupclass"]! = Null)
{
ObjCache. Remove ("Cashgroupclass ");
}

You can also use this method to update the control cache. By the way, Charles's Code uses the DataCache class, which is a self-written class. You can refer to DataCache, but the overload parameters in it are not correct. 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 would like to thank my friends for their help.

Reference: (1) cache application pages and data (1)

(2): ASP. NET Cache

(3): Where is the GetVaryByCustomString function called in Global. asax. cs?

(4): DataCache

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.