C# 内存映射文件 .NET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/357170/
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
Memory Mapped Files .NET
提问by CSharpAtl
I have a project and it needs to access a large amount of proprietary data in ASP.NET. This was done on the Linux/PHP by loading the data in shared memory. I was wondering if trying to use Memory Mapped Files would be the way to go, or if there is a better way with better .NET support. I was thinking of using the Data Cache but not sure of all the pitfalls of size of data being saved in the Cache.
我有一个项目,它需要访问 ASP.NET 中的大量专有数据。这是通过在共享内存中加载数据在 Linux/PHP 上完成的。我想知道是否尝试使用内存映射文件是可行的方法,或者是否有更好的方法可以更好地支持 .NET。我正在考虑使用数据缓存,但不确定缓存中保存的数据大小的所有缺陷。
回答by Filip Fr?cz
If you are looking for a Memory Mapped library for C#, take a peek at Tomas Restrepo's filemapwrapper. It's licensed under the LGPL.
如果您正在寻找用于 C# 的内存映射库,请查看Tomas Restrepo 的文件映射包装器。它是根据 LGPL 获得许可的。
回答by jlew
Memory Mapped files can be used when you have a large amount of data and don't want to incur the cost of marshaling it across process boundaries. I have used it for a similar purpose. You need to be fairly comfortable with unsafe and pinned memory concepts in .NET to take advantage of MMFs. Apparently, the Enterprise Library's caching block contains code which wraps the underlying C# API. I have seen at least one other implementation elsewhere.
当您拥有大量数据并且不想承担跨进程边界封送数据的成本时,可以使用内存映射文件。我已经将它用于类似的目的。您需要对 .NET 中的不安全和固定内存概念相当熟悉才能利用 MMF。 显然,企业库的缓存块包含包装底层 C# API 的代码。我在其他地方至少看到过另一种实现。
If you can live with the marshaling cost, it's probably easier and more elegant to use some kind of .NET remoting solution.
如果您可以忍受封送成本,那么使用某种 .NET 远程处理解决方案可能更容易、更优雅。
回答by Steven Behnke
You might want to just throw it in the Cache[] object. You can set a cache expiration based on the real file. Then whenever you modify the actual file the contents will be null for the object in the cache and you can reload it. This may not be appropriate if you're dealing with a large number of bytes.
您可能只想将其放入 Cache[] 对象中。您可以根据真实文件设置缓存过期时间。然后,每当您修改实际文件时,缓存中对象的内容将为空,您可以重新加载它。如果您要处理大量字节,这可能不合适。
byte[] fileBytes = Cache["fileBytes"];
if (null == fileBytes) {
// reload the file and add it to the cache.
string fileLocation = Server.MapPath("path/to/file.txt");
// Just a same of some bytes.
fileBytes = new byte[10];
Cache.Insert(fileLocation, fileBytes, new System.Web.Caching.CacheDependency(fileLocation));
}
I guess I don't have a specific answer about the performance characteristics of the cache and large amounts of data. http://www.alachisoft.com/ncache/asp-net-cache.htmlStates that you get between 2 and 3 gigs of cache space that must be shared between your application and the cache.
我想我没有关于缓存和大量数据的性能特征的具体答案。 http://www.alachisoft.com/ncache/asp-net-cache.html声明您获得了 2 到 3 个必须在您的应用程序和缓存之间共享的缓存空间。
回答by Titian Cernicova-Dragomir
I know this is a bit late, but the .NET 4.0 framework now supports memory-mapped files out of the box:
我知道这有点晚了,但 .NET 4.0 框架现在支持开箱即用的内存映射文件:
http://blogs.msdn.com/salvapatuel/archive/2009/06/08/working-with-memory-mapped-files-in-net-4.aspx
http://blogs.msdn.com/salvapatuel/archive/2009/06/08/working-with-memory-mapped-files-in-net-4.aspx