NCache 4.4 - Online Documentation

Using NCache View State Caching

 
Content Optimization Feature of NCache
 
Following are the two steps that need to be performed in order to use content optimization feature of NCache:
 
Step 1: Update/Configure App Browser File
 
  • Create an App browser file in the ASP.NET application. It will be created under the directory of App_browsers.
  • Now plug page adapters in the app browser file as following:
 
<browsers>
<!-- NCache Plug page adapters in the app browser file as following:. -->
  <browser refID="Default">
    <controlAdapters>
<adapter controlType="System.Web.UI.Page"
adapterType="Alachisoft.NCache.Adapters.PageAdapter" />
    </controlAdapters>
  </browser>
</browsers>
 
 
This is an example of page adapter plugged at page level. You can also plug it at any control level for which you want to cache the view state.
 
 
Step 2:  Update/Configure Web.config File
 
  • Add the following assembly reference in compilation section of web.config file
 
For Enterprise and Professional Editions:-
<compilation defaultLanguage="c#" debug="true" targetFramework= "4.0">
  <assemblies>
<add assembly="Alachisoft.NCache.Adapters, Version=1.0.0.0, Culture=neutral, PublicKeyToken=CFF5926ED6A53769"/>
  </assemblies>
</compilation>
 
For Open Source Edition:-
<compilation defaultLanguage="c#" debug="true" targetFramework= "4.0">
  <assemblies>
<add assembly="Alachisoft.NCache.Adapters, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1448e8d1123e9096"/>
  </assemblies>
</compilation>
 
  • A web config section needs to be registered which contains the following settings/properties in web.config file.
 
<configSections>
<!--NCache Register config section first. --> 
    <sectionGroup name="ncContentOptimization">
<section name= "settings" type= "Alachisoft.NCache.ContentOptimization.Configurations.ContentSettings" allowLocation="true" allowDefinition= "Everywhere"/>
    </sectionGroup>
</configSections> 
 
  • Add the actual setting/configuration section as follows:
 
<!--NCache actual setting/ configuration for view state 
<ncContentOptimization>
  <settings enableViewstateCaching="true" viewstateThreshold="1" cacheUriMapping="true" enableTrace="false" >
    <cacheSettings cacheName="mycache” connectionRetryInterval="300">
          <expiration type="Absolute" duration="1" />
    </cacheSettings>
  </settings>
</ncContentOptimization>
 
Configuration Members
   
Member
Description
enableViewstateCaching
Boolean value to enable /disable the ViewState caching.
 
viewstateThreshold
Sets the minimum threshold size of viewstate in bytes. Viewstates which are smaller than threshold value will not be cached.
cacheUriMapping
Cache URL mapping, Default is true.
 
enableTrace
Enable/disable traces.
 
cacheName
Name of the Cache.
connectionRetyInterval
Retry Connection to Cache interval. Default is 300 seconds.
 
Expiration
Sets the expiration type which can either be Sliding, Absolute or None. And also expiration interval in minutes.
 
Duration
Sets expiration interval in minutes.
 
  • In the end, register the HTTP handler in the HttpHandlers section of web.config as following: 
 
For Enterprise and Professional Editions:-
<add verb="GET,HEAD" path="NCResource.axd" validate="false" type="Alachisoft.NCache.Adapters.ContentOptimization.ResourceHandler, Alachisoft.NCache.Adapters, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cff5926ed6a53769"/>
 
For Open Source Edition:-
<add verb="GET,HEAD" path="NCResource.axd" validate="false" type="Alachisoft.NCache.Adapters.ContentOptimization.ResourceHandler, Alachisoft.NCache.Adapters, Version=1.0.0.0, Culture=neutral, PublicKeyToken=144
8e8d1123e9096"/>
 
If the same cache is being used for View State caching and other data, viewstate data can be retrieved by NC_ASP.net_viewstate_data tag. With this tag it is easy to find the data specific to viewstate.
 
Hashtable allViewStateData = cache.GetByTag(new Alachisoft.NCache.Runtime.Caching.Tag("NC_ASP.net_viewstate_data"));
 
The view state of a particular session can easily be retrieved from cache based on sessionId.
 
How to Group View State with Session
 
NCache provides a feature that allows to group view state with a particular session. Due to this dependency, as the session is expired, the view state associated with it expires too. The view state and session can reside either on two different caches or on a single cache.  To enable this feature, expireViewstateWithSession should be set as true in ncContentOptimization tag.
 
<ncContentOptimization>
  <settings enableMinification="false" enableViewstateCaching="true" groupedViewStateWithSessions="true" viewstateThreshold="1" cacheUriMapping="true" enableTrace="true" expireViewstateWithSession="true" sessionCacheName="vscache" sessionAppId="SSP">
    <cacheSettings cacheName="vscache2">
       <expiration type="None" duration="0" />
    </cacheSettings>
  </settings>
</ncContentOptimization>
 
Configuration Members
 
Member
Description
groupedViewStateWithSessions
Through this property session can be grouped with view state. All the viewstate for a session is grouped with sessionId. This allows to retrieve all viewstate grouped with a sessionId from the cache with group information. Set this property to "true" in order to enable viewstate grouping with sessions. By default, its value is false.
 
