This example shows how to configure report caching. Here is three examples of caching report - Default, File, MS SQL.

First, you need to add the StiNetCoreViewer component to the view page. Also, you need to pass the StiNetCoreViewerOptions object to the constructor. The minimum required options are two actions - GetReport and ViewerEvent:
@using Stimulsoft.Report.Mvc;

...

@Html.StiNetCoreViewer(new StiNetCoreViewerOptions()
{
    Actions =
    {
        GetReport = "GetReport",
        ViewerEvent = "ViewerEvent"
    }
})

Let's start with Default caching. First, create StiDefaultCacheHelper class:
public class StiDefaultCacheHelper : StiCacheHelper
{
	public override StiReport GetReport(string guid)
	{
		return base.GetReport(guid);
	}

	public override void SaveReport(StiReport report, string guid)
	{
		base.SaveReport(report, guid);
	}

	public override void RemoveReport(string guid)
	{
		base.RemoveReport(guid);
	}
}

Then, create DefaultCacheController method:
public DefaultCacheController() {
	StiNetCoreViewer.CacheHelper = new StiDefaultCacheHelper();
}

Finally, create report loading method:
public IActionResult GetReport(int id = 1)
{
	var report = new StiReport();
	report.Load(StiNetCoreHelper.MapPath(this, "Reports/TwoSimpleLists.mrt"));
	return StiNetCoreViewer.GetReportResult(this, report);
}

Next, File caching. First, create StiFileCacheHelper class:
public class StiFileCacheHelper : StiCacheHelper
{
	public override StiReport GetReport(string guid)
	{
		var path = Path.Combine(HttpContext.Server.MapPath("CacheFiles"), guid);
		if (File.Exists(path))
		{
			var report = new StiReport();
			var packedReport = File.ReadAllText(path);
			if (guid.EndsWith(GUID_ReportTemplate)) report.LoadPackedReportFromString(packedReport);
			else report.LoadPackedDocumentFromString(packedReport);

			return report;
		}
		return null;
	}

	public override void SaveReport(StiReport report, string guid)
	{
		var packedReport = guid.EndsWith(GUID_ReportTemplate) ? report.SavePackedReportToString() : report.SavePackedDocumentToString();
		var path = Path.Combine(HttpContext.Server.MapPath("CacheFiles"), guid);
		File.WriteAllText(path, packedReport);
	}

	public override void RemoveReport(string guid)
	{
		var path = Path.Combine(HttpContext.Server.MapPath("CacheFiles"), guid);
		if (File.Exists(path))
			File.Delete(path);
	}
}

Then, create FileCacheController method:
public FileCacheController()
{
	StiNetCoreViewer.CacheHelper = new StiFileCacheHelper();
}

Finally, create report loading method:
public IActionResult GetReport(int id = 1)
{
	var report = new StiReport();
	report.Load(StiNetCoreHelper.MapPath(this, "Reports/TwoSimpleLists.mrt"));
	return StiNetCoreViewer.GetReportResult(this, report);
}

The last, MS SQL caching. First, create StiMSSQLCacheHelper class:
public class StiMSSQLCacheHelper : StiCacheHelper
{
	// Please use your own database connection
	private string connectionString = @"Data Source=127.0.0.1;Initial Catalog=SampleDB;Integrated Security=False;User ID=******;Password=******;";

	public override StiReport GetReport(string guid)
	{
		var connection = new SqlConnection(connectionString);
		try
		{
			connection.Open();

			var query = $"SELECT Guid, Value FROM ReportCache WHERE Guid='{guid}'";
			var command = new SqlCommand(query, connection);
			var reader = command.ExecuteReader();

			if (reader.HasRows)
			{
				reader.Read();
				var packedReport = reader.GetString(1);
				reader.Close();

				var report = new StiReport();
				if (guid.EndsWith(GUID_ReportTemplate)) report.LoadPackedReportFromString(packedReport);
				else report.LoadPackedDocumentFromString(packedReport);

				return report;
			}
		}
		catch (SqlException ex)
		{
			Console.WriteLine(ex.Message);
		}
		finally
		{
			connection.Close();
		}

		return null;
	}

	public override void SaveReport(StiReport report, string guid)
	{
		var connection = new SqlConnection(connectionString);
		try
		{
			connection.Open();

			var query = $"DELETE FROM ReportCache WHERE Guid='{guid}'";
			var command = new SqlCommand(query, connection);
			command.ExecuteNonQuery();

			var packedReport = guid.EndsWith(GUID_ReportTemplate) ? report.SavePackedReportToString() : report.SavePackedDocumentToString();
			query = $"INSERT INTO ReportCache (Guid, Value) VALUES ('{guid}', '{packedReport}')";
			command = new SqlCommand(query, connection);
			command.ExecuteNonQuery();
		}
		catch (SqlException ex)
		{
			Console.WriteLine(ex.Message);
		}
		finally
		{
			connection.Close();
		}
	}

	public override void RemoveReport(string guid)
	{
		var connection = new SqlConnection(connectionString);
		try
		{
			connection.Open();

			var query = $"DELETE FROM ReportCache WHERE Guid='{guid}'";
			var command = new SqlCommand(query, connection);
			command.ExecuteNonQuery();
		}
		catch (SqlException ex)
		{
			Console.WriteLine(ex.Message);
		}
		finally
		{
			connection.Close();
		}
	}
}

Then, create MSSQLCacheController method:
public MSSQLCacheController()
{
	StiNetCoreViewer.CacheHelper = new StiMSSQLCacheHelper();
}

Finally, create report loading method:
public IActionResult GetReport(int id = 1)
{
	var report = new StiReport();
	report.Load(StiNetCoreHelper.MapPath(this, "Reports/TwoSimpleLists.mrt"));
	return StiNetCoreViewer.GetReportResult(this, report);
}

In the screenshot below you can see the result of the sample code:

Configuring Report Caching

By using this website, you agree to the use of cookies for analytics and personalized content. Cookies store useful information on your computer to help us improve efficiency and usability. For more information, please read the privacy policy and cookie policy.