Reputation: 13055
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.
Upvotes: 2
Views: 4110
Reputation: 13055
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>
Upvotes: 3
Reputation: 123901
I would say it this way: Do not worry about FakeCache
and NoCacheProvider
. It is just an implementation of the:
Description
Instead of using a null reference to convey absence of an object (for instance, a non-existent customer), one uses an object which implements the expected interface, BUT whose method body is empty. The advantage of this approach over a working default implementation is that a Null Object is very predictable and has no side effects: it does nothing.
These are few lines of FakeCache:
public class FakeCache : ICache
{
public FakeCache(string regionName)
{
RegionName = regionName;
}
public object Get(object key)
{
return null;
}
public void Put(object key, object value)
{
}
public void Remove(object key)
{
}
...
So, this is only NHibernate built-in way how to handle situations, when query or second level caches are turned on, but no Provider was properly configured. As we can see, the calls to that are just fake calls... doing nothing...
Summary: do not care about NoCacheProvider
or FakeCache
.
NOTE: On the other hand, properly configure Cache provider not only for Query cache, but you should also turn on the Second Level Cache. These are in fact tightly coupled...
config.Cache(c => c
.ProviderClass....
.UseQueryCache()
.UseSecondLevelCache() // Second Leve Cache keeps items by ID
EXTEND:
In case we do not want/like to see any internal features, e.g: NoCacheProvider.cs
namespace NHibernate.Cache
{
/// <summary>
/// A cache provider placeholder used when caching is disabled.
/// </summary>
public class NoCacheProvider : ICacheProvider
{
private static readonly IInternalLogger log = LoggerProvider
.LoggerFor(typeof(NoCacheProvider));
public const string WarnMessage = "Second-level cache is enabled in a class,"+
" but no cache provider was selected. Fake cache used.";
....
Just inject our own/custom implementation of the ICacheProvider
- without any logging etc. That is the biggest power of NHibernate - it is really very extensible and adjustable.
Upvotes: 1