微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决NHibernate CacheException:未提供 StackExchange.Redis 配置字符串

我正在使用 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());

我还在 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",abortConnect=false");
    })
    .Mappings(m =>
        m.FluentMappings.AddFromAssemblyOf<AccountMap>()
            .Conventions.AddFromAssemblyOf<UnderscoreColumnConvention>()
    );

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。