Tutorial :One Cache for various Applications?


i have two applications - one is an asp.net website and the other is a windows service.

both applications are referencing my business layer (library project), which itself is referencing my data access layer (library project) that itself uses the enterprise library data application block to receive the data form a sql server 2005 database.

currently i use the System.Web.Caching.Cache object in my BL to cache some of my objects:

public static System.Web.Caching.Cache Cache      {          get          {              if(cache == null)              {                  if (System.Web.HttpContext.Current != null)                  {                      //asp.net                      cache = System.Web.HttpContext.Current.Cache;                  }                  else                  {                      //windows service                      cache = System.Web.HttpRuntime.Cache;                  }              }              return cache;          }      }  

as both applications are running on their own - they both are using a separate Cache object on their own - and this is the problem actually:

if i change a object in the asp.net website and save it to the DB. the object to this cache key is removed from the cache - the asp.net application's cache! that's fine.

but the windows service's Cache becomes stale!


is it possible that both applications are using the same Cache? One Cache for both applications?

the only option i have i think about is that i will have to use SqlDependency with

Sql Server 2005 Notification-based Cache Invalidation

is there any other way?

EDIT: i just found http://www.codeplex.com/SharedCache. i will give it a try. as because velocity will be in release candidate status until mid 2009.


You should take a significant look at Microsoft's new free distributed cache "Velocity."

Here's a podcast I did on the subject: http://www.hanselman.com/blog/HanselminutesPodcast116DistributedCachingWithMicrosoftsVelocity.aspx

Here's details on MSDN: http://msdn.microsoft.com/en-us/data/cc655792.aspx

Here's downloads and samples: http://code.msdn.microsoft.com/velocity


I think what you need is a distributed cache system like memcached. But there are more, just do a quick Google and you will find what you are looking for.





Cache is only good per App Domain. Apparently you can not share same cache between web app and web service. So .. you have to make 2 caches in sync by various ways. Adding cache dependency object into your caches , once data has been changed , it would invalid both cache items and reload the object into caches. You can either use Polling or QN (much better performance) as your cache dependency mechanism.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »