IT Community - Software Programming, Web Development and Technical Support

Explain how Caching in Asp.net 2.0 is different from Caching in Asp.net 1.1?

This is a discussion on Explain how Caching in Asp.net 2.0 is different from Caching in Asp.net 1.1? within the ASP and ASP.NET Programming forums, part of the Web Development category; ASP.NET 2.0 also now includes automatic database server cache invalidation. This powerful and easy-to-use feature allows ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > ASP and ASP.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 09-11-2007, 08:30 AM
Arun Arun is offline
D-Web Trainee
 
Join Date: Mar 2007
Posts: 43
Arun is on a distinguished road
Post Explain how Caching in Asp.net 2.0 is different from Caching in Asp.net 1.1?

ASP.NET 2.0 also now includes automatic database server cache invalidation. This powerful and easy-to-use feature allows developers to aggressively output cache database-driven page and partial page content within a site and have ASP.NET automatically invalidate these cache entries and refresh the content whenever the back-end database changes. Developers can now safely cache time-critical content for long periods without worrying about serving visitors stale data.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-19-2007, 12:10 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Wink Re: Explain how Caching in Asp.net 2.0 is different from Caching in Asp.net 1.1?

Hi,

Here is a simple code showing file dependency in cache using vb.net.

Code:
Partial Class Default_aspx

Public Sub displayAnnouncement()
Dim announcement As String
If Cache(“announcement”) Is Nothing Then
Dim file As New _
System.IO.StreamReader _
(Server.MapPath(“announcement.txt”))
announcement = file.ReadToEnd
file.Close()
Dim depends As New _
System.Web.Caching.CacheDependency _
(Server.MapPath(“announcement.txt”))
Cache.Insert(“announcement”, announcement, depends)
End If
Response.Write(CType(Cache(“announcement”), String))
End Sub

Private Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Init
displayAnnouncement()
End Sub

End Class

Above given method displayAnnouncement() displays banner text from Announcement.txt file which is lying in application path of the web directory. Above method first checks whether the Cache object is nothing, if the cache object is nothing then it moves further to load the cache data from the file. Whenever the file data changes the cache object is removed and set to nothing.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-19-2007, 08:03 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 325
amansundar is on a distinguished road
Question Re: Explain how Caching in Asp.net 2.0 is different from Caching in Asp.net 1.1?

Can u explain the types of cache using in asp.net?
__________________
cheers
Aman
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-19-2007, 08:12 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Cool Re: Explain how Caching in Asp.net 2.0 is different from Caching in Asp.net 1.1?

ASP.NET caching enables you to create high-performance web applications. You can cache individual objects such as DataSets, and you can also cache objects that you have created yourself. If you have a ASPX page that contains lots of work behind the scenes, you can also do page level caching, or sections of a page if you have user controls inside that page.

When to use caching

If you are recreating information on each page request, caching might be a good idea. But before you go cache-happy, think about how this will effect your application. For example, a news related site might not want to cache its front page for longer than 1 minute, while the actual article can be cached for much longer.

So to summarize, ASP.NET caching is of two types:

Output Caching
Object Level or Data Caching

One of the problems that developers face when data caching is that in many instances they require to retrieve both fresh (non-cached) and cached data. For example, on the user end of the application you might want to display cached data, while at the administrative side you want to view non-cached data.


Source Code

The following C# code snippet is a technique to get the best of both worlds. I personally use this all the time in my development.



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

Explanation

So as you can see, the method has a parameter of type bool (true/false) that gets passed in while you’re calling the method. If you want to get live data, you pass in FALSE, for cached data you pass in TRUE. As an added bonus, you can add the #if DEBUG directive to your code to allow for non-cached data to be viewed while your coding your application in debug mode. If you switch to ‘release’ in VS.NET you will get cached data (assuming you passed in TRUE).
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-19-2007, 08:21 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 325
amansundar is on a distinguished road
Exclamation Re: Explain how Caching in Asp.net 2.0 is different from Caching in Asp.net 1.1?

hey S.Vinothkumar!

what about Fragment Caching AND Cache Configuration ?

