Merge pull request #413 from 4ba-Co/fix/412-peak-memory-usage

fix: Correct abnormal memory spikes
This commit is contained in:
Leon / 狮子的魂 2025-12-31 21:53:00 +08:00 committed by GitHub
commit fe2dfc1088
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 10 deletions

View File

@ -3,25 +3,32 @@
// license that can be found in the LICENSE file.
// @Author Alan <lzh.shap@gmail.com>
// @Date 2023/07/25
// Updated by Argo Zhang <argo@live.ca> at 2025/11/21
// Updated by Wong <vcd.hai@outlook.com> at 2025/12/31
namespace IP2Region.Net.Internal;
class ContentCacheStrategy : FileCacheStrategy
class ContentCacheStrategy(string xdbPath) : ICacheStrategy
{
private readonly ReadOnlyMemory<byte> _cacheData;
// TODO: these constants can be moved to the interface as defaults when using .NET 10
private const int HeaderInfoLength = 256;
private const int VectorIndexSize = 8;
public ContentCacheStrategy(string xdbPath) : base(xdbPath)
private readonly ReadOnlyMemory<byte> _cacheData = File.ReadAllBytes(xdbPath);
public int IoCount => 0;
public void ResetIoCount()
{
_cacheData = base.GetData(0, (int)XdbFileStream.Length);
XdbFileStream.Close();
XdbFileStream.Dispose();
// Do nothing
}
public override ReadOnlyMemory<byte> GetData(long offset, int length) => _cacheData.Slice((int)offset, length);
public ReadOnlyMemory<byte> GetVectorIndex(int offset)
=> _cacheData.Slice(HeaderInfoLength + offset, VectorIndexSize);
protected override void Dispose(bool disposing)
public ReadOnlyMemory<byte> GetData(long offset, int length) => _cacheData.Slice((int)offset, length);
public void Dispose()
{
base.Dispose(false);
// Do nothing
}
}