简体   繁体   中英

ASP.NET cleanest way to invalidate cache across applications

I have two ASP.NET applications running on the same server and sharing the same database. One is the front-end, developed with MVC, that caches data to avoid database calls to retrieve the same objects. The other is the back-end, developed with WebForms, that is used to manage CRUD operations.

I'd like to invalidate the front-end cache when a back-end operation occur. I don't need a refined mechanism... back-end will be used only sporadically and could invalidate ALL cached objects.

I've come across some solutions, but they're not very clean solutions... like, putting a flag on a db settings table, using a shared configuration file, calling a front-end web service from the back-end application. Every solution needs to be applied every time a front-end page is called, so I need it to be less resource consuming as possibile.

I don't want to use memcached or AppFabric or similar 'cause I think they're overkill for my basic needs.

Thank you very much!

You can just make an action that will invalidate cache. You can pass it a secret token to check if the request comes from your other web application for security.

So it will look like this:

public ActionResult Invalidate(string key)
{
    if (key == ConfigurationManager.AppSettings["ApplicationSecurityKey"])
    {
        _cacheService.Invalidate();
        return Content("ok");
    }
    return null;
}

In both web.config files of both projects you will have:

<appSettings>
    <add key="ApplicationSecurityKey" value="SomeVerySecureValue" />
</appsettings>

And you can call it from your other web application like this:

WebClient client = new WebClient();               
client.QueryString.Add("key", ConfigurationManager.AppSettings["ApplicationSecurityKey"]);
string response = client.DownloadString(url)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM