Manage an In-Memory Data Cache in ASP.NET Web Forms
- 6 minutes to read
This article describes the dashboard’s data cache functionality, corresponding API, and how to update the cached data.
The ASPxDashboard control can display dashboards supplied with data from different types of data sources, which include SQL databases, Excel files, Entity Framework data sources, and so on. The ASPxDashboard manages cache as follows depending on the data processing mode:
- Server
ASPxDashboard requests data automatically by sending a query that contains a corresponding SELECT statement.
- Client
ASPxDashboard creates a server-side in-memory cache that stores data from the data source in an optimized way. This capability accelerates specific client-side actions that require data updates (for instance, when you apply master filtering).
Share Cache
The Web Dashboard can create multiple caches to accelerate data loading for different dashboards/data sources. The following specifics are applied to cache sharing depending on the server-side API you use in your application:
- ASPxDashboard’s API
- For the ASPxDashboard control that uses its own server-side API (ASPxDashboard.UseDashboardConfigurator is
false
), the cache is shared across all users of the control’s instance. - DashboardConfigurator’s API
- For the ASPxDashboard control that uses DashboardConfigurator’s API (ASPxDashboard.UseDashboardConfigurator is
true
), the cache is shared across all users of all ASPxDashboard instances.
Use Different Caches for Different User Groups
The Web Dashboard can create multiple caches to accelerate data loading for different dashboards/data sources. Each cache has its own unique key that contains predefined and custom parts:
- The first part describes the dashboard object model, including information about the data source.
- The second part includes dashboard parameters (name-value pairs). You handle the ASPxDashboard.CustomParameters / DashboardConfigurator.CustomParameters event to create different caches.
The caches are shared between all users by default:
You can manage access to different caches for different users. For this, set a unique parameter value for each user group:
To do this in code, handle the ASPxDashboard.CustomParameters / DashboardConfigurator.CustomParameters event as follows:
- ASPxDashboard’s API
- DashboardConfigurator’s API
The following example shows how to use a custom parameter to manage access to different caches for different users:
// Configure user-specific data caching private void DashboardConfigurator_CustomParameters(object sender, CustomParametersWebEventArgs e) { var userId = contextAccessor.HttpContext.Session.GetString("CurrentUser").GetHashCode(); e.Parameters.Add(new Parameter("UserId", typeof(string), userId)); }
The parameter’s name-value pair is included in the cache key–users get the cache only for the corresponding user group. Encode the passed parameter value if possible. Do not store any sensitive information in dashboard parameters that isn’t encrypted.
Specify the Cache Timeout on the Server
When the Web Dashboard loads data for the first time, it checks whether any cache exists. If the existing cache is older than 300 seconds, the Web Dashboard recreates it. The Web Dashboard manages this cache in the following independent ways:
- Default time interval
- The Web Dashboard clears the cache if the time interval between two data requests exceeds 300 seconds. If the time interval between two data requests is less than 300 seconds, the Web Dashboard leaves the old cache unchanged and starts the timer again.
- Specified time interval
- The Web Dashboard allows you to set a time interval that specifies how frequently the Web Dashboard should refill its cache when a user sends a data request. You can handle the ASPxDashboard.ConfigureDataReloadingTimeout / DashboardConfigurator.ConfigureDataReloadingTimeout event to change this time interval. For instance, if you set the e.DataReloadingTimeout event parameter to 100 seconds, the Web Dashboard updates the cache when a new request is received and the current cache version was created more than 100 seconds ago.
Refresh the Control on the Client
When you call the client-side DashboardControl.refresh method, the control requests the latest data from the server and reloads the dashboard items. If a cache exists, the control fetches data from the cache.
Get access to the underlying DashboardControl instance and call the refresh
method as follows:
function onClick() {
clientDashboardControl.refresh();
}
To refresh specific dashboard items, pass their names as a parameter. The code below refreshes Chart and Grid items:
function onClick() {
clientDashboardControl.refresh(["chartDashboardItem1", "gridDashboardItem1"]);
}
See the following topic for details on how to customize ASPxDashboard on the client: Client-Side API Overview.
Force the Cache Reset
Client Side
To update the data source cache on the client, call the client-side DashboardControl.reloadData method. The method call sends a callback to the server. This forces the cache reset and recreates a new one. Note that multiple users can simultaneously open the dashboard and share the same server-side cache. If a user calls the reloadData
method, it forces the server cache to be reset and all users get new data on the next request to the server.
function onClick() {
clientDashboardControl1.GetDashboardControl().reloadData();
}
See the following topic for details on how to customize ASPxDashboard on the client: Client-Side API Overview.
Server Side
To refresh the data source cache on the server, pass a unique parameter value to the ASPxDashboard.CustomParameters / DashboardConfigurator.CustomParameters event.
For instance, you can store a unique GUID value within a Session or static variable as a parameter and update its value in your code when you need to refresh the cache.
public class CacheManager {
public static void ResetCache() {
if(HttpContext.Current.Session != null)
HttpContext.Current.Session["UniqueCacheParam"] = Guid.NewGuid();
}
public static Guid UniqueCacheParam {
get {
if(HttpContext.Current.Session == null)
return Guid.Empty;
else {
if(HttpContext.Current.Session["UniqueCacheParam"] == null)
ResetCache();
return (Guid)HttpContext.Current.Session["UniqueCacheParam"];
}
}
}
}
Pass the parameter to the DashboardConfigurator.CustomParameters / ASPxDashboard.CustomParameters event:
- ASPxDashboard’s API
- DashboardConfigurator’s API
private static void Default_CustomParameters(object sender,CustomParametersWebEventArgs e) { e.Parameters.Add(new DashboardParameter("UniqueCacheParam",typeof(Guid),CacheManager.UniqueCacheParam)); }
Reset the cache in your code:
protected void ASPxButton1_Click(object sender, EventArgs e) {
CacheManager.ResetCache();
}
Example: How to Reset the Cache
The following example shows how you can manage the cache in the ASP.NET Web Forms Dashboard control.
- Click Refresh Cache (Server) to force the cache reset on the server.
- Click Refresh Cache (Client) to force the cache reset using the client API.