public void WriteVariablesToDiskCache(GitVersionCacheKey cacheKey, VersionVariables variablesFromCache) { var cacheDir = PrepareCacheDirectory(); var cacheFileName = GetCacheFileName(cacheKey, cacheDir); variablesFromCache.FileName = cacheFileName; Dictionary <string, string> dictionary; using (log.IndentLog("Creating dictionary")) { dictionary = variablesFromCache.ToDictionary(x => x.Key, x => x.Value); } void WriteCacheOperation() { using var stream = fileSystem.OpenWrite(cacheFileName); using var sw = new StreamWriter(stream); using (log.IndentLog("Storing version variables to cache file " + cacheFileName)) { var serializer = new Serializer(); serializer.Serialize(sw, dictionary); } } var retryOperation = new RetryAction <IOException>(maxRetries: 6); retryOperation.Execute(WriteCacheOperation); }
public VersionVariables LoadVersionVariablesFromDiskCache(GitVersionCacheKey key) { using (log.IndentLog("Loading version variables from disk cache")) { var cacheDir = PrepareCacheDirectory(); var cacheFileName = GetCacheFileName(key, cacheDir); if (!fileSystem.Exists(cacheFileName)) { log.Info("Cache file " + cacheFileName + " not found."); return(null); } using (log.IndentLog("Deserializing version variables from cache file " + cacheFileName)) { try { var loadedVariables = VersionVariables.FromFile(cacheFileName, fileSystem, log); return(loadedVariables); } catch (Exception ex) { log.Warning("Unable to read cache file " + cacheFileName + ", deleting it."); log.Info(ex.ToString()); try { fileSystem.Delete(cacheFileName); } catch (Exception deleteEx) { log.Warning($"Unable to delete corrupted version cache file {cacheFileName}. Got {deleteEx.GetType().FullName} exception."); } return(null); } } } }
private static string GetCacheFileName(GitVersionCacheKey key, string cacheDir) { return(Path.Combine(cacheDir, string.Concat(key.Value, ".yml"))); }
private static string GetCacheFileName(GitVersionCacheKey key, string cacheDir) => PathHelper.Combine(cacheDir, string.Concat(key.Value, ".yml"));