Friday, June 17, 2005 #

The Microsoft Tech Ed 2005 Bloggers Contest

 

Tech Ed India 2005 had a grand opening and is already over in one of five cities, Bangalore. To view the dates for other cities, please check Microsoft Tech Ed 2005 India Registration

Tech Ed provides a very good opportunity to learn about the upcoming Technologies as well as to meet/interact with the Experts / Peers.

This time, there is more !!!

The Tech Ed Bloggers Contest is an opportunity for you to share your thoughts / opinion about Tech Ed. To know more please visit
Microsoft Tech Ed India 205

To participate in the contest you need to submit your Blog Posts to the Microsoft Community. Anyone interested in contributing their thoughts about Microsoft Tech Ed 2005 or associated technologies can add their blog to Tech Ed Bloggers.

Top 5 Entries from each city will win an award !!!

For participation and more information, please visit
Microsoft Tech Ed 2005 Bloggers Contest

Cheers and Happy Contesting !!!

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Friday, June 17, 2005 1:20 PM | Feedback (1)

ASP.NET 2.0 : New Caching Techniques - Part II

In the previous article, we saw the SQL Cache Notification and the benefits it provides for invalidating the Cache.

There are other methods which are very useful for enabling / validating cache. We will examine some of them as follows:-

Disk Output Cache
Though Output Cache serves the purpose of avoiding repeated requests to the Server, it cannot withstand Application restarts. Also, Cache is memory intensive and when memory reaches the isolation level, the Cache expires.

With disk output cache, ASP.NET 2.0 can save cached responses to disk in addition to keeping them in memory. When the cached response is removed from the output cache due to memory pressure, it still remains on disk allowing a much larger set of pages to be cached. In addition, disk cached pages survive application restarts, enabling the cache to remain warm for when the application comes back up and begins serving requests. The syntax for Disk Cache enabling in output directive is as follows:-

<%@ OutputCache Duration="3600" VaryByParam="none" DiskCacheable="true" %>

In addition, the disk cache is turned on in configuration and a per-application disk usage limit must be specified. It is important to always set application quota for disk cache as it can easily fill up the disk otherwise.


<?xml version="1.0"?>
<configuration>
<system.web>
<caching>
<outputCache>
<diskCache enabled="true" maxSizePerApp="2" />
</outputCache>
</caching>
</system.web>
</configuration>


In the above settings, the per-application disk usage limit is set to 2 megabytes, as specified in the maxSizePerApp attribute

Disk caching is turned on by default for all pages. To turn disk caching off, specify DiskCacheable=false in the <%@ OutputCache %>directive on the page.

Post Cache Substitution
In .NET 1.X versions if you want a dynamic section inside an Output Cached Page you have to either use Fragment Caching and split the page into Usercontrols and have the dynamic section as an Usercontrol without Caching. This is not always easy since you have break up the whole page into controls for achieving a simple functionality. You felt that right?

Now, you dont have to do that!!!

You have the Post Cache Substitution where you can delegate a call back to the Method which you specify using the Response.WriteSubstitution Method to make the particular section dynamic.

Lets say we have the following method which returns the Current date & time on a page.

static string GetCurrentDate(HttpContext context)
{
return DateTime.Now.ToString();
}


Then, in the ASPX Page's Output Directive, we will specify the Output Cache as follows:-

<%@ OutputCache Duration="60" VaryByParam = "none" %>

Now this ensures that the page will be cached for 60 sections.

Next, we will have the following Code in the Page:-


Time: <%= DateTime.Now.ToString() %>

Exact Time: <% Response.WriteSubstitution(new HttpResponseSubstitutionCallback(GetCurrentDate)); %>


The above two statements intially will display the same Time when you run the page for the first time.

However, when you make subsequent refreshes, you will see that, next to Time: you will find the original time and no chance until 60 seconds.

However, next to Exact Time: you will find that the exact time is displayed which implies that the section is not being Cached, though the other sections of the page are cached.

As you can see from the above code, the Response.WriteSubstitution callback executes on every request, and the result gets inserted into the cached response chain that is served from output cache.

This is a very useful mechanism, particularly if you have portions of your section to be maintained very dynamic.

