/// <summary> /// Convert a <see cref="Report" /> to a <see cref="StructuredQuery" />. /// </summary> /// <param name="report">The <see cref="Report" /> to convert. This cannot be null.</param> /// <param name="settings"></param> /// <returns> /// The converted report. /// </returns> /// <exception cref="System.ArgumentNullException">report</exception> public StructuredQuery Convert(Report report, ReportToQueryConverterSettings settings) { if (report == null) { throw new ArgumentNullException("report"); } if (settings == null) { settings = ReportToQueryConverterSettings.Default; } StructuredQuery result; CachingReportToQueryConverterValue cacheValue; CachingReportToQueryConverterKey key = new CachingReportToQueryConverterKey(report, settings); using (MessageContext msg = new MessageContext("Reports")) { // Check cache bool doConvert = !TryGetValue(key, msg, out cacheValue); // Check for force recalculation if (settings.RefreshCachedStructuredQuery) { msg.Append(() => "CachingReportToQueryConverter refreshed forced"); doConvert = true; } if (doConvert) { lock (_syncRoot) { using (CacheContext cacheContext = new CacheContext( )) { result = Converter.Convert(report, settings); cacheValue = new CachingReportToQueryConverterValue(result); Cache.Add(key, cacheValue); // Add the cache context entries to the appropriate CacheInvalidator _cacheInvalidator.AddInvalidations(cacheContext, key); } } } else if (CacheContext.IsSet( )) { // Add the already stored changes that should invalidate this cache // entry to any outer or containing cache contexts. using (CacheContext cacheContext = CacheContext.GetContext( )) { cacheContext.AddInvalidationsFor(_cacheInvalidator, key); } } } result = cacheValue.StructuredQuery; return(result); }
/// <summary> /// Try to get the value from cache, with logging. /// </summary> private bool TryGetValue(CachingReportToQueryConverterKey key, MessageContext msg, out CachingReportToQueryConverterValue result) { msg.Append(() => "CachingReportToQueryConverter key:" + key); CachingReportToQueryConverterValue cacheValue; bool foundValue; if (Cache.TryGetValue(key, out cacheValue)) { msg.Append(() => "CachingReportToQueryConverter cache hit"); msg.Append(() => $"Entry originally cached at {cacheValue.CacheTime}"); result = cacheValue; foundValue = true; } else { msg.Append(() => "CachingReportToQueryConverter cache miss"); result = null; foundValue = false; } return(foundValue); }