6

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

wayofthefuture
  • 8,339
  • 7
  • 36
  • 53

4 Answers4

6

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.

Pablo Romeo
  • 11,298
  • 2
  • 30
  • 58
  • Thank You Pablo. Which one of those programs do you think would be easier for a novice programmer? Your time is appreciated. – wayofthefuture Mar 09 '13 at 20:03
  • Basically, I am reading udp data with a windows forms application. The asp.net web application uses this udp data. The data is about 100 messages a minute. To avoid delays with users of the asp.net web application pulling the data through an odbc connection, I was hoping that sharing the memory would make the asp.net application respond much quicker as the app must pull large chunks of the database at one time. Thanks! – wayofthefuture Mar 09 '13 at 20:10
  • Oh, I see. Well no, you can't share memory between different machines like this. Ultimately, the actual data needs to be transferred from the winforms app to the server. One thing you can do, is create a WCF service on the asp.net application. The winforms application calls that service once a minute, with those 100 messages, and within the service, you can chose to either add those to the local cache, store them in the DB, or both. – Pablo Romeo Mar 11 '13 at 14:02
  • Interesting. I did some research into Redis and learned a lot of stuff. Apparently MySQL has a memory table engine that's accessible from both applications. I have it running and receiving about 10-20 insert statements per seconds, and it only consumes 1% CPU. On the other side the entire database is selected, about 1000 rows, and the load time is around 50ms. I was very impressed by the performance. Initially, I thought that it would not be able to handle 10-20 inserts per second, but the memory engine table has definitely performed. Have you had any experience with these memory tables? – wayofthefuture Mar 13 '13 at 03:48
  • Oh, interesting! No I've never used mysql's memory tables, i'll read up on that, thanks! :D. Now, one other hint: if the data you are sharing is volatile, meaning, there's no need to store it long-term, but just for real-time visualization, you can probably avoid the storage, the data access layer, and all that, altogether, and just send the data to the server and have it keep it in memory through static state (or Redis if you deploy a farm of web servers). That way you should be able to handle over hundreds of messages per second, and reading it would be instant. – Pablo Romeo Mar 13 '13 at 04:24
  • So you mean a wcf service? I have to be able to access the data from both a windows form app and an asp.net app. Maybe I could use a ConcurrentDictionary? – wayofthefuture Mar 13 '13 at 08:27
  • Yes, possibly a wcf service. Well, but the winform app already has the data, it's the one that collected it, isn't it? Or you need to consolidate the info from multiple different winform apps on different machines? – Pablo Romeo Mar 15 '13 at 14:16
1

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

wayofthefuture
  • 8,339
  • 7
  • 36
  • 53
1

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.

Terry C
  • 79
  • 4
0

if you are interested in : single server multiple applications - memory share you should consider create web api at the same node and consume it (simple key value API) and use simple Memory Cache provided by .net framework

multiple servers - memory share free $$ solution

  1. you can use "Distributed SQL Server Cache", allows the distributed cache to use a SQL Server database as its backing store. To create a SQL Server cached item table in a SQL Server instance, you can use the sql-cache tool. The tool creates a table with the name and schema that you specify.

  2. if you have a cluster of nodes you can use NCache it is an open source in-memory distributed cache developed natively in .NET and .NET Core. NCache works both locally and configured as a distributed cache cluster for an ASP.NET Core app running in Azure or on other hosting platforms.

solutions that require paying money: NCache mentioned previously, there's Professional and Enterprise solutions, or Redis Cache - Redis is an open-source in-memory data store, which is often used as a distributed cache. You can configure an Azure Redis Cache for an Azure-hosted ASP.NET Core app, and use an Azure Redis Cache for local development.