Manage an In-Memory Data Source Cache in ASP.NET Web Forms
- 6 minutes to read
This article describes cache-related API members and use cases related to the data source caching mechanism in the Web Dashboard control.
The Web Dashboard control uses in-memory data source cache to optimize the dashboard performance. In-memory cache represents a set of cache records. Each cache record consists of a cache key and a data source instance. Each data request results in a data source cache key creation. If there is no cache record with such key, a new cache record is created. Creation of a cache record fires the ConfigureDataConnection
or DataLoading
event (depending on data source configuration in your application). Use the DataSourceCacheKeyCreated
event to access and modify a data source cache key when it is created.
Disable Caching
Set the DashboardConfigurator.DataSourceCacheEnabled or ASPxDashboard.DataSourceCacheEnabled property to false
to disable data source caching. When caching is disabled, data is queried from the database each time a dashboard item requests data. ConfigureDataConnection
and DataLoading
events are fired every time data is requested.
The following code snippets disable the cache depending on the server-side API you use in your application:
- ASPxDashboard’s API
<dx:ASPxDashboard ID="ASPxDashboard1" runat="server" DataSourceCacheEnabled="false"> </dx:ASPxDashboard>
- DashboardConfigurator’s API
DashboardConfigurator.Default.DataSourceCacheEnabled = false;
Share Cache
The in-memory cache is created when data is loaded for the first time. The Web Dashboard can create multiple cache records 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.
Separate Cache for User Groups
The Web Dashboard can create multiple cache records to accelerate data loading for different dashboards/data sources. Each cache record has its own unique key that implements the IDataSourceCacheKey interface. Use the CustomData field to add information about user groups to the cache key to use separate cached datasets for different users. Note that the information stored in the cache key cannot be accessed from the client side.
Caches are shared among all users:
You can manage access to different caches for different users. For this, add information about user groups to the CustomData field.
- ASPxDashboard’s API
The following example uses the CustomData field to add information about a user to the cache key:
- DashboardConfigurator’s API
The following example uses the CustomData field to add information about a user to the cache key:
// Configure user-specific data caching private void DashboardConfigurator_DataSourceCacheKeyCreated(object sender, DataSourceCacheKeyCreatedEventArgs e) { var userId = contextAccessor.HttpContext.Session.GetString("CurrentUser").GetHashCode(); e.Key.CustomData.Add("UserId", userId.ToString()); }
Specify the Cache Timeout on the Server
When the Web Dashboard loads data for the first time, it checks whether any cache record exists. If a cache record 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 record 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 existing cache record unchanged and starts the timer again.
- Specified time interval
- The Web Dashboard allows you to set a time interval between two data requests, after which the cache record will be cleared. 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 recreates a cache record when a new request is received and the current cache record is older than 100 seconds.
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
Use the InvalidateCacheRecord() method to reset a cache record referenced by the current cache key. This way you can ensure that data is always requested from the server and users have access to the latest changes.
The following code snippets invalidate the cache record for the sqlDataSource1 data source :
- ASPxDashboard’s API
- DashboardConfigurator’s API
private static void Default_DataSourceCacheKeyCreated(object sender, DataSourceCacheKeyCreatedEventArgs e) { if(args.Key.DataSourceId == "sqlDataSource1") args.InvalidateCacheRecord(); }
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.
Server Side
On the server-side, the Web Dashboard uses the same cache record within a session. To implement the scenario, a value identifying the session is written to the CustomData field in the ASPxDashboard.DataSourceCacheKeyCreated or DashboardConfigurator.DataSourceCacheKeyCreated event handler.
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"];
}
}
}
}
Assign the parameter value to the CustomData field:
- ASPxDashboard’s API
- DashboardConfigurator’s API
private static void Default_DataSourceCacheKeyCreated(object sender, DataSourceCacheKeyCreatedEventArgs e) { e.Key.CustomData.Add("SessionId", CacheManager.UniqueCacheParam.ToString()); }
Reset the cache in your code:
protected void ASPxButton1_Click(object sender, EventArgs e) {
CacheManager.ResetCache();
}