NHibernate CacheException: StackExchange.Redis 未提供配置字符串

NHibernate CacheException: StackExchange.Redis configuration string was not provided

我正在为 NHibernate 使用 Redis 设置缓存。我已经按照有关如何设置它的文档进行操作。唯一的区别是我没有使用下面的实现。

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

我还在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>

我唯一没有添加的是下面的内容。

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

27.1. How to use a cache?

所述

最终结果将如下所示。

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