How we use the following caching in asp.net?

Output Caching
Fragment Caching
Data Caching
Cache Configuration


thnx in advance!.
__________________
cheers
Aman
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 09-19-2007, 08:34 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Wink Re: Explain how Caching in Asp.net 2.0 is different from Caching in Asp.net 1.1?

Output Caching

Performance is improved in an ASP.NET application by caching the rendered markup and serving the cached version of the markup until the cache expires. For example, if you have a page that displays user information from a database, caching will help improve performance by serving the page from memory instead of making a connection to the database on each page request.

You can cache a page by using the OutputCache API or simply by using the @OutputCache directive.

Code:
<%@ Page Language=”C#” %>
<%@ OutputCache Duration=”15” VaryByParam=”none” %>
The above cache directive will cache the page for 15 minutes.

So output caching is great for when you want to cache an entire page.

Fragment Caching

In many situations caching the entire page just isn’t going to cut it, for some reason or another you require specific sections of the page to display live information. One way to improve performance is to analyze your page and identify objects that require a substantial overhead to run. You can build a list of these objects that are expensive to run, and then cache them for a period of time using fragment caching.

For example, say your page default.aspx consists of three user controls. After looking over the code, you identified that you can cache one of them. You can simply add the caching directive to the top of the user control:

Code:
<%@ Control %>
<%@ OutputCache Duration=”5” VaryByParam=”none” %>
Now keep in mind that the actual page that contains the control is not cached, only the user control. This means that the default.aspx page will be rendered each and every page request, but the user control is only ‘run’ every 15 minutes.

Data Caching

So we know that we can cache an entire page, or a fragment of a page by caching down to the user control level. Wouldn’t it be great if we could cache down to the object level? The good news is you can with ASP.NET Data caching.

The cache consists of a dictionary collection that is private to each application in memory. To insert items in the cache simply provide the collection with a unique name:

Code:
Cache[“someKey”] = myObject;
Retrieving the object form the cache:

Code:
myObject = (MyObject)Cache[“someKey”];
It is a good time to point out that you should always remember to check for null, and be sure to caste to your datatype.

Cache Configuration

If you are familiar with how caching worked in ASP.NET 1.0, you realize that managing all the cache directions for all your pages could potentially get out of hand. ASP.NET 2.0 introduces Cache Profiles that helps you centrally manage your cache. Cache settings can be inherited by your pages, and overridden if required by using the OutputCache directive.

The page directive looks pretty much the same, expect this time it references a cache profile that you defined in your web.config file.

Code:
<%@ Page Language=”C#” %>
<%@ OutputCache CacheProfile=”myCacheProfile1” VaryByParam=”none” %>

<?xml version="1.0"?>
<configuration>
    <system.web>
        <caching>
            <outputCacheSettings>
                <outputCacheProfiles>
                    <add name=" myCacheProfile1" duration="60" />
                </outputCacheProfiles>
            </outputCacheSettings>
        </caching>    
    </system.web>
</configuration>
So each and every page that references the ‘myCacheProfile1’ can be centrally managed in the web.config, this means that any changes to the cache settings in the web.config file will be automatically changed on all your referenced pages.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 09-19-2007, 10:15 PM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Smile Re: Explain how Caching in Asp.net 2.0 is different from Caching in Asp.net 1.1?

Hi,
Can we insert an Item into the Cache in asp.net 2.0?.. I need to know the insert functionalities in Cache.. Can any one help me?...
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
What are the Types of caching in dot net 2005? Archer ASP and ASP.NET Programming 12 08-18-2007 03:00 AM
Caching in php abhilashdas PHP Programming 1 08-06-2007 11:42 PM
How do we prevent browser from caching output of my JSP pages? oxygen Java Server Pages (JSP) 1 07-26-2007 04:45 AM
How caching will help to improve the performance of asp .net application? kingmaker ASP and ASP.NET Programming 1 07-20-2007 06:32 AM
ASP Caching nhoj ASP and ASP.NET Programming 0 04-09-2007 09:12 AM


All times are GMT -7. The time now is 08:40 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0