refactor: 精简代码
This commit is contained in:
parent
87666aa353
commit
62ab160572
|
|
@ -26,16 +26,6 @@ internal abstract class AbstractCacheStrategy
|
|||
useAsync: true);
|
||||
}
|
||||
|
||||
protected int GetVectorIndexStartPos(uint ip)
|
||||
{
|
||||
var il0 = ip >> 24 & 0xFF;
|
||||
var il1 = ip >> 16 & 0xFF;
|
||||
var idx = il0 * VectorIndexCols * VectorIndexSize + il1 * VectorIndexSize;
|
||||
return (int)idx;
|
||||
}
|
||||
|
||||
internal abstract ReadOnlyMemory<byte> GetVectorIndex(uint ip);
|
||||
|
||||
internal virtual ReadOnlyMemory<byte> GetData(int offset, int length)
|
||||
{
|
||||
byte[] buffer = ArrayPool<byte>.Shared.Rent(length);
|
||||
|
|
@ -45,14 +35,14 @@ internal abstract class AbstractCacheStrategy
|
|||
XdbFileStream.Seek(offset, SeekOrigin.Begin);
|
||||
|
||||
int bytesRead;
|
||||
do
|
||||
while (totalBytesRead < length)
|
||||
{
|
||||
int bytesToRead = Math.Min(BufferSize, length - totalBytesRead);
|
||||
bytesRead = XdbFileStream.Read(buffer, totalBytesRead, bytesToRead);
|
||||
totalBytesRead += bytesRead;
|
||||
|
||||
IoCount++;
|
||||
} while (bytesRead > 0 && totalBytesRead < length);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
|
|
|||
|
|
@ -9,23 +9,12 @@ using IP2Region.Net.XDB;
|
|||
|
||||
namespace IP2Region.Net.Internal;
|
||||
|
||||
internal class CacheStrategyFactory
|
||||
internal class CacheStrategyFactory(string xdbPath)
|
||||
{
|
||||
private readonly string _xdbPath;
|
||||
|
||||
public CacheStrategyFactory(string xdbPath)
|
||||
public AbstractCacheStrategy CreateCacheStrategy(CachePolicy cachePolicy) => cachePolicy switch
|
||||
{
|
||||
_xdbPath = xdbPath;
|
||||
}
|
||||
|
||||
public AbstractCacheStrategy CreateCacheStrategy(CachePolicy cachePolicy)
|
||||
{
|
||||
return cachePolicy switch
|
||||
{
|
||||
CachePolicy.Content => new ContentCacheStrategy(_xdbPath),
|
||||
CachePolicy.VectorIndex => new VectorIndexCacheStrategy(_xdbPath),
|
||||
CachePolicy.File => new FileCacheStrategy(_xdbPath),
|
||||
_ => throw new ArgumentException(nameof(cachePolicy))
|
||||
};
|
||||
}
|
||||
CachePolicy.Content => new ContentCacheStrategy(xdbPath),
|
||||
CachePolicy.VectorIndex => new VectorIndexCacheStrategy(xdbPath),
|
||||
_ => new FileCacheStrategy(xdbPath),
|
||||
};
|
||||
}
|
||||
|
|
@ -19,12 +19,6 @@ internal class ContentCacheStrategy : AbstractCacheStrategy
|
|||
XdbFileStream.Dispose();
|
||||
}
|
||||
|
||||
internal override ReadOnlyMemory<byte> GetVectorIndex(uint ip)
|
||||
{
|
||||
int idx = GetVectorIndexStartPos(ip);
|
||||
return _cacheData.Slice(HeaderInfoLength + idx, VectorIndexSize);
|
||||
}
|
||||
|
||||
internal override ReadOnlyMemory<byte> GetData(int offset, int length)
|
||||
{
|
||||
return _cacheData.Slice(offset, length);
|
||||
|
|
|
|||
|
|
@ -8,15 +8,6 @@ using IP2Region.Net.Internal.Abstractions;
|
|||
|
||||
namespace IP2Region.Net.Internal;
|
||||
|
||||
internal class FileCacheStrategy : AbstractCacheStrategy
|
||||
internal class FileCacheStrategy(string xdbPath) : AbstractCacheStrategy(xdbPath)
|
||||
{
|
||||
public FileCacheStrategy(string xdbPath) : base(xdbPath)
|
||||
{
|
||||
}
|
||||
|
||||
internal override ReadOnlyMemory<byte> GetVectorIndex(uint ip)
|
||||
{
|
||||
var idx = GetVectorIndexStartPos(ip);
|
||||
return GetData(HeaderInfoLength + idx, VectorIndexSize);
|
||||
}
|
||||
}
|
||||
|
|
@ -17,10 +17,4 @@ internal class VectorIndexCacheStrategy : AbstractCacheStrategy
|
|||
var vectorLength = VectorIndexRows * VectorIndexCols * VectorIndexSize;
|
||||
_vectorIndex = base.GetData(HeaderInfoLength, vectorLength);
|
||||
}
|
||||
|
||||
internal override ReadOnlyMemory<byte> GetVectorIndex(uint ip)
|
||||
{
|
||||
var idx = GetVectorIndexStartPos(ip);
|
||||
return _vectorIndex.Slice(idx, VectorIndexSize);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue