Caching a user control and clearing that cache programmatically - asp.net

I'm trying to cache user controls and on some pages i want to cache single objects. There are multiple ways of implementing caching, and my head is breaking over it.
The way I see the caching options now:
You have the PartialCaching option which is set to cache the control for 30 minutes, and after that it clears itself... You have the varyByParam to identity the page by its querystring paramaters... or other vary options
But i just cant find an appropriate way to add caching to a 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 do HttpContext.Current.Cache.Insert(), which accepts a key on which you can destroy the caching item later by using remove... This can save objects in cache but can you use options like varyByParam?
My questions are burnt down to two:
Is there a way to clear the caching on specific user controls from the code? If yes, can this be done according to the varyby options?
How would the object caching respond to logged in users or anonymous users using Insert()?
EDIT:
I'm caching multiple things.... And I'm really flabbergasted in which choice to make referring to caching. Can the Cache.Insert be varied by Parameters?
The main problem is peopling editing things from the backend, which needs to trigger an event that reinstantiates or clears all caching items referring that object.

You can remove items from the output cache using the following.
HttpResponse.RemoveOutputCacheItem("/caching/CacheForever.aspx");
Now, this is only going to get you part of what you are looking for. This will remove ALL cache entries for that specific page. The MSDN documentation confirms the behavior.
As to your other question Cache.Insert() is a single cache store across the application, user identity is not considered.
Now I would also take a bigger look at what you are doing, it might make sense to only cache the actual data and then you can add/remove the specific items. However, if your .control really does take a lot of CPU etc to handle the display then the output cache idea works.

Using the System.Web.Caching.Cache class you can cache items and create dependencies for the items in the cache. If you're using a SQL server, then you can use the SqlCacheDependency class to clear items from your cache based on your database.
Otherwise you can create your own derivatives of the CacheDependency class which you can use to accomplish the same thing. I found this post that describes doing that.

Related

How to design a website when its content layout needs to be changed all the time?

Our team is building a new web site with ASP.NET. We plan to use a 3-tier architecture. The problem is that the controls shown on the web page need to be changed all the time according to the customer's requirements, which means adding a pair of label/textbox, or removing a pair of label/dropdownlist when the customer needs it. So the layout needs to be flexible and allow to easily add or remove controls, although it just shows some simple product messages like price, discount, tax, etc.
The previous version of the web site saved all the control information in a database, like control name, control type (textbox, label, dropdownlist), which page and panel it belongs to, etc. You can see there is a big performance hit because every time there is a request to this page, it needs to get all the required controls from the database and add them to the page manually, no matter whether the request is a postback or not.
We thought about adding the controls directly to the .aspx page, but in this case it will be difficult to change them later. We also considered holding all the controls' information in XML files, which may give a little performance advantage, but it still needs to render the controls all the time.
So this is the problem we have, to improve the app's performance and also meet the users' needs at the same time. Could anyone help me with any solutions or ideas?
PS: you can also ask questions if I didn't make it clear enough. Best regards.
This sounds like a good situation for User Controls. If all you're doing is toggling child-control visibility, then creating a user control with toggleable visibility properties should meet your needs. You can still use your backend to toggle visibility, but you'll only need to pull yes/no flags from the db instead of entire page schemas.
From an architectural standpoint, User Controls are great because they encourage modularity, code reuse, and lend themselves well to version control (UsercontrolV1.cs, UserControlsV2.cs, etc). The point on version control is especially great in cases where change requests require logic updates, or simply need to revert to a build that existed x iterations ago.
Now that is what i call a Flexible web-Application.
the controls shown on the web page need to be changed all the time
Who will change the controls? The client? Can you not just update the .aspx file and publish it to the server every time a control is requested to be changed?
but any way, its an interesting question. There is nothing else really that can be done except using a XML file.

Thoughts on how to implement paging for a detail page

I am curious as to what people would thing is the best way to implement this.
On my overview page, I have a data grid which is sortable and has custom filtering written for it. When you click on an item in the grid, it loads a separate page with details.
I would like to implement result paging through on the detail page keeping in the same order as the previous page data grid.
Now I've thought of a few ways to do this:
Change the link to be a postback and handle the sort order programatically
Sort filter and sort settings on application of the settings in the session
Pass the filter and sort settings on the query string to the detail page
None of these seem very clean to me, and option 3 is the only cogent option I've thought of.
Am I missing something? Is there a more elegant way to do this?
It is preferable to have url access to any details view that may be needed independently of the master.
Of course, you can have both postback and GET access, but begin with the url and querystring. I have had to fix numerous postback-only views to support direct urls, when the need has eventually arised.
If the filters and sort order are very complex and would require an elaborate querystring parameter scheme, I would think hard what is really needed for url access. Complex settings are best passed in a serializable .NET object in the Session or ViewState. You'll get .NET type safety and avoid error-prone querystring/form/cookie parsing. Session would be better if you need to pass these settings from another page of the application.

Cache Manager for ASP.NET

I would like to integrate a Cache Manager in an ASP.NET application. Basically I would like a page that would display what's in the cache and let me delete specific items or clear the whole cache. Ideally I would like as much information as possible, such as how long it's been in the cache for, the hit count, the size of the object, possibly see the object itself, etc. Of course I realize that some of this information might not be available from the default cache API.
I think it would be fairly easy to implement but I don't want to reinvent the wheel. I did a search and came across that one:
http://aspalliance.com/cachemanager/Screenshots.aspx
Just wondering if there are other options that I could compare.
Cheers
I use this code to view the cache data.
http://www.codeproject.com/KB/session/exploresessionandcache.aspx
Its not a cache manager, but its a good point to start.
You can use one of the following open source cache solution if you find it useful
http://csharp-source.net/open-source/cache-solutions
hope it helps

Is it possible to cache an asp page on the server side?

Let's assume you have a big complex index page, that shows news articles and stuff. It's not going to change very often. Can you cache it somehow on the serverside, so requests don't force to server to dynamically generate the entire page every time someone visits it? Or does ASP.NET do this automatically?
If so, how does it know if something has changed?
Yes you can, here is the declarative version of page caching, which will cache the page for 60 seconds:
You ask about changes, notice the VaryByParam part - you can, for example, ensure that there is one cached page for each parameter. You can even implement your own custom variation with VaryByCuston, which can be really powerful:
VaryByCustom
Any text that represents custom output caching requirements. If this attribute is given a value of browser, the cache is varied by browser name and major version information. If a custom string is entered, you must override the HttpApplication.GetVaryByCustomString
method in your application's Global.asax file.
Yes, caching exists, MSDN discusses it better than I can here. http://msdn.microsoft.com/en-us/library/06bh14hk.aspx

How can I view the contents of the ASP.NET OutputCache?

Is there any way that I can list the pages which are currently stored in the OutputCache?
Just a list of paths would do, but if there's a way to get more information about each item (expiry etc), then all the better.
As far as I remember Cache is a singleton and there is only one instance of it per app domain. OutputCache uses it too and it's nothing more than just a Response.Cache. So I think cached pages should be available through the Cache (Sorry, I can't check this at the moment). And the following articles should help you in this case:
http://www.codeproject.com/KB/session/exploresessionandcache.aspx
http://aspalliance.com/CacheManager/Default.aspx
Here is a little tool I wrote that will let you to view the contents of your Cache. You can also view the dependencies on a file and remove the cache.
https://github.com/azamsharp/WIYC
Here is another tool that displays the Usercontrol(Webforms) cache and am extending that to display the outputcache details as well
https://github.com/chandarmk/InternalCacheHandler

Resources