You can achieve a similar functionality by using the substitution control, as explained in my
Earlier Article.

These are some of the new methods available in Whidbey to leverage the benefits of Dynamic Caching of your web pages.

Cheers and Happy Caching !!!

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Friday, June 17, 2005 12:46 PM | Feedback (5)

ASP.NET 2.0 : New Caching Techniques

 

Well, as explained in my previous post, Caching has been improved a lot in Whidbey thanks to the SQL Cache Notification and other new mechanisms which provide way to create robust applications leveraging the power of Caching to improve performance.

SQL Cache Notification
When you enable Output Cache or Fragment Cache for your page, the data gets cached for the specified duration, regardless of whether the data has changed in the database.

The New SQL cache invalidation enables you to make the cache entry dependent on the database, so the cache entry will only be cleared when data in the database is changed.

This way is very useful since you can create the dependency to your Database Table and then set the Cache such that, when the Data is updated, you can ensure that the Cache will get invalidated and subsequent requests gets the New Data. This is particularly useful in scenarios where you show time-sensitive reports such as a Railway Reservation Status or Stock Market Status.

To enable SQL Cache Notification, there are 2 methods.

1. Polling-based Invalidation
This method can be useful when we are targetting SQL Server 2000 / SQL Server 7.0 Database versions since in SQL Server 2005, we have the Notification-based Cache Validation which is even simpler.

The Polling-based Invalidation works by polling to check if a table has been updated since the page was cached.

To enable this, Polling must be enabled for the Database. To do that, the following command needs to be run from the Visual Studio Command Prommpt:-

aspnet_regsql.exe -S ".\SQLExpress" -E -d "pubs" -ed

Where "pubs" is the name of the Database to be enabled for Polling.

This command needs to be executed once for each Database for which we would like to enable Polling.

Next, Polling must be enabled for the Table that we would like to keep checking.

To do that, the following command needs to be run from the Visual Studio Command Prompt:-

aspnet_regsql.exe -S ".\SQLExpress" -E -d "pubs" -et -t "authors"

Where "pubs" is the Database name and "authors" is the name of the Database Table.

Finally, we need to register the notification in the Web.config file of the application as follows:-

<system.web>
<caching>
<sqlCacheDependency enabled="true" pollTime="1000" >
<databases>
<add name="PubsDB" connectionStringName="Pubs" />
</databases>
</sqlCacheDependency>
</caching>
</system.web>

The pollTime specifies the interval the application takes between consequent polling to the database.

Then, a SQL Dependency can be set at the Page level as follows:-

<%@ OutputCache Duration="999999" SqlDependency="Pubs:Authors" VaryByParam="none" %>

Or, directly for a DataSource, as follows:-

<asp:SqlDataSource EnableCaching="true" CacheDuration="Infinite" SqlCacheDependency="PubsDB:Authors" ... />

Thus, the SQL Dependency has been created for the Authors table in the Pubs Database and once the Data is changed in Authors Table, the Cache gets invalidated.

2. Sql Server 2005 Notification-based Cache Invalidation
SQL Server 2005 offers a simpler way to enable SQL Cache Dependency without requiring to enable Polling for Database/Table and registering in the Web.Config file.

A notification based dependency is configured on the OutputCache directive using the string CommandNotification. This value indicates to ASP.NET that a notification based dependency should be created for the page or datasource control.

To Enable Notification-based Cache for a page, the following is an example:-

<%@ OutputCache Duration="999999" SqlDependency="CommandNotification" VaryByParam="none" %>

To Enable for a DataSource,

<asp:SqlDataSource EnableCaching="true" SqlCacheDependency="CommandNotification" CacheDuration="Infinite" ... />

Whenever a command is issued to Sql Server 2005, ASP.NET and ADO.NET will automatically create a cache dependency that listens to change notifications sent from the Sql Server. As data is changed in Sql Server, these notifications will cause the cached queries to be invalidated on the web server. The next time a page or datasource control associated with the dependency is requested, the page or datasource control will be executed again as opposed to serving cached information.

There are other techniques such as the New Fragment Caching API, Disk Output Caching which we will explore in the future articles.

Cheers and Happy Caching !!!

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Friday, June 17, 2005 8:27 AM | Feedback (4)