Using CacheProperties to Specify Global Cache Settings
The CacheProperties class is used to specify global cache settings for a DataSetDef class using its CacheProperties property.
Global cache is the default caching mode. It is used by default for every WebDataObjects for ASP.NET data set if you do not change the cache settings and do not call SessionCache methods.
To disable caching for a data set completely, set CacheProperties.CacheMode = NotCached.
Here is some example code that can be used with global cache.
1. Enter the following in the Page_Load event handler:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' c1WebProductsDataSet (ProductsDataSet) uses global
' cache
' See in Schema Designer: SessionProperties.CacheMode =
'InWebForms.
c1WebProductsDataSet.Fill() 'global cache works
'automatically when you call C1WebDataSet.Fill
End Sub
• C#
private void Page_Load(object sender, System.EventArgs e)
{
// c1WebProductsDataSet (ProductsDataSet) uses global
//cache
// See in Schema Designer:
//SessionProperties.CacheMode = InWebForms.
c1WebProductsDataSet.Fill();
// global cache works automatically when you call
// C1WebDataSet.Fill
}
2. Save changes in the global cache every time you change data in the data set:
Public Sub DataGrid1_Update(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
GlobalCache.SaveChanges(c1WebProductsDataSet) ' save
' changes in global cache every time you change data in
'the data set
End Sub
• C#
public void DataGrid1_Update(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
GlobalCache.SaveChanges(c1WebProductsDataSet);
// save changes in global cache every time you change
// data in the data set
}
3. If you need to refresh data from the database, the global cache should be cleared:
Private Sub btnRefresh_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
' We need to clear global cache first to ensure the data is fetched from the database, not from cache.
GlobalCache.Remove(c1WebProductsDataSet)
' fetch data from the database
c1WebProductsDataSet.Fill()
ViewState.Remove("SortExpression")
BindGrid()
End Sub
• C#
private void btnRefresh_Click(object sender, System.EventArgs e)
{
// We need to clear global cache first to ensure the
// data is fetched from the database, not from cache.
GlobalCache.Remove(c1WebProductsDataSet);
// fetch data from the database
c1WebProductsDataSet.Fill();
ViewState.Remove("SortExpression");
BindGrid();
}
4. Clean up the cache files when your application is shut down. Enter the following code in the Global.asax:
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
GlobalCache.DeleteFiles(Me)
End Sub
• C#
protected void Application_End(Object sender, EventArgs e)
{
GlobalCache.DeleteFiles(this);
}
Sample
Available
For additional information see the DataOnTheWeb sample, which is available for download from the ComponentOne HelpCentral Sample page.
|