Search code examples
c#cachingnhibernatefluent-nhibernate

Nhibernate Disable Second Level Cache with Fluent - Disable Fake Cache also


var config =
            Fluently
                .Configure()
                .Database(MsSqlConfiguration.MsSql2008
                                            .IsolationLevel(IsolationLevel.ReadCommitted)
                                            .ConnectionString(connectionString)
                                            .DefaultSchema(defaultSchema)
                                            .FormatSql())
                                            .ExposeConfiguration
                                            (
                                                c => c.SetProperty("current_session_context_class", sessionContext)
                                            );

        if (secondLevelCacheSettings.UseSecondLevelCache)
        {
            if (secondLevelCacheSettings.CacheType == SecondLevelCacheSettings.SecondLevelCacheType.Memcached)
            {
                config.Cache(c => c.ProviderClass<MemCacheProvider>().UseQueryCache())
                    .ExposeConfiguration(c => c.SetProperty("expiration",
                                                            secondLevelCacheSettings.CacheExpirationMinutes.ToString()));
            }
            if (secondLevelCacheSettings.CacheType == SecondLevelCacheSettings.SecondLevelCacheType.HashTable)
            {
                config.Cache(c => c.ProviderClass<HashtableCacheProvider>().UseQueryCache());
            }
        }

When I don't want to use second level cache, i want to completely disable it. Seems the default configuration is using FakeCache. How do I disable FakeCache also?

Also in the logs I see,

09-04 14:14:02,088 WARN Second-level cache is enabled in a class, but no cache provider was selected. Fake cache used. - [4] NHibernate.Cache.NoCacheProvider [(null)]

Seems second level cache is enabled by default even though we did not configure it. what is the cleaner way to disable it.


Solution

  • config.Cache(x => x.Not.UseSecondLevelCache());
    

    solved my problem. It removed all logs and unnecessary cpu cycles. This is via fluent. If you are using configuration following configuration may be needed.

    <property name="cache.use_second_level_cache">
      false
    </property>