ASP.NET 2.0: CacheKeyDependency

Published 08 August 05 10:07 AM | despos

Imagine you have a master/detail page with a GridView and a DetailsView. You select a record in the DetailsView, make some changes and save. How can you have the master element--the GridView--refresh to reflect the change?

You hook up the ItemUpdated event on the DetailsView and call DataBind on the GridView control

Simple and effective. It works most of the times, but as-of-my-experience-and-coding not always. So when isn't it working? Let's take it the other way around and list when it actually works.

  • When the GridView is bound to an enumerable data source object (i.e., DataTable) and you manage caching manually (or have no caching at all)
  • When the GridView is bound to a data source control and caching is disabled.
  • When the GridView is bound to a data source control, caching is enabled, but a database cache invalidation mechanism is set up on the table.

Again, when isn't it working? When the GridView is bound to a data source control, caching is enabled, and no database invalidation is in place. Note that this is exactly the most common situation (or the easiest you fall into) as it delineates when you simply add EnableCaching=true.

When caching is enabled on a data source control, it defaults to a time-based expiration policy. Moreover, the default cache duration is set to 0 which means an infinite time. Unless you also specify a nonzero duration (in seconds), the cached data won’t never get stale and you have no way to refresh the view other than binding to another data source. If you set a cache duration, the data will be refreshed when the timeout expires, not when the data really changes. If adding a SQL cache dependency is too much work (it is usually the kind of stuff that makes a DBA jumping for joy), you can rely on a manual invalidation mechanism implemented through the CacheKeyDependency property.

This property indicates a user-defined key dependency that is linked to all data cache objects created by the data source control. All cache objects are explicitly expired when the key is expired. By removing or modifying the key specified through the CacheKeyDependency  property you can programmatically invalidate the data cached by a data source control.

As I understand, the property should be used in the following way.

<asp:SqlDataSource id="SqlDataSource1" runat="server" CacheKeyDependency="MyTestKey" ... />

void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
    Cache[SqlDataSource1.CacheKeyDependency] = someData;
}
void OnItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
  Cache.Remove(SqlDataSource1.CacheKeyDependency);
  GridView1.DataBind();
}

However, it doesn't work for me. The cache contains proper information but it is apparently detached from the cache entries created by the SqlDataSource control.

Is there anybody out there who can provide more information and shed some light? I tried to track down  CacheKeyDependency using .NET Reflector, but also this approach proved unsuccessful.

Looking forward to hear something :)

Comments

# Sergey Pashkevich said on August 24, 2005 01:32 AM:

Hi.
The documentation in MSDN is definetely lacking.
Anyway, as it turns out, the data that you put in cache is not anything, but a CacheDependency object. When THAT object "invalidates", the DataSource cache will be invalidated too. You can depend the CacheDependency object on some files or keys in the systems, but in the simplest case, when you want to decide programmaticaly to invalidate, you would do such thing (replace an old CacheDependency with a new one).

Cache[SqlDataSource1.CacheKeyDependency] = new CacheDependency(new string[0]);

this could also be done in a PageLoad on initializing the stuff (i'm not sure whether it MUST be done).

# Pensamento Dot Net! said on September 4, 2006 02:37 PM:

Olá pessoal, Hoje descobri na web mais uma dica genial, como sempre um pequeno toque de sutiliza...

# Oleksandr said on May 24, 2007 03:04 PM:

Cache[SqlDataSource1.CacheKeyDependency] = new string[0];

will work too.

# Alex said on August 14, 2007 12:26 PM:

Please, look at the following article:

CacheKeyDependency code, that works:

ikosoftware.blogspot.com/.../aspnet-20-cachekeydependency.html

Leave a Comment

(required) 
(required) 
(optional)
(required)