public bool Exists(FileSystemCacheKey key) { lock (KeyLockFor(key)) { return(_fileSystemAccess.Exists(CachePathForKey(key))); } }
public void Add(FileSystemCacheKey key, Stream contents, CancellationToken cancellationToken = default, bool ensureFolderExists = false) { string cachePathForKey = CachePathForKey(key); try { lock (KeyLockFor(key)) { if (ensureFolderExists) { EnsureFolderExists(Path.GetDirectoryName(cachePathForKey)); } _fileSystemAccess.Write(cachePathForKey, contents, cancellationToken) .GetAwaiter() .GetResult(); } } catch (IOException) when(_fileSystemAccess.Exists(cachePathForKey)) { _logger.Warning($"Detected file collision for {CachePathForKey(key)}. Swallowing exception"); } catch (Exception exception) { string message = $"Unable to write content for file system cache item with key {key.Key}"; _logger.Error(exception, message); throw new Exception(message, exception); } }
private string CachePathForKey(FileSystemCacheKey key) { string cachePath = key.Path; string fileName = Path.GetFileName(cachePath); string folder = Path.GetDirectoryName(cachePath); return(Path.Combine(_settings.Path, folder, $"{_settings.Prefix}_{fileName}")); }
private object KeyLockFor(FileSystemCacheKey key) { if (!_evictionInProgress) { return(KeyLocks.GetOrAdd(key.Key, new object())); } lock (EvictionLock) { return(KeyLocks.GetOrAdd(key.Key, new object())); } }
public Stream Get(FileSystemCacheKey key) { try { lock (KeyLockFor(key)) { return(_fileSystemAccess.OpenRead(CachePathForKey(key))); } } catch (Exception exception) { string errorMessage = $"Unable to read content for file system cache item with key {key.Key}"; _logger.Error(errorMessage, exception); throw new Exception(errorMessage, exception); } }