Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MemoryCache Sharing Across Applications

I have been reading all over the place about the new MemoryCache class starting in .Net Framework 4.0. From what I've read, you can access the MemoryCache across different .Net applications. I am trying to share an object between an Asp.Net application and a standard windows forms .Net application. If I add the object to the MemoryCache in the .Net application, the Asp.Net application does not see it. Is there any way to accomplish this? Thank you for your time, it is greatly appreciated.

Windows Form App:

    Dim cache As ObjectCache = MemoryCache.Default
    Dim policy As New CacheItemPolicy()
    policy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(60)
    cache.Set("testcache", TestObj, policy)

Asp.Net App:

    Dim cache As ObjectCache = MemoryCache.Default
    If IsNothing(cache("testcache")) Then Response.Write("TestCache Is Nothing")

Thanks - Ryan

like image 436
wayofthefuture Avatar asked Mar 09 '13 19:03

wayofthefuture


3 Answers

No, that's not possible. MemoryCache is not a distributed caching solution. So it will only be available locally.

If you are looking for a distributed cache alternative you may want to look into AppFabric or Redis.

However, it does sound a bit like an odd architecture to want to share the cache that way. Maybe exposing a shared services layer, that both the asp.net and winforms consume, and have just the services implement the caching would seem more logical (take into account I actually know nothing about the problem you are trying to solve, so I could be wrong).

Caching is more commonly used for performance reasons, not as a way to share data among applications.

like image 82
Pablo Romeo Avatar answered Sep 20 '22 11:09

Pablo Romeo


MySQL memory tables are working great and having stellar performance. 20/30 inserts a second and only around 1% CPU load.

like image 38
wayofthefuture Avatar answered Sep 17 '22 11:09

wayofthefuture


I realize this advice is not timely, but for others reading this question, another possibility is Interprocess Communication (IPC) between the two programs. This way the two programs can exchange messages/data directly without going thru an intermediate.

https://msdn.microsoft.com/en-us/library/windows/desktop/aa365574(v=vs.85).aspx

From the documentation above, here are some of your options.

Clipboard COM Data Copy DDE File Mapping Mailslots Pipes RPC Windows Sockets

In your case, mapped memory files might be the best approach as it allows for a shared memory space between applications. Fundamentally, your MySql/Redis approach is probably not that different.

like image 31
Terry C Avatar answered Sep 21 '22 11:09

Terry C