I'm setting up caching using Redis for NHibernate. I have followed the documentation on how to set up this. The only difference is I did not use the below implementation.
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_cache">true</property>
<property name="cache.provider_class">NHibernate.Caches.Redis.RedisCacheProvider,
NHibernate.Caches.Redis</property>
Instead, I use something like this:
return Fluently.Configure()
.Database(
PostgreSQLConfiguration.PostgreSQL82.ConnectionString(
c => c.FromConnectionStringWithKey(connectionStringName))
.Driver<NpgsqlDriverExtended>()
.Dialect<NpgsqlDialectExtended>()
)
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<AccountMap>()
.Conventions.AddFromAssemblyOf<UnderscoreColumnConvention>()
)
.Cache(c => c.ProviderClass<RedisCacheProvider>()
.UseQueryCache()
.UseSecondLevelCache());
I also added the below in web.config
<configSections>
<section name="redis" type="NHibernate.Caches.StackExchangeRedis.RedisSectionHandler, NHibernate.Caches.StackExchangeRedis" />
</configSections>
<redis>
<cache region="foo_bar" expiration="999" priority="4" />
</redis>
The only thing that I did not add is the below.
RedisCacheProvider.ConnectionSettings = new RedisCacheConnection("localhost", 6379) { { "allowAdmin", "true" }, { "abortConnect", "false" } };
I do not know where to place the above snippet. Even if I know where to place it, I'd rather want it to be set up in web.config, but I could not find anything that put me in that direction.
Please can anyone direct me in the right direction? I have tried also dependency injection but still having an issue that StackExchange.Redis configuration string was not provided
.
For anyone who might have this issue, I was able to figure this out. What you need to set Redis with NHibernate properly is to add the following configuration.
.ExposeConfiguration(cfg =>
{
cfg.Properties.Add("cache.default_expiration", "900");
cfg.Properties.Add("cache.use_sliding_expiration", "true");
cfg.Properties.Add("cache.configuration", "localhost:6379,allowAdmin=true,abortConnect=false");
})
As stated at 27.1. How to use a cache?
The final result will be something like the below.
return Fluently.Configure()
.Database(
PostgreSQLConfiguration.PostgreSQL82.ConnectionString(
c => c.FromConnectionStringWithKey(connectionStringName))
.Driver<NpgsqlDriverExtended>()
.Dialect<NpgsqlDialectExtended>()
)
.Cache(c =>
c.UseSecondLevelCache()
.UseQueryCache()
.ProviderClass<RedisCacheProvider>())
.ExposeConfiguration(cfg =>
{
cfg.Properties.Add("cache.default_expiration", "900");
cfg.Properties.Add("cache.use_sliding_expiration", "true");
cfg.Properties.Add("cache.configuration", "localhost:6379,allowAdmin=true,abortConnect=false");
})
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<AccountMap>()
.Conventions.AddFromAssemblyOf<UnderscoreColumnConvention>()
);