问题描述
我正在使用 Redis 为 NHibernate 设置缓存。我已经按照有关如何设置的文档进行了操作。唯一的区别是我没有使用下面的实现。
<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>
相反,我使用的是这样的:
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());
<configSections>
<section name="redis" type="NHibernate.Caches.StackExchangeRedis.RedisSectionHandler,NHibernate.Caches.StackExchangeRedis" />
</configSections>
<redis>
<cache region="foo_bar" expiration="999" priority="4" />
</redis>
RedisCacheProvider.ConnectionSettings = new RedisCacheConnection("localhost",6379) { { "allowAdmin","true" },{ "abortConnect","false" } };
我不知道把上面的片段放在哪里。即使我知道把它放在哪里,我也希望它被设置在 web.config 中,但我找不到任何让我朝那个方向发展的东西。
请问谁能指导我正确的方向?我也尝试过依赖注入,但仍然遇到 StackExchange.Redis configuration string was not provided
的问题。
解决方法
对于可能遇到此问题的任何人,我都能弄清楚这一点。使用NHibernate正确设置Redis需要的是添加如下配置。
.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");
})
所述
最终结果将如下所示。
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",abortConnect=false");
})
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<AccountMap>()
.Conventions.AddFromAssemblyOf<UnderscoreColumnConvention>()
);