Comanighttrain
Comanighttrain

Reputation: 247

How to clear the cache of an asp.net application from another asp.net application on the same server?

Got a bit of an odd problem. Here goes:

I have two ASP.NET applications: A web app and a web service app.

Information arriving via the webservice effects data in the database used by the web app.

One particular bit of data controls items in a drop down menu - when the data is altered in the app it can call:

HttpContext.Current.Cache.Remove

but I now need to clear the cache in the web service as i can recieve messages which update that information.

Can anyone recommend a way of doing this?

Upvotes: 1

Views: 2275

Answers (4)

Ryan Teh
Ryan Teh

Reputation: 549

You can try SQL Dependency. It will trigger an event when the table you have subscribed has any changes.

https://www.codeproject.com/Articles/12335/Using-SqlDependency-for-data-change-events

Upvotes: 0

quarksoup
quarksoup

Reputation: 129

Cache invalidation can be hard. Off the top of my head I can think of 3 solutions of varying complexity which may or may not work for you.

First, you could write a web service for the web app that the web service app calls to invalidate the cache. This is probably the hardest.

Second, you could have the web service app write a "dirty" flag in the database that the web app could check before it renders the drop down menu. This is the route I would go.

Third, you could simply stop caching that particular data.

Upvotes: 5

Kinexus
Kinexus

Reputation: 12904

Implement the cache with an expiry time.

Cache.Insert("DSN", connectionString, null, DateTime.Now.AddMinutes(2), Cache.NoSlidingExpiration);

Cache.Insert Method

Upvotes: 0

Jonathan
Jonathan

Reputation: 26619

You could have a web method whose sole purpose is to clear the cache.

var webRequest = HttpWebRequest.Create(clearCacheURL);
var webResponse = webRequest.GetResponse();

// receive the response and return it as function result
var sr = new System.IO.StreamReader(webResponse.GetResponseStream());
var result = sr.ReadToEnd();

Upvotes: 3

Related Questions