When to Use Caching


It's very difficult to provide concrete rules for when caching should be used, because it is very application-dependent. In general, any form of caching will have a benefit as long as your server has sufficient memory and if you don't try to cache everything. Perhaps the best rule to stick with is to cache data that is frequently used and that costs a lot (in performance terms), which is often files and databases.

Listing 6.23. Using Cache Removal Callbacks

static HttpContext _ctx = null; void Application_Start(object sender, EventArgs e) {   _ctx = Context;   StringCollection words = Words.ReadWords();   _ctx.Cache.Insert("Words", words,     new CacheDependency(Words.WordsFile),     Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,     CacheItemPriority.Normal,     new CacheItemRemovedCallback(RefreshWords)); } static void RefreshWords(string key, object item,                          CacheItemRemovedReason reason) {   StringCollection words = Words.ReadWords();   _ctx.Cache.Insert("Words", words,     new CacheDependency(Words.WordsFile),     Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,     CacheItemPriority.Normal,     new CacheItemRemovedCallback(RefreshWords)); }

Since there are different types of caching, Table 6.8 gives a brief look at the technique use, where the cached data is visible, and the size of data the technique is useful for.

Table 6.8. When to Use Caching

Technique

Visibility

Optimum Data Size

Page ViewState

Page

Small

Data source controls

Page

Large

Page output caching

Page

Large

ASPSession

User

Small/Medium

ASPApplication

Application

Medium

ASP.NETCache

Application

Large

Disk file

Global

Large


You can see that because the cached data is stored in different places, its visibility is different, and this might dictate when the technique is used. When you investigate caching, it is important that you test the performance before and after caching has been done. You may think that a page is faster because it, or the data it relies upon, is being cached, but what if you have 500 simultaneous users, or 500 requests a second? How will the server cope with a large load? How can you fine-tune and find the optimum amount of data to cache, or the optimum time to cache it for, if you don't stress-test the application?



ASP. NET 2.0 Illustrated
ASP.NET 2.0 Illustrated
ISBN: 0321418344
EAN: 2147483647
Year: 2006
Pages: 147

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net