一般情况下,session对象是放在服务器上的,其实也可把Session对象放到cache里做负载均衡,让Web应用程序通过cache集群使用Session对象,提供可扩展性..,需要注意的是放在Session对象要序列化。现在我们来看看怎么样配置session对象呢?假定已经准备好Web应用程序的开发环境,并设置为“Velocity”程序集的引用,等等。
怎么样配置session对像,使其放在指定计算机的集群里呢。为了让ASP.NET Web应用程序使用“Velocity”会话状态提供程序,您必须添加以下内容到应用程序的web.config文件。
configSections: 它是< configuration />子节点,这个节点必须放在< configuration />节点中的最前面,配置“Velocity”的程序集
dataCacheClient: 它也是< configuration />子节点,用来配置缓存客户端, 可以缓存到本机,也可以缓存到指定主机上.
sessionState: 它是< system.web>的元素, 它用来指定Web 应用程使用"Velocity" 来管理session. 其中cacheName 属性是你要使用的缓存名.如果你要把session放cache region里,你还必须要设置regionName 属性来指定是哪个region.
注:对象存储在一个region,将不缓存主机的负载平衡,但将在该region创建了缓存的主机位置。由于这个原因,它不是一般推荐的配置。region只应在有特殊要求,找到一台主机上所有的会话对象。在缓存中的数据是不加密的,可适当配置设置的任何客户端缓存。我们强烈建议您确保web.config中用于指定客户端的缓存文件。确保数据安全。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="dataCacheClient"
type="Microsoft.Data.Caching.DataCacheClientSection,
CacheBaseLibrary"
allowLocation="true"
allowDefinition="Everywhere"/>
<section name="fabric"
type="System.Data.Fabric.Common.ConfigFile,
FabricCommon"
allowLocation="true"
allowDefinition="Everywhere"/>
</configSections>
<dataCacheClient deployment="routing">
<!-- (optional) specify local cache
<localCache
isEnabled="true"
sync="TTLBased"
objectCount="100000"
ttlValue="300" />
-->
<hosts>
<host
name="CacheServer1"
cachePort="22233"
cacheHostName="DistributedCacheService"/>
</hosts>
</dataCacheClient>
<system.web>
<sessionState mode="Custom" customProvider="Velocity">
<providers> <add
name="Velocity"
type="Microsoft.Data.Caching.SessionStoreProvider"
cacheName="NamedCache1"/>
</providers>
</sessionState>
</system.web>
</configuration>