Sunday, 23 December 2012 13:55

asp.net caching

Written by 
Rate this item
(0 votes)

Asp.net caching helps you to create high performance applications.
you can cache objects like DataSets,or your created objects.you can
also do page level caching if you used user controls in the page
if your aspx page includes lots of work.

If you regenerating info on every page request,then cache may be a
very good idea.but before going to cache,you have to think about it's
effectiveness in application.
there are two kinds of asp.net caching.one is-
output caching the other is-object level or data caching

one problem programmers or developers face in case of data caching is
that in some instances programmers require to bring back both non-cached
and cached data.as a example,the application user end might want to see
cached data,on the other hand the administrator site might want to
view non-cached data.

following C# code is a way to get the best of both.

public ArrayList Getviewers(bool CacheAllowing)
{
// this directive must force variable CacheAllowing to
// be false while in 'debug mode' in Visual Studios.NET
#if DEBUG
CacheAllowing = false;
#endif
string stringCacheKey = "Users-GetUsers";
// cached?
if(CacheAllowing && ( null != HttpContext.Current.Cache[stringCacheKey]) )
return (ArrayList)HttpContext.Current.Cache[stringCacheKey];
// code to fetch users from database
ArrayList alUsers = null;
allUsers = Users.GetUsers();
// cache it if 'allowed'
if ((CacheAllowing) && (HttpContext.Current.Cache[stringCacheKey] == null))
HttpContext.Current.Cache.Insert(stringCacheKey, alUsers, null, DateTime.
Now.AddMinutes(5), TimeSpan.Zero);
return allUsers;
}

in the above code,the method has a parameter value of boolean type which
passed in the time of calling the method.to get live data you pass in 'false',
but you pass 'true' for cached data.in the visual studio.net if you switch to
"release",you must get the data cached(guessing that you passed in 'true')

summary:
caching is very useful when you builds better scalable web applications.caching
can also give a better techniques for you to employ while you are coding
in c# language.

Read 1533 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.
Login to post comments