Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 20, 2012 09:21 AM by Mamba Dai - MSFT
Apr 16, 2012 12:03 PM|LINK
I was wondering wich would be the best approach to have a granular and programmatic caching feature.
I would like to have an admin page where, for each web page, the admin can define for how long to cache the page and where should it be persisted (inmemory, disk, other).
Also it would be nice to be able to serve cached contents for anonymous users and maybe serve real time rendered content for logged in users or for particular roles.
So the points to solve are:
- Defining the cache time for output cache (of the whole page) in a programmatic way.
Maybe this can be achieved with Response.Cache in code behind, retrieving the value from the DB/Application/Cache
- Choosing the persist storage.
I have no clue on how to obtain this. Even if I have several cache providers defined in the web.config I don't know how to tell to output cache wich one to use programmatically, based on the page URL, so that an admin could choose this from an admin
Storing path and config in the DB/Application/Cache is trivial, but how to choose the provider at runtime based on those data?
- Serving cached pages to anonymous and real-time pages to logged user.
After lot of googling I found a very nice hint by Scott Hanselman on how to obtain this
http://www.hanselman.com/blog/AdvancedASPNETCachingAndAddValidationCallBack.aspx and maybe this is the right direction.
I'm totally new to asp.net so I really need practical suggestions on specific subjects like this one.
Thanks in advance
Apr 18, 2012 06:12 AM|LINK
Even thought your quesiton is not so clear. I guess you just want to konw ASP.NET Cache. As the reference you provided, it's a output cache, one type of ASP.NET Cache. There are main two types of ASP.NET Cache: output cache and applicaiton data cache. The
output cache is responsible to cache page/usercontrol and the second cache you can use to store some arbitrary objects programmatically, such as saving the data of database into memory. For persisting storage, apart from cache, the session/application may
be your alternative.
I don't want to say more, instead please check this document about ASP.NET Cache which show all aspects on cache. Hope it helps!
Apr 18, 2012 01:41 PM|LINK
Thank you for your kind answer.
I'm sorry if my post was not clear enough, my english it's bad my fault.
I'm would like to have some suggestion on how to implement programmatic caching strategies that let you define on-the-fly, based on some settings, how long should a page be cached, where should it be cached (in-proc, file, db, any other provider) and as
a last point a way to serve cached pages only for some roles.
One common example of this last point is that you would want to serve a cached version of your home page to anonymous users and maybe serve a real-time rendered home page to logged in users wich would have additional UI functionalities in the home page.
Using a sort of "conditional" output cache strategy would surely perform better than a fragment cache strategy (or post-cache strategy when the real-time stuff to render goes far beyond a simple "Hallo username")
I hope this is more clear now.
Apr 19, 2012 06:25 AM|LINK
Ok, now I understand what you want to konw exactly.
Actually, if you check the reference above I provided you also find your idea about your questions.
Never mind, I will explain them:
how long should a page be cached
For this question, obviously, it depends upon your practice requirement. To configure the expiration of cache page, just need to set the duration property of output elemet on your web page you want to cache:
<%@ OutputCache Duration="60" VaryByParam="None" %>
More information about setting expiration values of cache page please check this:
where should it be cached (in-proc, file, db, any other provider)
Basically, the asp.net cache saves the cache page or cache data into your memory(in-proc), not file database. But you can create your custom cache provider:
as a last point a way to serve cached pages only for some roles.
For this question, I check your explanation carefully. In a nutshell, I guess you want to different cache page based on the user identity(anonymous user or authenticated/login user). You mentioned there are some different sections for login user compare
to anonymous user. That means you need to use partial page cache. Partial page cache there are two mechanisms Portion-cache and Post-Cache. The Post-Cache may address your requrirement:
Post-cache substitution is the opposite. The page as a whole is cached, but fragments within the page are dynamic. For example, if you create a page that is static for set periods of time, you can set the entire page to be cached. If you added a
Label control to the page that displayed the user's name, the
Label would stay the same for each page refresh and each user, showing the name of the user who requested that page before it was cached. However, with post-cache substitution, you can configure the page to be cached, but mark individual sections of
the page as not cacheable. In this case, you could add the Label controls to a non-cacheable section and they would be dynamically created for each user and page request. For more information, see
Caching Portions of an ASP.NET Page.
BTW, I hope you can do some research about ASP.NET Cache, it's really helpful to you:
Apr 19, 2012 01:11 PM|LINK
Thank you again for your time and the resources you pointed out.
I have read them already and read many books on the subject. I already know the things you pointed out. What I'm missing is how to set those parameters programmatically (at run time if you prefer) and not in a declarative way.
What I don't know is how to change the cache provider at run time to choose if the same default.aspx page has to be stored in-proc or on the hard disk. How to change the cache duration from 60 seconds to 2 seconds at run time.
What I'm missing is how to change all the settings programmatically and not in a declarative way, so that they can be changed without touching the code again. This is a granular way of adjusting some caching parameters based on admin preferences, wich on
turn are based on website traffic or specific pages traffic and so on
Apr 20, 2012 09:21 AM|LINK
if the same default.aspx page has to be stored in-proc or on the hard disk.
I don't understand why you may stored the cached page on the hard disk, maybe you just give a sample to explain how to chang the cache provider. If you cache the page on the hard dick, still need to use IO to read it, that may hit the performance.
What I'm missing is how to set those parameters programmatically (at run time if you prefer) and not in a declarative way.
You can surely change the parameters programmatically like this:
This is a granular way of adjusting some caching parameters based on admin preferences, wich on turn are based on website traffic or specific pages traffic and so on
I'm afraid you can't implement this functionality currently. If you host your web application on IIS, you also can via the output cacheing of IIS to configure your web application anytime.