Back to Devexpress

ASPxDashboard.DataSourceCacheKeyCreated Event

dashboard-devexpress-dot-dashboardweb-dot-aspxdashboard-9674554f.md

latest5.8 KB
Original Source

ASPxDashboard.DataSourceCacheKeyCreated Event

Occurs when a data source cache key is created. Allows you to manage cache granularity.

Namespace : DevExpress.DashboardWeb

Assembly : DevExpress.Dashboard.v25.2.Web.WebForms.dll

NuGet Package : DevExpress.Web.Dashboard

Declaration

csharp
public event DataSourceCacheKeyCreatedEventHandler DataSourceCacheKeyCreated
vb
Public Event DataSourceCacheKeyCreated As DataSourceCacheKeyCreatedEventHandler

Event Data

The DataSourceCacheKeyCreated event's data class is DataSourceCacheKeyCreatedEventArgs. The following properties provide information specific to this event:

PropertyDescription
KeyProvides access to the data source cache key.

The event data class exposes the following methods:

MethodDescription
InvalidateCacheRecord()Invalidates the associated cache record.

Remarks

The Web Dashboard control uses a caching mechanism to optimize data requests. For information on the caching mechanism in the Web Dashboard Control, refer to the following topic: Manage an In-Memory Data Cache.

Each time the dashboard needs data, it creates a cache key. Handle the DataSourceCacheKeyCreated event to access this key or modify it according to your needs. If you need separate cache records for different users or need to refresh the cache record with the latest updates from the data source, handle the event and customize the key as needed.

The next sections describe cache-related scenarios and how to implement them with the DataSourceCacheKeyCreated event.

Remove Parameters from the Cache Key

In the DataSourceCacheKeyCreated event handler, you can remove the parameter collection or only the specified parameters from the cache key. Any value change in the dashboard parameter collection leads to the cache key recreation and excessive data requests. When dashboard parameters are not used to filter data sources, you can choose to exclude them from the cache key to optimize dashboard performance and reduce the number of data requests.

The following code snippet deletes the parameter collection from the cache key for the MyDashboard dashboard in the ASP.NET Web Forms Dashboard Control:

cs
protected void DataSourceCacheKeyCreated(object sender, DataSourceCacheKeyCreatedEventArgs args) {
    if (args.Key.DashboardId == "MyDashboard")
        args.Key.Parameters.Clear();
}
vb
Protected Sub DataSourceCacheKeyCreated(ByVal sender As Object, ByVal args As DataSourceCacheKeyCreatedEventArgs)
    If args.Key.DashboardId = "MyDashboard" Then
        args.Key.Parameters.Clear()
    End If
End Sub

Separate Cache for User Groups

Use the CustomData field to add custom information to the cache key. For example, you can 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.

The snippet below adds information about the current user to the cache key in the DataSourceCacheKeyCreated event handler:

cs
protected void DataSourceCacheKeyCreated(object sender, DataSourceCacheKeyCreatedEventArgs args) {
    args.Key.CustomData.Add("userGroup", "Sales");
}
vb
Protected Sub DataSourceCacheKeyCreated(ByVal sender As Object, ByVal args As DataSourceCacheKeyCreatedEventArgs)
    args.Key.CustomData.Add("userGroup", "Sales")
End Sub

Invalidate the Cache Record

Use the InvalidateCacheRecord() method to invalidate (reset) the cache record associated with 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 snippet invalidates the cache record for the sqlDataSource1 data source in the ASP.NET Web Forms Dashboard Control:

cs
protected void DataSourceCacheKeyCreated(object sender, DataSourceCacheKeyCreatedEventArgs args) {
    if (args.Key.DataSourceId == "sqlDataSource1")
        args.InvalidateCacheRecord();
}
vb
Protected Sub DataSourceCacheKeyCreated(ByVal sender As Object, ByVal args As DataSourceCacheKeyCreatedEventArgs)
    If args.Key.DataSourceId = "sqlDataSource1" Then
        args.InvalidateCacheRecord()
    End If
End Sub

See Also

Manage an In-Memory Data Source Cache in ASP.NET Web Forms

ASPxDashboard Class

ASPxDashboard Members

DevExpress.DashboardWeb Namespace