windows 将持久数据保存在内存中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3724553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Keeping persistent data in memory
提问by Eldad Mor
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).
我即将用 C# 开发 Windows 服务。该服务需要对系统中的事件进行跟踪,时不时地向文件中写入一些数据。这些正在进行的事件形成某种状态,因此我会将状态保存在内存中,并在事件到达时更新它。我不想让事情过于复杂,所以我不希望状态在磁盘上持久化,但我想知道我是否可以以某种方式使其持久化在内存中,以便如果服务崩溃(并且自动重启Windows)它可以从它离开的地方继续前进(可能会丢失一些事件,没什么大不了的)。
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.
我正在考虑创建一个“共享”内存区域,从而让 Windows 管理它,并且只在服务中使用它 - 但我不确定该对象在服务终止后是否会持续存在。
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 tooimportant. 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.
编辑:我不是在寻找矫枉过正的解决方案。数据有点重要,所以我想让它在内存中等待直到服务重新启动,但数据不是太重要。如果我可以轻松地保留数据,而无需处理文件、外部 3rd 方进程等,那么它更像是一个很好的功能。我理想的解决方案是一个简单的内置功能(在 .NET 或 Windows 中),它将为我提供一些内存中的持久性,只是为了从崩溃事件中恢复。
采纳答案by jmservera
You can use a Persitent Caching Block from the Microsoft Enterprise Library.
您可以使用Microsoft Enterprise Library 中的永久缓存块。
It is configurable and you can use many backing stores like database and isolated storage.
它是可配置的,您可以使用许多后备存储,例如数据库和隔离存储。
回答by Jaco Pretorius
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.
我建议您将所有状态保留在单个对象或对象层次结构中,将此对象序列化为 XML 并将其写入文件。它真的没有比这更简单了。
回答by mamoo
You could use Memcached, or Redis (which also persists it's data on disk, but handles it automatically).
您可以使用 Memcached 或 Redis(它也将数据保存在磁盘上,但会自动处理)。
http://code.google.com/p/redis/
http://code.google.com/p/redis/
You could also take a look at this question:
你也可以看看这个问题:
回答by eglasius
回答by KM?n
Even if, for instance, you keep the data on a shared-memory of some other networked pc, how would you "guarantee" that the networked pcwont hang/restart/halt/etc? In that case your service will lose the persisted data anyway.
例如,即使您将数据保存在其他联网电脑的共享内存中,您如何“保证”联网电脑不会挂起/重启/停止等?在这种情况下,您的服务无论如何都会丢失持久数据。
I would suggest, and chances are you'd likely to end up, storing the data on the same disk.
我会建议,而且您很可能最终会将数据存储在同一个磁盘上。
Note that, because of the volatile nature of memory(RAM) you cannot reload data that was previously there, before the system restart; not unless you use some mechanism to store/reload on disk.
请注意,由于内存(RAM)的易失性,您无法在系统重新启动之前重新加载以前存在的数据;除非您使用某种机制在磁盘上存储/重新加载。
--EDIT--
- 编辑 -
In that case, how about using MSMQ? So you can push everything over the queue, and even if your service gets a restart, it would look for the items in the queue and continue onwards.
在这种情况下,如何使用MSMQ?因此,您可以将所有内容推送到队列中,即使您的服务重新启动,它也会查找队列中的项目并继续前进。
回答by Madeleine
How about using isolated storage and persisting the object into memory that way?
如何使用隔离存储并以这种方式将对象持久化到内存中?