MemoryMappedFile的初级应用

归志宁无五亩园,读书本意在元元。这篇文章主要讲述MemoryMappedFile的初级应用相关的知识,希望能为你提供帮助。

1public class SyncMemoryList< T> : SyncList< T> , IDisposable 2{ 3private MemoryCache< T> _memoryCache = new MemoryCache< T> (); 4 5public new void Add(T item) 6{ 7string json = $"{ToJson(item)}\n"; 8WriteCache(json); 9 10base.Add(item); 11} 12 13private string ToJson(T item) 14{ 15Type type = typeof(T); 16if (type.IsClass) 17return JsonExtensions.ToJson(item); 18else 19return item.ToString(); 20} 21 22private void WriteCache(string text) 23{ 24byte[] bytes = Encoding.UTF8.GetBytes(text); 25if (_memoryCache.IsAllowWrite(bytes.Length)) 26{ 27_memoryCache.Write(bytes); 28} 29else 30{ 31_memoryCache.Dispose(); 32_memoryCache = new MemoryCache< T> (); 33_memoryCache.Write(bytes); 34} 35} 36 37#region IDisposable Support 38private bool disposedValue = https://www.songbingjia.com/android/false; // 要检测冗余调用 39 40protected virtual void Dispose(bool disposing) 41{ 42if (!disposedValue) 43{ 44if (disposing) 45{ 46// TODO: 释放托管状态(托管对象)。 47} 48 49_memoryCache?.Dispose(); 50disposedValue = true; 51} 52} 53 54// TODO: 仅当以上 Dispose(bool disposing) 拥有用于释放未托管资源的代码时才替代终结器。 55// ~SyncMemoryList() { 56//// 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。 57//Dispose(false); 58// } 59 60// 添加此代码以正确实现可处置模式。 61public void Dispose() 62{ 63// 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。 64Dispose(true); 65// TODO: 如果在以上内容中替代了终结器,则取消注释以下行。 66// GC.SuppressFinalize(this); 67} 68#endregion 69} 70 71internal class MemoryCache< T> :IDisposable 72{ 73private readonly MemoryMappedFile _memoryMapped = null; 74private readonly MemoryMappedViewStream _stream = null; 75private static readonly long _defaultSize = 1024 * 1024; 76private readonly long _capatity = 0; 77public MemoryCache() 78{ 79string mapname = $"{typeof(T).Name}"; 80string fileName = $"{typeof(T).Name}_{DateTime.Now:yyyy_M_d}.dat"; 81long maxlen = 0; 82if (File.Exists(fileName)) 83{ 84(long size,long offset) = (0, 0); 85FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); 86(size,offset) = GetFileInfo(stream); 87//stream.Close(); 88maxlen = size + _defaultSize; 89_memoryMapped = MemoryMappedFile.CreateFromFile(stream, mapname, maxlen, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); 90_stream = _memoryMapped.CreateViewStream(); 91_stream.Position = offset == 0 ? 16 : offset; 92} 93else 94{ 95maxlen = _defaultSize + 16; 96_memoryMapped = MemoryMappedFile.CreateFromFile(fileName, FileMode.OpenOrCreate, mapname, maxlen, MemoryMappedFileAccess.ReadWrite); 97_stream = _memoryMapped.CreateViewStream(); 98_stream.Position = 16; 99} 100 101_capatity = maxlen - 16; 102} 103 104public long Position => _stream.Position; 105 106private (long, long) GetFileInfo(Stream stream) 107{ 108try 109{ 110byte[] byteSize = new byte[8]; 111byte[] byteOffset = new byte[8]; 112 113stream.Read(byteSize, 0, byteSize.Length); 114stream.Read(byteOffset, 0, byteOffset.Length); 115 116return (BitConverter.ToInt64(byteSize, 0), BitConverter.ToInt64(byteOffset, 0)); 117} 118catch (Exception e) 119{ 120return (_defaultSize, 0); 121} 122} 123 124public bool IsAllowWrite(long size) 125{ 126return _capatity - _stream.Position > size; 127} 128 129public void WriteLength(long offset) 130{ 131byte[] byteSize = BitConverter.GetBytes(_capatity); 132byte[] byteOffset = BitConverter.GetBytes(offset); 133_stream.Position = 0; 134_stream.Write(byteSize, 0, byteSize.Length); 135_stream.Write(byteOffset, 0, byteOffset.Length); 136} 137 138public void Write(byte[] bytes) 139{ 140var _offset = _stream.Position; 141WriteLength(_offset + bytes.Length); 142_stream.Position = _offset; 143_stream.Write(bytes, 0, bytes.Length); 144} 145 146public void Dispose() 147{ 148_memoryMapped?.Dispose(); 149_stream?.Dispose(); 150} 151}

【MemoryMappedFile的初级应用】 

    推荐阅读