public void TestSerializationOfReportResultCacheKey( )
        {
            /////
            // Get the AA_All Fields Report since it has rollups.
            /////
            long reportId = Entity.GetIdFromUpgradeId(new Guid("da986865-1c90-4ae4-9a48-36cd4514208c"));

            var reportingInterface = new ReportingInterface( );

            ReportCompletionData reportCompletion = reportingInterface.PrepareReport(reportId, new ReportSettings( ));

            ReportResultCacheKey reportResultCacheKey = reportCompletion.ResultCacheKey;

            using (var stream = new MemoryStream( ))
            {
                ProtoBuf.Serializer.Serialize(stream, reportResultCacheKey);

                byte [] bytes = stream.ToArray( );

                using (var stream2 = new MemoryStream(bytes))
                {
                    var key2 = ProtoBuf.Serializer.Deserialize <ReportResultCacheKey>(stream2);

                    Assert.AreEqual(key2, reportResultCacheKey);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Check the cache.
        /// Run the continuation callback if necessary.
        /// Apply presentation formatting.
        /// Store result in cache if possible.
        /// </summary>
        /// <typeparam name="T">Type of result after presentation formatting.</typeparam>
        /// <param name="reportCompletionData">The completion process for a partially processed report.</param>
        /// <param name="presentationCallback">Presentation formatting, such as conversion to webapi message.</param>
        /// <returns></returns>
        public string GetReportResult(ReportCompletionData reportCompletionData, Func <ServiceResult.ReportResult, string> presentationCallback)
        {
            if (reportCompletionData == null)
            {
                return(null);
            }

            ReportResultCacheKey cacheKey;
            string result;

            using (MessageContext msg = new MessageContext("Reports"))
            {
                cacheKey = reportCompletionData.ResultCacheKey;

                // A null cacheKey indicates that the report is uncacheable
                if (cacheKey == null)
                {
                    msg.Append(() => "ReportResultCache received no cache key");

                    // Invoke callback
                    ServiceResult.ReportResult serviceResult = reportCompletionData.PerformRun( );

                    // And format it
                    result = presentationCallback(serviceResult);
                }
                else
                {
                    // Check cache
                    bool fromCache = TryGetOrAdd(cacheKey, msg, out result, key1 =>
                    {
                        string formattedResult;

                        using (CacheContext cacheContext = new CacheContext( ))
                        {
                            // Call completion callback to run report
                            ServiceResult.ReportResult serviceResult = reportCompletionData.PerformRun( );

                            // Format result
                            formattedResult = presentationCallback(serviceResult);

                            // Add the cache context entries to the appropriate CacheInvalidator
                            _cacheInvalidator.AddInvalidations(cacheContext, cacheKey);

                            if (reportCompletionData.CacheContextDuringPreparation != null)
                            {
                                _cacheInvalidator.AddInvalidations(reportCompletionData.CacheContextDuringPreparation, cacheKey);
                            }
                        }
                        return(formattedResult);
                    });

                    // Note: No call to AddInvalidationsFor because no-one is listening.
                }
            }

            return(result);
        }