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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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. |
| Sponsored Links |
| |||
| 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! |
| |||
| 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! |
| |||
| 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 |
| |||
| 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” %> 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” %> 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; Code: myObject = (MyObject)Cache[“someKey”]; 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>
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |