private async Task <(DatabaseUsage DbUsage, File Cached)> GetFromCache(CancellationToken cancel) { File cached = null; DatabaseUsage databaseUsage = null; try { using (new SystemAccount()) { cached = await Node.LoadAsync <File>(DatabaseUsageCachePath, cancel) .ConfigureAwait(false); } if (cached != null) { if (DateTime.UtcNow - cached.ModificationDate <= DatabaseUsageCacheTime) { databaseUsage = (DatabaseUsage)cached.GetCachedData(CacheKey); if (databaseUsage == null) { var src = RepositoryTools.GetStreamString(cached.Binary.GetStream()); databaseUsage = JsonConvert.DeserializeObject <DatabaseUsage>(src); cached.SetCachedData(CacheKey, databaseUsage); } } } } catch (Exception e) { _logger.LogWarning("An error occurred during loading DatabaseUsage.cache: " + e); // do nothing } return(databaseUsage, cached); }
private async STT.Task PutToCacheAsync(DatabaseUsage databaseUsage, File cached, CancellationToken cancel) { var resultBuilder = new StringBuilder(); using (var writer = new StringWriter(resultBuilder)) JsonSerializer.Create(SerializerSettings).Serialize(writer, databaseUsage); using (new SystemAccount()) { cached ??= await CreateCacheFileAsync(cancel); if (cached == null) { return; } var iteration = 0; try { var serialized = resultBuilder.ToString(); var cachedStream = !cached.IsNew ? cached.Binary?.GetStream() : null; var cachedData = cachedStream != null?RepositoryTools.GetStreamString(cachedStream) : string.Empty; // save the content only if there was a change if (string.Equals(serialized, cachedData)) { return; } Retrier.Retry(5, 500, typeof(NodeIsOutOfDateException), () => { iteration++; // reload to have a fresh instance if (!cached.IsNew && iteration > 1) { cached = Node.Load <File>(cached.Id); } cached.SetCachedData(CacheKey, databaseUsage); cached.Binary.SetStream(RepositoryTools.GetStreamFromString(serialized)); cached.Save(SavingMode.KeepVersion); _logger.LogTrace($"DatabaseUsage.cache has been saved. Iteration: {iteration}"); }); } catch (Exception e) { _logger.LogWarning(e, $"An error occurred during saving DatabaseUsage.cache in iteration {iteration}."); // do nothing } } }
protected static string GetBinaryText(File file) { if (file == null) { return(string.Empty); } using (var stream = file.Binary.GetStream()) { return(RepositoryTools.GetStreamString(stream) ?? string.Empty); } }