Solved

Caching questions ...

Posted on 2004-09-03
9
681 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
  • 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
 

Author Comment

by:Mamine
ID: 11998911
LOL np - glad you haven't deserted me :)
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
call function 8 40
DLL in ASP.NET 20 40
Automatic sizing of HTML.TextAreaFor based on content 1 20
JavaScript error 1 33
AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now