ComponentOne WebData for ASP.NET:Using WebDataObjects Enterprise Edition > Using the Session Cache in Enterprise Edition

Using the Session Cache in Enterprise Edition

You can use the global cache for some data sets and the session cache for others. You don't need to change any settings in order to use the session cache. The session cache is used by calling SessionCache methods.

The usual pattern is to replace C1DataSet.Fill calls with the following:

      Visual Basic

If Not SessionCache.Fill(c1DataSet1) Then

        c1DataSet1.Fill()

    SessionCache.SaveData(c1DataSet1)

End If

      C#

if (!SessionCache.Fill(c1DataSet1))

{

       c1DataSet1.Fill();

       SessionCache.SaveData(c1DataSet1);

}

This code first tries to fill the data set with data from the session cache. If this is executed for the first time in the user session, then the session cache does not yet contain data c1DataSet1, and the data is fetched from the database with C1WebDataSet1.Fill(). After fetch, we save the data in the session cache for future use.

Note: Make sure that C1WebDataSet1.FillOnRequest is set to False, to prevent C1WebDataSet from filling the data set automatically when you bind controls to it.

If you modify data in a data set that uses the session cache, you are responsible for saving the modified data set in the session cache if you need (and you usually do) the modifications to be persistent, to be restored next time you restore the data set from session cache. Usually, you update the session cache after you are done modifying the data set by calling the SaveData method:

      Visual Basic

SessionCache.SaveData(c1DataSet1)

      C#

SessionCache.SaveData(c1DataSet1);

SessionCache.SaveData has a cacheStorage argument that is one of two possible values: File or Memory. If the argument is omitted, the value specified in C1SchemaDef.SessionCacheProperties.DefaultStorage is used. The default is Memory.

If you use cacheStorage = File, you will usually need to set the SessionCacheProperties.StoragePath property of the C1SessionDef component to point to a directory where your session cache files are stored. If you use a multi-server configuration (Web farm), you must include a server name in the path to ensure that all cache files are stored on the same server (usually called state server). If you fail to provide a server name, data will be cached on the server that is serving current user requests. That can cause data inconsistency when requests are assigned to different servers. By default, if you leave SessionCacheProperties.StoragePath empty, the temporary directory of the current server is used.

Finally, if you use cacheStorage = File, you must include a clean-up line in your application Global.aspx file:

      Visual Basic

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)

  SessionCache.DeleteFiles(Me)

End Sub

      C#

protected void Session_End(Object sender, EventArgs e)

{

  SessionCache.DeleteFiles(this);

}

This is necessary to clean up the cache files when a session is closed. It is necessary only if you use cacheStorage = File, but it is a good idea to always call this method in the Session_End event.

For additional information on using the session cache, see Using the Session Cache in the WebDataObjects for ASP.NET Task-Based Help.


Send comments about this topic to ComponentOne.
Copyright © 1987-2010 ComponentOne LLC. All rights reserved.