0

I have a very large c# website that is broken down into separate applications and separate app pools running on the same server. (background) I am using the .net 4.6.x framework. ASP.NET WebForms & MVC. Some application pools still use "Classic" and the MVC applications use "Integrated".

I want to share certain cached items across the different applications. I do not want each application to have its own copy of the cache. I want just one shared instance of the cache.

Here is the heart of my question...

How do I share a single instance of MemeroyCache across all my application pools on the same server? If one application pool puts something in the cache I want to be able to retrieve it from the other application / pools. Is this possible or do I need to look at a different technology. Any examples or suggestions would be great. :)

Thank you in advance!

Dennis Fazekas
  • 479
  • 4
  • 10

1 Answers1

1

You could create a separate Web Api project and host your implementation of the MemoryCache there. Then expose an api that gets / sets items from the cache that all of your apps can use.

Though, there are caveats with this approach. For example, if you use MemoryCache.Default then your cache lives as long as your application pool lives - the default application pool idle time-out is 20 minutes which is less than 1 hour. What I'm getting to is that I think you will have to keep a lot of the App Pool settings in sync and I believe this will lead to issues.

A better approach is to implement a distributed cache. I recommend using Redis and the StackExchange.Redis client. There are other alternatives such as memcached.

William Xifaras
  • 5,212
  • 2
  • 19
  • 21
  • Yeah I was actually considering the web api approach. For some reason I thought MemoryCache would hang around after an app pool recycles. I think the better approach would a distributed cache like Redis as you mentioned. Thank you for responding! – Dennis Fazekas Aug 20 '16 at 00:23