expireViewstateWithSession
By setting this property as true, it is made sure that when a session expires, its associated view state expires too.
sessionAppId
Specifies an identifier to make sure that session ID remains unique in case multiple applications are using the same cache. Its value should be the same as the sessionAppId in sessionState tag.
sessionCacheName
The name of the cache on which the session data is residing. Its value should be the same as the cacheName property in sessionState tag.
 
 
How to Limit View State Caching
 
Following are the configuration changes:
 
<ncContentOptimization>
  <settings enableViewstateCaching="true" viewstateThreshold="0" cacheUriMapping="true" enableTrace="true" groupedViewStateWithSessions="true" maxViewStatesPerSession="5" enableMetadataLocking="false" releaseLockInterval="5000">
  <cacheSettings cacheName="mycache">
    <expiration type="None"duration="0" />
  </cacheSettings>
  </settings>
</ncContentOptimization>
 
 
Configuration Members
 
Members
Description
MaxViewStatesPerSession
To enable this feature specify any value greater than zero, if zero or lesser value is configured all view state will be cached. 
If a tag is skipped, all view state will be cached.
EnableMetadataLocking
Defines whether internal metadata manipulation is a locking operation or not. Its default value is true.
It comes into play if MaxViewStatesPerSession is enabled.
ReleaseLockInterval
Defines interval after which lock is released. Its value is in milliseconds. Its default value is 5000ms.
It comes into play if MaxViewStatesPerSession is enabled.
 
 
How to Perform Page level Grouping for View State
 
The following configurations should be made in web.config file. Also add the property webAppId in settings as follows:
 
 <ncContentOptimization>
  <settings webAppId="WebApp_1" enableViewstateCaching="true" groupedViewStateWithSessions="trueviewstateThreshold="12" enableTrace="true" maxViewStatesPerSession="10">
    <cacheSettings cacheName = "mycache">
      <expiration type="Sliding" duration = "300"/>
    </cacheSettings>
  </settings>
</ncContentOptimization> 
 
Configuration Members
 
Members
Description
webAppId
Specifies the web application Id where the application is currently running. When webappId is specified, NCache looks up for ViewState.xml file to read configurations. If webAppId is not specified, view states will not be grouped and the default settings from web.config will be used.
 
ViewState.xml
 
ViewState.xml is required for this feature. This view state configuration file (ViewState.xml) can be placed at NCache installation config directory C:\Program Files\NCache\config  or at web application bin path,e.g., C:\\ASP App\bin.
The web application id provided in web.config is matched with web application id in ViewState.xml file. From ViewState.xml detailed configurations for each group are then loaded.
 
The configurations provided in the web.config file are loaded as default settings. The configurations in this xml file are loaded per group. Here is a sample ViewState.xml file:
 
<configuration>
  <viewstateConfig>
    <webApp webAppId="aspViewState_A ">
      <viewstateGroup groupId="Group_A" cacheUriMapping="false" enableViewstateCaching="True" viewstateThreshold="12" groupedViewStateWithSessions="false" maxViewStatePerSession="20">
        <cacheSettings cacheName="myCache" connectionRetryInterval="300">
            <expiration type="Sliding"duration="5"/>
        </cacheSettings>
        <pages>
          <page url=”…/leftmenu.aspx"/><!—Relative url of a page-->
        </pages>
      </viewstateGroup>
    <viewstateGroup groupId="Group_B" cacheUriMapping="false" enableViewstateCaching="true" viewstateThreshold="23" groupedViewStateWithSessions="false" maxViewStatePerSession="5" enableMetadataLocking="True" releaseLockInterval="5">
      <cacheSettings cacheName="replicatedCache" connectionRetryInterval="300">
        <expiration type="Sliding" duration="20"/>
      </cacheSettings>
          <pages>
            <page url="…/testcaselist.aspx"/>
            <page url="…/ViewState/ViewState_SessionGroup.aspx"/>
          </pages>
    </viewstateGroup>
</webApp>
  <webApp webAppId="aspViewState_B">
    <viewstateGroup groupId="Group_A" enableViewstateCaching="True" viewstateThreshold="2" groupedViewStateWithSessions="false" maxViewStatePerSession="200”>
      <cacheSettings cacheName="cache">
        <expiration type="Sliding" duration="20"/>
      </cacheSettings>
          <pages>
            <page url="…/menu/viewstate.aspx"/>
          </pages>
      </viewstateGroup>
    </webApp>
  </viewstateConfig>
</configuration>
 
    Configuration Members
   
Members
Description
webAppId
Used to distinguish multiple web app sections.(compulsory)
 
GroupId
Used to distinguish multiple groups. (compulsory)
 
page url
Relative URL for a page. (compulsory)
 
 
 
Please keep the following important points in mind.
 
  • The XML file can have multiple web app configurations. In each web app configuration, there can be one or more group configurations.
  • All the web pages for a certain group are provided in <pages> section and page url is the relative url of a page.
  • If a page configuration is not found in view state configuration file, default settings provided in web.config will be used.
  • If any property is missing for any group, default property value provided in web.config will be used.
  • Different caches can also be configured for different groups.
 
Default and Group Setting Usage:
 
If web.config provides a web app id, NCache will look for viewstate.xml file to read configuration. In this case, default settings will only be used if configuration for a page is not found in viewstate.xml or if some optional property is missing in viewstate.xml. But if web.config settings does not include web app ID, default settings will be used for all view state caching.
 
 
See Also