feat: 增加 IDispose 接口保证释放文件句柄
This commit is contained in:
parent
5b8b47df5e
commit
7612e679fb
|
|
@ -78,6 +78,8 @@ public class SearcherTest
|
|||
|
||||
searcher.Search("58.251.27.201");
|
||||
Assert.Equal(3, searcher.IoCount);
|
||||
|
||||
searcher.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -89,6 +91,8 @@ public class SearcherTest
|
|||
|
||||
searcher.Search("58.251.27.201");
|
||||
Assert.Equal(2, searcher.IoCount);
|
||||
|
||||
searcher.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -100,6 +104,8 @@ public class SearcherTest
|
|||
|
||||
searcher.Search("58.251.27.201");
|
||||
Assert.Equal(0, searcher.IoCount);
|
||||
|
||||
searcher.Dispose();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ namespace IP2Region.Net.Abstractions;
|
|||
/// <summary>
|
||||
/// IP 转化为地理位置搜索器接口
|
||||
/// </summary>
|
||||
public interface ISearcher
|
||||
public interface ISearcher : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// 搜索方法
|
||||
|
|
|
|||
|
|
@ -14,7 +14,14 @@ class ContentCacheStrategy : FileCacheStrategy
|
|||
public ContentCacheStrategy(string xdbPath) : base(xdbPath)
|
||||
{
|
||||
_cacheData = base.GetData(0, (int)XdbFileStream.Length);
|
||||
XdbFileStream.Close();
|
||||
XdbFileStream.Dispose();
|
||||
}
|
||||
|
||||
public override ReadOnlyMemory<byte> GetData(long offset, int length) => _cacheData.Slice((int)offset, length);
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
base.Dispose(false);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -60,4 +60,26 @@ class FileCacheStrategy(string xdbPath) : ICacheStrategy
|
|||
ArrayPool<byte>.Shared.Return(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放文件句柄
|
||||
/// </summary>
|
||||
/// <param name="disposing"></param>
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
XdbFileStream.Close();
|
||||
XdbFileStream.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <inheritdoc/>
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
namespace IP2Region.Net.Internal;
|
||||
|
||||
internal interface ICacheStrategy
|
||||
internal interface ICacheStrategy : IDisposable
|
||||
{
|
||||
int IoCount { get; }
|
||||
|
||||
|
|
|
|||
|
|
@ -147,4 +147,13 @@ public class Searcher(CachePolicy cachePolicy, string xdbPath) : ISearcher
|
|||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <inheritdoc/>
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_cacheStrategy.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue