perf: 优化缓存策略

This commit is contained in:
Argo Zhang 2025-11-21 12:24:32 +08:00
parent 30fa54a83f
commit 6684d0a928
2 changed files with 12 additions and 14 deletions

View File

@ -8,18 +8,18 @@ using IP2Region.Net.Internal.Abstractions;
namespace IP2Region.Net.Internal;
class ContentCacheStrategy(string xdbPath) : AbstractCacheStrategy(xdbPath)
class ContentCacheStrategy : AbstractCacheStrategy
{
ReadOnlyMemory<byte> _cacheData = default;
public ContentCacheStrategy(string xdbPath) : base(xdbPath)
{
using var reader = base.GetXdbFileStream();
_cacheData = base.GetData(0, (int)reader.Length);
}
public override ReadOnlyMemory<byte> GetData(int offset, int length)
{
if (_cacheData.IsEmpty)
{
using var reader = base.GetXdbFileStream();
_cacheData = base.GetData(0, (int)reader.Length);
}
return _cacheData.Slice(offset, length);
}
}

View File

@ -8,16 +8,14 @@ using IP2Region.Net.Internal.Abstractions;
namespace IP2Region.Net.Internal;
internal class VectorIndexCacheStrategy(string xdbPath) : AbstractCacheStrategy(xdbPath)
internal class VectorIndexCacheStrategy : AbstractCacheStrategy
{
ReadOnlyMemory<byte> _vectorCahe = default;
public override ReadOnlyMemory<byte> GetVectorIndexStartPos(int offset)
public VectorIndexCacheStrategy(string xdbPath) : base(xdbPath)
{
if (_vectorCahe.IsEmpty)
{
_vectorCahe = base.GetData(256, 256 * 256 * 8);
}
return _vectorCahe.Slice(offset, 8);
_vectorCahe = base.GetData(256, 256 * 256 * 8);
}
public override ReadOnlyMemory<byte> GetVectorIndexStartPos(int offset) => _vectorCahe.Slice(offset, 8);
}