ActiveReports 18 .NET Edition
Developers / Create Applications / Js Viewer Application / Caching Reports
In This Topic
    Caching Reports
    In This Topic

    Caching reports is a great technique to improve the performance of report rendering and is extremely beneficial for large reports with an infrequent change of data, like periodic reports that generate monthly or weekly.

    The Js Viewer backends read a report as many times as required. However, sometimes you may need to cache the report as demonstrated in the example below.

    Startup.cs
    Copy Code
    app.UseReportViewer(settings =>
    {
        settings.UseReportProvider(new ReportProvider());
    });
    
    ReportProvider.cs
    Copy Code
    public class ReportProvider : IReportProvider
      {
          public Stream GetReport(string reportId)
          {
              PageReport report = new PageReport();
              var cache = new MemoryCache(new MemoryCacheOptions { SizeLimit = 10000 });
              cache.GetOrCreate(reportId, entry =>
              {
                  entry.SetSlidingExpiration(TimeSpan.FromSeconds(30));
                  report = new PageReport(new FileInfo(Path.Combine(@"C:\Reports", reportId)));
                  return new MemoryStream(Encoding.ASCII.GetBytes(report.ToRdlString()));
              });
    
              return new MemoryStream(Encoding.ASCII.GetBytes(report.ToRdlString()));  
          }
    
          public ReportDescriptor GetReportDescriptor(string reportId)
          {            
              switch (Path.GetExtension(reportId))
              {
                  case ".rdlx": return new ReportDescriptor(ReportType.RdlXml);
                  case ".rdf": return new ReportDescriptor(ReportType.Rdf);
                  case ".rdlx-master": return new ReportDescriptor(ReportType.RdlMasterXml);
                  case ".rpx": return new ReportDescriptor(ReportType.RpxXml);
                  default: return null;
              }
          }
      }