views:

437

answers:

3

hi,

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!

vice-versa

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.

+2  A: 

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.

http://www.google.com/search?q=distributed+caching

Regards,

Jochen

Jochen
+7  A: 

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

Scott Hanselman
Props to Scott, but velocity isn't ready for anyone's production environment.
jro
I hear what you're saying, but Velocity is in production all over. That said, there's also memcached, and other 3rd party solutions.
Scott Hanselman
A: 

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.

RocketsLee