Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping persistent data in memory

I am about to develop a Windows service in C#. This service needs to keep track of events in the system, and write some data to files from time to time. These ongoing events form a certain state, so I'll keep the state in memory and update it as events will arrive. I don't want to over-complicate things so I don't want the state to be persistent on disk, but I'm wondering if I could somehow make it persistent in memory, so that if the service crashes (and auto restarts by Windows) it could pick up from where it left and go on (possibly losing some events, not a big deal).

I was thinking along the line of creating a "shared" memory area, thus letting Windows manage it, and using it only in the service - but I'm not sure that object will persist after the service dies.

Any ideas?

EDIT: I'm not looking for an overkill solution. The data is somewhat important so I'd like to keep it waiting in memory until the service is restarted, but the data is not too important. It's more of a nice-to-have feature if I can persist the data easily, without working with files, external 3rd party processes and so on. My ideal solution would be a simple built-in feature (in .NET or in Windows) that will provide me with some in-memoory persistence, just to recover from a crash event.

like image 551
Eldad Mor Avatar asked Sep 16 '10 07:09

Eldad Mor


2 Answers

You can use a Persitent Caching Block from the Microsoft Enterprise Library.

It is configurable and you can use many backing stores like database and isolated storage.

like image 90
jmservera Avatar answered Oct 09 '22 17:10

jmservera


I know you said that you don't want to over-complicate things by persisting it to disk, but it's definitely going to much more complicate to persist stuff into shared memory or any of the solutions listed here. The reason why so many applications use databases or file storage is because it's the simplest solution.

I would recommend you keep all the state in a single object or object hierarchy, serialize this object to XML and write it to a file. It really doesn't get much simpler than that.

like image 40
Jaco Pretorius Avatar answered Oct 09 '22 16:10

Jaco Pretorius