views:

127

answers:

1

I am using AppFabric as the session state provider in my ASP.Net MVC2 app, and I would like it to also use the local cache. I have the following entry in my web.config right after the configSections node:

<dataCacheClient>
    <localCache
         isEnabled="true"
         sync="TimeoutBased"
         objectCount="100000"
         ttlValue="300" />
    <hosts>
        <host name="127.0.0.1" cachePort="22233"/>
    </hosts>
</dataCacheClient>

I also have the following entry in web.config as a child of the system.web node:

<sessionState mode="Custom" customProvider="AppFabricCacheSessionStoreProvider">
    <providers>
        <add name="AppFabricCacheSessionStoreProvider" type="Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider" cacheName="Default" sharedId="DefaultId"/>
    </providers>
</sessionState>

Unfortunately if I add something to session then run the following two commands in the AppFabric powershell, anything I added to my session data is no longer there which leaves me to believe it was not using local cache:

Stop-CacheCluster
Start-CacheCluster

I also try caching objects with AppFabric using the following code and after I start and stop the CacheCluster the once cached object is no longer cached:

var factory = new DataCacheFactory();
var cache = factory.GetCache("Default");
cache.Put("Key", "Test");

However if I instantiate AppFabric using the following code where I explicitly tell it to use local cache rather than relying on the web.config entry it works:

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");

What am I doing wrong, why doesn't my web.config entry work in telling AppFabric to use local cache? Can you use AppFabric as your session state provider and also have it use local cache?

+1  A: 

I found a little ditty at http://social.msdn.microsoft.com/Forums/en-US/velocity/thread/24e72dab-bb20-47ec-aae2-77423b1d296b.

Basically, "enableSessionState" is "true" by default meaning you need to go remote for all requests. If you set the property to "ReadOnly" the session state object will be retrieved from the Local Cache. Then if local cache is invalidated it will go to remote store again.