Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

17.19 Caching Data

Caching Data

If you are building an ASP.NET website, it is important to optimize the web server’s memory. Reports can be quite large, and when you have many users on the site then they can consume a lot of the server’s resources. You can optimize reports by caching them. This saves them to memory so that the same report can be used across page views as well as shared between multiple users. Otherwise, the report would have to be processed each time it is referenced on a page. This creates unnecessary overhead for the web server as well as the database server which is servicing the queries.

There are two ways to cache reports and the method that you choose is dependent upon the report’s shareability. A report that is used by multiple users within a relatively small time frame has high shareability. A report that is only needed by a single user is deemed to have a low shareability.

When discussing whether a report is needed by more than one user, there is more to it than whether just the report itself is being used. Shareability takes into consideration the parameters that the report uses as well as the data that is returned from the database. For a report to have high shareability, every aspect of it must be identical across all users requesting that report. If a user enters one set of parameters for a report and another user enters a different set of parameters for that same report, the reports are considered to be different and can’t be shared.

Reports that have low shareability are only used by a single user at a time. Thus, the report only needs to be persisted across page views for that one user. In this case, you can persist the report across page views by saving the report to the Session collection. This is discussed in detail in Chapter 14.

Reports that have high shareability will be used by multiple users on a regular basis. You shouldn’t store the report to the Session collection because each user has their own instance of this object. Instead, persist the report using the Cache object. There is only a single instance of the Cache object on the server and all users share it.

The following code demonstrates saving a report to the Cache or retrieving it if it already exists.

Listing 17.17. Using the Cache to share a report.
[VB.NET]
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Dim myReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
'Check if the report already exists or not
If Cache("CustomerReport") IsNot Nothing Then
'The report is already in the Cache, so load it
myReport = CType(Cache("CustomerReport"), _
CrystalDecisions.CrystalReports.Engine.ReportDocument)
Else
'Create the report and save it to the Cache
myReport.Load(Server.MapPath("CustomerReport.rpt"))
Cache.Insert("CustomerReport", myReport)
End If
CrystalReportViewer1.ReportSource = myReport
End Sub
[C#]
protected void Page_Init(object sender, System.EventArgs e)
{
CrystalDecisions.CrystalReports.Engine.ReportDocument myReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
//Check if the report already exists or not
if (Cache["CustomerReport"] != null)
{
//The report is already in the Cache, so load it
myReport = (CrystalDecisions.CrystalReports.Engine.ReportDocument)Cache["CustomerReport"];
}
else
{
//Create the report and save it to the Cache
myReport.Load(Server.MapPath("CrystalReport.rpt"));
Cache.Insert("CustomerReport", myReport);
}
CrystalReportViewer1.ReportSource = myReport;
}

The code first checks to see if the Cache object has an instance of the Customer Report in its collection. If so, it assigns it to the myReport variable. Notice that it uses the CType() function to cast the object as a ReportDocument class.

If the report doesn’t exist in the Cache object, a new copy of the report is instantiated and saved into the Cache object using the Insert() method. It is now available to be reused by the current user as well as any other user on the site.

You might be surprised to see that this code doesn’t test whether the Page.IsPostBack() property is true or false. Typically, web pages check if a page is being posted back to itself or not to determine whether it is necessary to reload certain objects or not. Since the Cache object is server based and not user based, it doesn’t matter whether the user is loading the current page for the first time or not. It is only necessary to check whether the report is in the Cache object and act accordingly.