Using the Session Cache in Express Edition
You can use the global cache for some C1WebExpressConnection components and session cache for others. You do not 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 C1WebExpressConnection.Fill calls with the following:
If Not SessionCache.Fill(c1WebExpressConnection1) Then
c1WebExpressConnection1.Fill()
SessionCache.SaveData(c1WebExpressConnection1)
End If
• C#
if (!SessionCache.Fill(c1WebExpressConnection1))
{
c1WebExpressConnection1.Fill();
SessionCache.SaveData(c1WebExpressConnection1);
}
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 c1WebExpressConnection1, and the data is fetched from the database with c1WebExpressConnection1.Fill(). After fetch, we save the data in the session cache for future use.
Note: Make sure that c1WebExpressConnection1.FillOnRequest is set to False, to prevent C1WebDataObjects from filling the data set automatically when you bind controls to it.
If you modify data in a data set that uses 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 the next time you restore the data set from session cache. Usually, you update the session cache after you are done with modifying the data set, calling:
SessionCache.SaveData(c1WebExpressConnection1)
• C#
SessionCache.SaveData(c1WebExpressConnection1);
SaveData has a cacheStorage argument that is one of two possible values: File or Memory. If the argument is omitted, the value specified in 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 c1WebExpressConnection1 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 the current user request. 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:
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.
|