我在我的 ASP.Net MVC2 应用程序中使用 AppFabric 作为会话状态提供程序,并且我希望它也使用本地缓存。在 configSections 节点之后,我的 web.config 中有以下条目:
<dataCacheClient>
<localCache
isEnabled="true"
sync="TimeoutBased"
objectCount="100000"
ttlValue="300" />
<hosts>
<host name="127.0.0.1" cachePort="22233"/>
</hosts>
</dataCacheClient>
我在 web.config 中也有以下条目作为 system.web 节点的子节点:
<sessionState mode="Custom" customProvider="AppFabricCacheSessionStoreProvider">
<providers>
<add name="AppFabricCacheSessionStoreProvider" type="Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider" cacheName="Default" sharedId="DefaultId"/>
</providers>
</sessionState>
不幸的是,如果我在会话中添加一些内容,然后在 AppFabric PowerShell 中运行以下两个命令,我添加到会话数据中的任何内容都不再存在,这让我相信它没有使用本地缓存:
Stop-CacheCluster
Start-CacheCluster
我还尝试使用以下代码使用 AppFabric 缓存对象,并且在启动和停止 CacheCluster 后,不再缓存曾经缓存的对象:
var factory = new DataCacheFactory();
var cache = factory.GetCache("Default");
cache.Put("Key", "Test");
但是,如果我使用以下代码实例化 AppFabric,我明确告诉它使用本地缓存而不是依赖 web.config 条目,它可以工作:
var servers = new List<DataCacheServerEndpoint>(1) { new DataCacheServerEndpoint("127.0.0.1", 22233) };
var configuration = new DataCacheFactoryConfiguration {
Servers = servers,
LocalCacheProperties = new DataCacheLocalCacheProperties(100000, new TimeSpan(0, 30, 0), DataCacheLocalCacheInvalidationPolicy.TimeoutBased)
};
var factory = new DataCacheFactory(configuration);
var cache factory.GetCache("StpWebSession");
cache.Put("Key", "Test");
我在做什么错,为什么我的 web.config 条目不能告诉 AppFabric 使用本地缓存?您可以使用 AppFabric 作为会话状态提供程序并让它使用本地缓存吗?