Caching a user control and flushing this cache programmatically

I am trying to cache user controls, and on some pages I want to cache individual objects. There are several ways to implement caching, and my head breaks over it.

Now I see the caching options:

You have the PartialCaching option, which is configured to cache the control for 30 minutes, and after that it is cleared ... You have the ByParam variable to identify the page by its querystring parameters ... or other change parameters

But I just can't find a suitable way to add caching to the control and be able to clear the caching programmatically when I update one of the objects used in the control from the backend.

You can make an HttpContext.Current.Cache.Insert () that accepts a key on which you can subsequently destroy the caching element by deleting ... This can save the objects in the cache, but you can use parameters like variableByParam?

My questions are burned to two:

  • Is there a way to clear the caching of certain user controls from code? If so, can this be done according to different options?
  • How will object caching respond to users or anonymous users with the Insert () function?

EDIT: I cache a few things ... And I'm really stunned when I choose to link to the cache. Can Cache.Insert vary in parameters?

The main problem is to copy the editing of things from the backend, which should trigger an event that restores or clears all caching elements that reference this object.

+4
source share
2 answers

Using the System.Web.Caching.Cache class, you can cache elements and create dependencies for elements in the cache. If you are using a SQL-server, you can use the class SqlCacheDependency for cleaning elements from your cache based on your database.

Otherwise, you can create your own derivatives of the CacheDependency class, which you can use to achieve the same. I found this post that describes this.

+2
source

You can remove items from the output cache using the following.

HttpResponse.RemoveOutputCacheItem("/caching/CacheForever.aspx"); 

Now, this will only make you part of what you are looking for. This will delete all cache entries for this particular page. The MSDN documentation confirms the behavior.

As for your other question, Cache.Insert () is one cache in the application, then the user ID is not considered.

Now I will also take a closer look at what you are doing, it may only make sense to cache the actual data, and then you can add / remove certain elements. However, if your .control really takes a lot of CPU, etc. To handle the display, the idea of ​​outputting the cache works.

+3
source

Source: https://habr.com/ru/post/1315546/


All Articles