public Task<ByteBuffer> Get(ByteBuffer key)
{
if (key.Length != _keySize) throw new ArgumentException("Key has wrong Length not equal to KeySize");
var tcs = new TaskCompletionSource<ByteBuffer>();
try
{
var k = new ByteStructs.Key20(key);
CacheValue cacheValue;
if (_cache.TryGetValue(k, out cacheValue))
{
var newCacheValue = cacheValue;
newCacheValue.AccessRate = cacheValue.AccessRate + 1;
_cache.TryUpdate(k, newCacheValue, cacheValue);
// It is not problem if update fails, it will have just lower access rate then real
var result = new byte[cacheValue.ContentLength];
_fileCollection.GetFile(cacheValue.FileId).RandomRead(result, 0, (int)cacheValue.ContentLength,
cacheValue.FileOfs, false);
tcs.SetResult(ByteBuffer.NewAsync(result));
return tcs.Task;
}
}
catch { } // It is better to return nothing than throw exception
tcs.SetResult(ByteBuffer.NewEmpty());
return tcs.Task;
}