Solved

Caching questions ...

Posted on 2004-09-03
9
687 Views
Last Modified: 2008-01-09
I'm trying to understand the best way to cache my site ... My pages are currently designed so that each has 2 user controls (banner & menu). The banner displays the current userid in a label server control. The userid also allows for a 'guest' user. The main bulk of each page shows various database views which will be different depending on the page (directed via the menu user control) and the userid (Guest users will always get the same results per page).

If I use VaryByCustom="browser" do I have to specify any code or variables anywhere else ? Also will it prevent me from using other caching methods or will it interwork with, say, VaryByControl ?

Can I use VaryByControl with the userid label in the banner (given that it is in a label which is read from a current session variable) ? If I can use it, will it cache just the banner or the whole page ? If I can't use this, should I use partial page caching on each individual datagrid etc or is there a better way ?

Also, (how) can I utilise Shared with any of these so that all my guest pages are cached together whilst maintaining the individual views of non-guest users?

0
Comment
Question by:Mamine
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 8

Accepted Solution

by:
daffodils earned 500 total points
ID: 11973968
Okay.. so you want to implement extensive caching in your application!

> If I use VaryByCustom="browser" do I have to specify any code or variables anywhere else ? Also will it prevent me from using other caching methods or will it interwork with, say, VaryByControl ?
No, you don't have to specify anything else.. "browser" is the default value for 'VaryByCustom' option.
Also, you can use multiple Caching techniques (provided it is efficient) on the same page / user control.

>>Can I use VaryByControl with the userid label in the banner (given that it is in a label which is read from a current session variable) ?
You can cache it, but you are already saving the information in a Session variable and this is user-specific information. What is use of caching some information specific only to one user at a time. This means if you have 1000 users, it will have 1000 cached copies of banner .. and these 1000 users might not log back in before the cache expires! That is saving too much for too little a thing! I would say leave it with the Sesison variable.

Now menu on the other hand is a different deal.. I am assuming that menu remains same for all users. This user control can definitely be cached [Fragment caching].
In your User control .aspx file.... add an @ OutputCache directive, set Duration="some time" VaryByCustom="browser" VaryByParam="none" (else if the menu user control depends on some control value then set VaryByControl to that value) id ). Also set.. Shared="true".. so that the same copy of menu controls can be used on multiple web forms.

>>If I can use it, will it cache just the banner or the whole page ? If I can't use this, should I use partial page caching on each individual datagrid etc or is there a better way ?

Partial caching applies to user controls.. For Data caching, you would need to use Cache object with dependencies (System.Web.Caching.Cache).

Generally for a Web Page,
If a page has static content.. it makes sense to apply Caching to whole page itself. (Duration, VaryByParam=none)
If content varies based on QueryString or POST parameters, set VaryByParam to those varuiables or *.

For User Controls,
Partial Caching [using PartialCaching attribute] or Fragment Cahing (using OutputCache directive or Response.Cache in code) is good, when the user control content is static, or varies on certain control values.

Also, remember.. you cannot call methods or properties of Cached objects.. its members are not accessible in code any more.
Too much caching also slows down your system.. there is something called as ""Turnover Rate"" (use Cache Performance counters)..Number of new entries plus the number of removed entries per second

You can use the counters to determine whether an application uses caching effectively. For example, if there are a lot of entries in the output cache, but the hit ratio is very small, your application might be caching stuff that are not frequently used. If the turnover rate is very high, your server might want to reduce the number of items you store in the cache, make the period they are stored in the cache longer, or increase the memory available on your server.

Hope that helps..
0
 

Author Comment

by:Mamine
ID: 11978115
WoW thanks for all that info! ... I'm still a bit woolly on a few things tho ...

> VaryByCustom="browser" - thx all explained

>> VaryByControl .... I think I understand how I could work the menu but will I lose the special effects of the visited/active links (eg if one guest user has visited a menu item, would all guests see that menu link as already visited ?).
Also is there a way to make it would work for the banner too if the user is a guest ('cos in spite of your comment, all guest users will see exactly the same banner with same userid in it, so it would be good to cache 1 copy for 1000 users).

>>>Partial caching - I presume here you mean the behind-code caching (e.g. Cache("dataObject")=dataObject). I think I understand this, but can you explain what you mean by "with dependencies" ?

If I cache a web form using VaryByParam will it also cache the embedded user controls or will they remain dynamic/uncached ?

Your warning re over-caching is heeded! Is this an area where I should maybe leave it all alone until it really is slow - this is my first web site so I was trying to put some good stuff in there, but maybe it would be better to leave well alone until performance is a problem ?

Many thanks for your help!
0
 
LVL 8

Expert Comment

by:daffodils
ID: 11998895
Hey, sorry boss.. I was out of town for the holidays.
Give me a minute here to get my bearings.. I will answer right back.

0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:Mamine
ID: 11998911
LOL np - glad you haven't deserted me :)
0
 
LVL 8

Expert Comment

by:daffodils
ID: 12000085
>>...lose the special effects of the visited/active links...........
Well that's true.. finally its your call.. performance vis-a-vis appearance.

>>>Also is there a way to make it would work for the banner too if the user is a guest.........
You could use VaryByControl here and set ...Shared="True".. (I am assuming there are no links, so appearance is not compromised).. but you are saving one copy across different Web Forms.

>>>what you mean by "with dependencies" ?....
With dependencies means.. you can update the Cache when Data changes. For Cache.Add or Cache.Insert, a dependency parameter(accepts a CacheDependency object) identifies the file, folder, or set of files to watch for changes.

Cache.Add Method
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebCachingCacheClassAddTopic.asp

The dependency can be set by CacheDependency Class
OR  by Response object's dependency methods like
AddCacheItemDependencies --> Makes the validity of a cached item dependent on other items in the cache.
AddCacheItemDependency --> Makes the validity of a cached item dependent on another item in the cache.
AddFileDependencies --> Adds a group of file names to collection of file names on which current response is dependent.
AddFileDependency --> Adds a single file name to collection of file names on which current response is dependent.

Here are the MSDN reference articles..

CacheDependency Class...
Tracks cache dependencies, which can be files, directories, or keys to other objects in your application's Cache. This class cannot be inherited.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebcachingcachedependencyclasstopic.asp

HttpResponse.AddCacheItemDependency Method
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebhttpresponseclassaddcacheitemdependencytopic.asp

NOTE--> If you don't want to use dependencies.. pass null as the dependency parameter in the Cache.Add method.. Add some expiration time ... Use the CacheItemRemovedCallback onRemoveCallback method to do your custom refresh based on Item #

>> If I cache a web form using VaryByParam..
If a Web Form has user controls, then Fragment Cahcing is a much better approach.. since Caching caches just about everything on the form user control etc.
As I said above.... Generally for a Web Page,
If a page has static content.. it makes sense to apply Caching to whole page itself. (Duration, VaryByParam=none)
If content varies based on QueryString or POST parameters, set VaryByParam to those variables or *.

Else use, Fragment Caching.. that is use make User Controls out of static and dynamic content and handle caching of each individually.
 
>>Your warning re over-caching is heeded!....
Actually it makes better sense to take care of things at design time.. that would be the correct approach. Use a simple prototype with caching in place and then load test the proto app.. you would get an idea as to how much Caching is really required. But, that takes really a lot of time.. so you can consider some good practices like Caching DataSets, Caching User Controls and then go on to see how they can be improved as the application takes a definitive shape.

I am beginning to sermonize on the issue.. sorry about that :)).  Hope this info helps..
MSDN is a very good place to check basic Caching concepts.
0
 

Author Comment

by:Mamine
ID: 12004932
Thanks again for the info. daffodils.  Am still reading the detail of all your links but one Q I'm still confused about:

>If I cache a web form using VaryByParam will it also cache the embedded user controls or will they remain dynamic/uncached ?

You mentioned what was best approach (fragment caching) but I'm just trying to understand what actually happens ... am I right in interpreting that IF (which I won't) I were to use VaryByParam caching on a web form with embedded user controls then this would cache the user controls according to the Param i.e. it would ignore/override any caching in the user control itself ?

PS: np with the sermon - tis useful. I find MSDN very dry, long-winded and quite hard to understand - experts, such as yourself, are much easier to understand :)
0
 
LVL 8

Expert Comment

by:daffodils
ID: 12006781
>>If I cache a web form using VaryByParam will it also cache the embedded user controls or will they remain dynamic/uncached ?

Yeah you are right there. When Caching is enabled at both Web Form and User Control Levels, the cache settings interact as:
-- The Cache Location is determined by the Web Form setting (Location settings on User Control have no affect).
-- If the Web Form's cache duration is longer than the User COntrol's, both the Web Form response and the user control response will expire using the Web Form setting.

>> I find MSDN very dry, long-winded and quite hard to understand
LOL :)). So I am not the only one who thinks that MSDN is a whirlpool... glad to hear that :))
0
 

Author Comment

by:Mamine
ID: 12007492
Brilliant! Thanks for clearing that up. You've been really helpful!
0
 
LVL 8

Expert Comment

by:daffodils
ID: 12007560
Sure.. Glad I could help.
Best of Luck for your 'first web site' :))!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question