The question of scalability is always front and center for major web apps. If scalability is of importance to your organization, you may need to implement a caching strategy. As it relates to the DevExpress Web Document Viewer, caching will require your application to store temporary states in a cache. For distributed systems, you will need a cache storage system to keep all nodes in sync. In this article, we will review the cache management capabilities of the DevExpress Web Document Viewer and demonstrate how they can be used to optimize application performance.
Caching Levels
The DevExpress Web Document Viewer uses two levels of caching: short-term (in-memory cache) and long-term (storage).
Short-term Cache (In-memory Cache)
Short-term cache resides in memory and stores object models for faster execution of frequently used operations. It caches report layouts and documents as objects that are later serialized and moved to long-term storage (when needed).
Long-term Cache (Storage)
Long-term cache stores serialized data in order to retrieve objects on demand. DevExpress Reporting offers various storage options for long-term cache, including file storage, database storage (based on XPO), the ASP.NET Core distributed cache (SQL Server, Redis), and Azure Cloud Storage.
Cache Management
With that brief intro to cache levels, let's look at the implementation details. DevExpress Reporting allows you to configure its cache based on selected storage type (such as the UseDbStorage
method for a database cache or the the UseDistributedCache
method for distributed cache). Selecting storage type allows the DevExpress Web Document Viewer to utilize the specified cache for efficient caching and retrieval of report layouts, generated documents and exported files.
For more information, review the following help topic: Web Document Viewer Cache Management.
Distributed Cache in ASP.NET Core Apps
The UseDistributedCache
method enables the use of distributed caching within an ASP.NET Core application. Different distributed cache implementations are available, including SQL Server, Redis, and NCache. For more information, review the following help topic: Distributed caching in ASP.NET Core.
To implement distributed cache within ASP.NET Core Apps (for the DevExpress Web Document Viewer), you must:
Step 1: Install Required Packages
Ensure that necessary packages are installed in your ASP.NET Core project. You will need the following packages:
- DevExpress.AspNetCore.Reporting
- Microsoft.Extensions.Caching.SqlServer (for SQL Server cache) or Microsoft.Extensions.Caching.StackExchangeRedis (for Redis cache)
Step 2: Configure the Distributed Cache
In your ASP.NET Core application, configure and enable distributed cache as described in the following guide: Distributed caching in ASP.NET Core.
Step 3: Enable Distributed Cache in DevExpress Web Document Viewer
In the same Startup.cs file, locate the ConfigureServices
method and find the services.ConfigureReportingServices
block. Within that block, add the following code to enable the distributed cache:
services.ConfigureReportingServices(builder =>
{
builder.ConfigureWebDocumentViewer(viewerBuilder =>
{
viewerBuilder.UseDistributedCache();
});
});
Database Cache (XPO-based Storage)
Unlike the distributed cache, which is only available for ASP.NET Core, this type of cache is available for all supported web platforms.
The UseDbStorage method enables the use of database cache storage. It requires a connection string to an existing database. The cache is based on XPO Data Providers and supports multiple relational database management systems. To implement the Database Cache, you must:
Step 1: Use the Database Cache in Web Document Viewer
In the Startup.cs file, locate the ConfigureServices
method and find the services.ConfigureReportingServices
block. Within that block, add the following code to enable the database caching:
public void ConfigureServices(IServiceCollection services) {
//…
services.ConfigureReportingServices(configurator => {
configurator.ConfigureWebDocumentViewer(viewerConfigurator => {
viewerConfigurator.UseCachedReportSourceBuilder();
viewerConfigurator.UseDbStorage("XpoStorageConnection");
//…
});
//…
}
Replace XpoStorageConnection with the connection string for your selected database. Make certain that you have a database set up and the appropriate XPO database provider installed.
Step 2: Initialize the Database
Initialize database tables and schema using the IStorageDbInitializer
interface:
public class Program {
public static void Main(string[] args) {
IWebHost host = CreateWebHostBuilder(args).Build();
if (args.Any(s => s.ToLower() == "--initDatabase")) {
using (var scope = host.Services.CreateScope()) {
var services = scope.ServiceProvider;
services.GetRequiredService<IStorageDbInitializer>().InitDbSchema();
}
}
host.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) {
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
File Cache
As you might guess, the time it takes to generate a document from a cached report source will vary based on the performance of the file storage system.
To cache report documents as files, use the UseFileDocumentStorage, UseFileExportedDocumentStorage, UseFileReportStorage, and UseCachedReportSourceBuilder methods as necessary.
Azure Caching
If you'd like to cache report documents using options offered by Azure, please refer to the following help topic: Web Farms and Web Garden Support.
Cache Expiration Strategies
Caching is effective if properly managed – including the length of time cached report layouts and documents are retained. DevExpress Reporting offers cache expiration options. Use CacheCleanerSettings and StorageCleanerSettings, to achieve a balance between performance and memory consumption in your applications with DevExpress Reports.