BTDB.ChunkCache.DiskChunkCache.Get C# (CSharp) Method

Get() public method

public Get ( ByteBuffer key ) : Task
key ByteBuffer
return Task
        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;
        }

Usage Example

Example #1
0
 public void GetFromEmptyCacheReturnsEmptyByteBuffer()
 {
     using (var fileCollection = new InMemoryFileCollection())
     using (var cache = new DiskChunkCache(fileCollection, 20, 1000))
     {
         Assert.AreEqual(0, cache.Get(CalcHash(new byte[] { 0 })).Result.Length);
     }
 }
All Usage Examples Of BTDB.ChunkCache.DiskChunkCache::Get