views:

39

answers:

3

I have a web application (MainApplication) where many of the pages contain a custom Web Control that looks for some content in a cache. If it can't find any data within the cache, then it goes out to a database for the content. After retrieving the content, the Control displays the content on the page.

There is a web application (CMS) in a subdirectory within the aforementioned web application. Users use this CMS to update the content pulled in by the MainApplication.

When a user updates some content using the CMS, I need the CMS to clear the relevant portion of the cache used by the MainApplication. The problem is that, as two different web applications, they can't simply interact with the same static cache object.

The ideal solution would be to somehow share an instance of a cache object between both web applications.

Failing that, what would be the best (performance-wise) way of communicating between the two web applications? Obviously, writing/reading to a database would defeat the purpose. I was thinking about a flat file?

Update

Thank you all for your help. Your wonderful answers actually gave me the right search terms to discover that this was a duplicate question (sorry!): http://stackoverflow.com/questions/2596730/cache-invalidation-between-two-web-applications

+1  A: 

One option that comes to my mind in such scenario is using Velocity distributed cache mechanism. Do read about it and give it a try if possible http://msdn.microsoft.com/en-us/magazine/dd861287.aspx

Subhash Dike
+1  A: 

We had the exact same setup in a previous project i worked on, where we had one ASP.NET Web Application (with MCMS Backing), and another ASP.NET Web Application to display data.

Completely different servers (same domain though).

However, when a "editor" updated content in the CMS application, the UI was automatically refreshed.

How? Glad you asked.

We stored the content in SQL Server, and used Replication. :)

The "frontend" Web Application would read the data from the database (which was replicated by the CMS system).

Now - we don't cache this data, because in the database, we actually stored the markup (the HTML) for the control. Therefore we dynamically re-rendered the HTML.

Why is that "defeating the purpose"?

You can't get one application to "invalidate" the cache on another application.

If you're going down this path, you need to consider a distributed caching engine (e.g Velocity).

RPM1984
After spending a couple hours setting up a WCF service to communicate between the two web applications, it turned out you were right all along. I just created a table that contains the volatile items in the cache. I thought this was defeating the purpose because I am using a call to the database to determine whether or not I should make a call to the database, but it turned out to be quite speedy. Thanks!
attack
@attack - exactly. The other thing is, use a seperate database for the CMS content. In other words, your CMS system would "write" to "CMSDatabaseA", this would replicate to "CMSDatabaseB", then the web app would read "CMSDatabaseB". Keep these DB's seperate from your regular data. Also, you might not even need replication, if the web sites are on the same box (ours were not, so we did need replication). You could just have the CMS write to the db, and the wep app read from the same DB. Only problem with that is deadlocks of course. Anyway, glad i could help.
RPM1984
A: 

In ASP.NET there is the notion of Cache Dependency. You can have a look here: http://www.codeproject.com/KB/web-cache/CachingDependencies.aspx or http://www.devx.com/dotnet/Article/27865/0/page/5.

There is also the Enterprise Library Caching Block available here that adds some feature to the standard stuff: http://msdn.microsoft.com/en-us/library/ff649093.aspx

Now, if you're running on .NET 4, there is a new System.Runtime.Caching namespace that you should definitely use: http://msdn.microsoft.com/en-us/library/system.runtime.caching.aspx

This article here "Caching in ASP.NET with the SqlCacheDependency Class" is quite interesting: http://msdn.microsoft.com/en-us/library/ms178604.aspx

Simon Mourier
It's `two seperate applications` - therefore two seperate application pools. CacheDependency is static for a particular application pool (unless you use distributed caching), like i and @Subhash Dike mentioned.
RPM1984
The "CacheDependency" notion does not presume anything about how the cache actually works, where the real data resides, etc. For example, SqlCacheDependency works on web farms with, say, one Sql server and many web servers, and yes, it's a distributed cache. If you don't want to use distributed cache, you may use for example Memory Mapped Files (there is direct support in .NET 4 http://blogs.msdn.com/b/salvapatuel/archive/2009/06/08/working-with-memory-mapped-files-in-net-4.aspx)
Simon Mourier