Exemplo n.º 1
0
 public ReadStream(CachedItem cachedItem)
 {
     _cachedItem = cachedItem;
     _cachedItem.LastAccessed = DateTime.UtcNow;
     Interlocked.Increment(ref cachedItem._readerCount);
     _sourceStream = File.OpenRead(cachedItem.FilePath);
 }
 public Stream Add(string fileKey, Stream stream, long uncompressedLength)
 {
     if (!_cacheSettings.Enabled || uncompressedLength > _cacheSettings.MaxCacheSize)
     {
         return(stream);
     }
     _lock.EnterUpgradeableReadLock();
     try
     {
         if (_cachedItems.ContainsKey(fileKey))
         {
             return(stream);
         }
         _lock.EnterWriteLock();
         CachedItem cachedItem;
         try
         {
             if (IsFull(uncompressedLength))
             {
                 EvictCachedItems(uncompressedLength);
                 if (IsFull(uncompressedLength))
                 {
                     return(stream);
                 }
             }
             cachedItem = new CachedItem(fileKey, uncompressedLength, _cacheSettings);
             _cachedItems.Add(fileKey, cachedItem);
             _cacheSize += uncompressedLength;
         }
         finally
         {
             _lock.ExitWriteLock();
         }
         // call create outside of the write lock as to not stop other writers whilst we copy the data into the cache.
         return(cachedItem.Create(stream));
     }
     finally
     {
         _lock.ExitUpgradeableReadLock();
     }
 }