Sql – One Cache for various Applications

asp.netcachingsql server

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.

Best Answer

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