private async Task TryLoadManifestAsync() { //Avoid unnecessary lock contention way after manifest is loaded by checking before lock if (CacheManifest is null) { await ManifestLock.WaitAsync(); try { //Check that once we have lock (due to a race condition on the outer check) that we still need to load the manifest if (CacheManifest is null) { if (File.Exists(ManifestPath)) { CacheManifest = await DeserializeFileAsync<ConcurrentDictionary<string?, IManifestEntry>>(ManifestPath); } else { if (!Directory.Exists(DirectoryPath)) { Directory.CreateDirectory(DirectoryPath); } CacheManifest = new ConcurrentDictionary<string?, IManifestEntry>(); await SerializeFileAsync(ManifestPath, CacheManifest); } } } finally { ManifestLock.Release(); } } }
/// <summary> /// Saves the cache manifest to the file system. /// </summary> /// <returns></returns> public async Task SaveManifestAsync() { await ManifestLock.WaitAsync(); try { if (!Directory.Exists(DirectoryPath)) { Directory.CreateDirectory(DirectoryPath); } await SerializeFileAsync(ManifestPath, CacheManifest); } finally { ManifestLock.Release(); } }
public void SaveManifest() { ManifestLock.Lock(); try { if (!Directory.Exists(DirectoryPath)) { Directory.CreateDirectory(DirectoryPath); } SerializeFile(ManifestPath, CacheManifest); } finally { ManifestLock.Release(); } }