プロジェクトがあり、ASP.NET の大量の独自データにアクセスする必要があります。これは、共有メモリにデータをロードすることにより、Linux/PHP で実行されました。私は、Memory Mapped Files を使用しようとするのが道なのか、それとも .NET サポートが強化されたより良い方法があるのか疑問に思っていました。データ キャッシュを使用することを考えていましたが、キャッシュに保存されるデータのサイズに関するすべての落とし穴について確信が持てませんでした。
4 に答える
これが少し遅れていることは承知していますが、.NET 4.0 フレームワークは、すぐに使えるメモリ マップト ファイルをサポートするようになりました。
http://blogs.msdn.com/salvapatuel/archive/2009/06/08/working-with-memory-mapped-files-in-net-4.aspx
If you are looking for a Memory Mapped library for C#, take a peek at Tomas Restrepo's filemap wrapper. It's licensed under the LGPL.
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.
If you can live with the marshaling cost, it's probably easier and more elegant to use some kind of .NET remoting solution.
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.
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.html States that you get between 2 and 3 gigs of cache space that must be shared between your application and the cache.