private bool CacheChunk(long chunkIndex) { lock (_lock) { if (_decryptedChunkByChunkIndex.Contains(chunkIndex)) { return(false); } //log.Info(string.Format("{0}: Going to add chunk #{1} to cache", new object[] { DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff"), chunkIndex })); DecryptedChunk DecryptedChunk = new DecryptedChunk(this, chunkIndex); _decryptedChunkByChunkIndex.Add(chunkIndex, DecryptedChunk); _decryptedChunkIndexSortedByCreationTime.Add(chunkIndex); RemoveOldCachedItems(); // Cached return(true); } }
private DecryptedChunk GetOrAddDecryptedChunk(long chunkIndex) { lock (_lock) { if (_decryptedChunkByChunkIndex.Contains(chunkIndex)) { //Logger.Log(string.Format("{0}: Chunk #{1} already cached", new object[] { DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff"), chunkIndex })); return((DecryptedChunk)_decryptedChunkByChunkIndex[chunkIndex]); } //Logger.Log(string.Format("{0}: Chunk #{1} is NOT yet available", new object[] { DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff"), chunkIndex })); DecryptedChunk DecryptedChunk = new DecryptedChunk(this, chunkIndex); _decryptedChunkByChunkIndex.Add(chunkIndex, DecryptedChunk); _decryptedChunkIndexSortedByCreationTime.Add(chunkIndex); RemoveOldCachedItems(); return(DecryptedChunk); } }
public byte[] GetDecryptedChunk(long chunkIndex) { //log.Info(string.Format("{0}: Asked for chunk #{1}, thread = {2}", new object[] { DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff"), chunkIndex, Thread.CurrentThread.ManagedThreadId })); DecryptedChunk DecryptedChunk = GetOrAddDecryptedChunk(chunkIndex); // Start caching some next chunks int cachedChunks = 0; for (long AdditionalCachedChunkIndex = chunkIndex + 1; cachedChunks < _additionalChunkToCache && // Cache some number of next chunks AdditionalCachedChunkIndex < _chunkCount && _decryptedChunkByChunkIndex.Count < _maxDecryptedChunkCount; // Doesn't occupy entire cache AdditionalCachedChunkIndex++) { if (CacheChunk(AdditionalCachedChunkIndex)) { cachedChunks++; } } return(DecryptedChunk.DecryptedData); }