public bool TryCacheInMemory(bool shouldCacheNextChunk) { lock (_cacheSyncObj) { if (!_chunkConfig.EnableCache || _isMemoryChunk || !_isCompleted || _memoryChunk != null) { _cachingChunk = 0; return(false); } try { var chunkSize = (ulong)GetChunkSize(_chunkHeader); if (!ChunkUtil.IsMemoryEnoughToCacheChunk(chunkSize, (uint)_chunkConfig.ChunkCacheMaxPercent)) { return(false); } _memoryChunk = FromCompletedFile(_filename, _chunkManager, _chunkConfig, true); if (shouldCacheNextChunk) { Task.Factory.StartNew(() => _chunkManager.TryCacheNextChunk(this)); } return(true); } catch (OutOfMemoryException) { return(false); } catch (Exception ex) { _logger.Error(string.Format("Failed to cache completed chunk {0}", this), ex); return(false); } finally { _cachingChunk = 0; } } }
public bool TryCacheInMemory(bool shouldCacheNextChunk) { lock (_cacheSyncObj) { if (!_chunkConfig.EnableCache || _isMemoryChunk || !_isCompleted || _memoryChunk != null) { _cachingChunk = 0; return(false); } try { var cachedFileChunks = _chunkManager.GetCachedFileChunks(); if (cachedFileChunks.Count >= _chunkConfig.ChunkCacheMaxCount) { return(false); } _memoryChunk = FromCompletedFile(_filename, _chunkManager, _chunkConfig, true); if (shouldCacheNextChunk) { Task.Factory.StartNew(() => _chunkManager.TryCacheNextChunk(this)); } return(true); } catch (OutOfMemoryException) { return(false); } catch (Exception ex) { _logger.Error(string.Format("Failed to cache completed chunk {0}", this), ex); return(false); } finally { _cachingChunk = 0; } } }