具有自动映射功能的流畅 NHibernate 缓存

发布于 2024-08-27 11:33:30 字数 581 浏览 3 评论 0原文

我试图了解如何配置 Fluent NHibernate 来为查询、实体等启用二级缓存...并同时使用自动映射。网上关于如何做到这一点的信息很少。当然,当一一映射类时可以完成......但是自动映射怎么样?

这是到目前为止我的配置代码:

AutoPersistenceModel model = AutoMap.AssemblyOf<Seminar>()
.Where(t => t.Namespace == "[MY NAMESPACE]")
.Conventions.Add(DefaultCascade.All());

Configuration config = Fluently.Configure()
.Database
(
    MsSqlConfiguration.MsSql2005
    .ConnectionString(@"[MY CONNECTION STRING]")
)
.Mappings(m => m.AutoMappings.Add(model))
.BuildConfiguration();

_sessionFactory = config.BuildSessionFactory();

谢谢!

I'm trying to understand how to configure Fluent NHibernate to enable 2nd-level caching for queries, entities, etc... And at the same time use automapping. There is very little information online on how to do that. Sure it can be done when mapping the classes one by one... But how about automapping?

Here is my configuration code so far:

AutoPersistenceModel model = AutoMap.AssemblyOf<Seminar>()
.Where(t => t.Namespace == "[MY NAMESPACE]")
.Conventions.Add(DefaultCascade.All());

Configuration config = Fluently.Configure()
.Database
(
    MsSqlConfiguration.MsSql2005
    .ConnectionString(@"[MY CONNECTION STRING]")
)
.Mappings(m => m.AutoMappings.Add(model))
.BuildConfiguration();

_sessionFactory = config.BuildSessionFactory();

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原野 2024-09-03 11:33:31

假设您已经从 NHibernate Contribution 项目下载了二级缓存提供程序,您应该能够使用以下命令在自动映射设置中初始化缓存。

Configuration config = Fluently.Configure()
.Database
(
    MsSqlConfiguration.MsSql2005
    .ConnectionString(@"[MY CONNECTION STRING]")
    .Cache(c => c.UseQueryCache().ProviderClass<YourCacheProviderOfChoice>())
)
.Mappings(m => m.AutoMappings.Add(model))
.BuildConfiguration();

选择要缓存的查询只需在 Criteria 实例上调用 SetCacheable(true) 即可。

 var query = session.CreateQuery("from Blog b where b.Author = :author")
    .SetString("author", "Gabriel")
    .SetCacheable(true);
 var list = query.List<Blog>();

这是有关 NHibernate 一级和二级缓存的史诗级博客文章,很好的参考资料。

Assuming you've already downloaded a 2nd-level cache provider from the NHibernate Contribution project, you should be able to use the following to initialize the cache within your automappings setup.

Configuration config = Fluently.Configure()
.Database
(
    MsSqlConfiguration.MsSql2005
    .ConnectionString(@"[MY CONNECTION STRING]")
    .Cache(c => c.UseQueryCache().ProviderClass<YourCacheProviderOfChoice>())
)
.Mappings(m => m.AutoMappings.Add(model))
.BuildConfiguration();

Selecting the queries you want to cache is simply a matter of calling SetCacheable(true) on your Criteria instance.

 var query = session.CreateQuery("from Blog b where b.Author = :author")
    .SetString("author", "Gabriel")
    .SetCacheable(true);
 var list = query.List<Blog>();

This is an epic blog post on NHibernate's first and second level caches, good reference material.

横笛休吹塞上声 2024-09-03 11:33:31

我已经为此苦苦挣扎了一段时间,并惊讶于那里的信息如此之少。这个问题是我能找到的最好的问题,即使在这里,接受的答案也没有说明如何启用实体缓存。这是我发现的。

要启用二级缓存:

Fluently.Configure()
    .Database(/* your DB config */)
    .Cache(c => c.UseSecondLevelCache().ProviderClass<CacheProviderClass>())

您可以同时使用此缓存和查询缓存:

Fluently.Configure()
    .Database(/* your DB config */)
    .Cache(c => c.UseSecondLevelCache()
        .UseQueryCache()
        .ProviderClass<CacheProviderClass>())

要启用每个实体缓存:

.Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Entity>()
              .Conventions.Add(ConventionBuilder.Class.Always(c => c.Cache.ReadWrite()))
          )
)

当然,您可以使用 ReadOnly()NonStrictReadWrite() 如果你希望。

I've been struggling with this for a while and was surprised how little information is out there. This question is the best I could find and even here the accepted answer doesn't say how to enable entity caching. Here's what I've found out.

To enable second level cache:

Fluently.Configure()
    .Database(/* your DB config */)
    .Cache(c => c.UseSecondLevelCache().ProviderClass<CacheProviderClass>())

You can use both this and query cache:

Fluently.Configure()
    .Database(/* your DB config */)
    .Cache(c => c.UseSecondLevelCache()
        .UseQueryCache()
        .ProviderClass<CacheProviderClass>())

To enable per-entity caching:

.Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Entity>()
              .Conventions.Add(ConventionBuilder.Class.Always(c => c.Cache.ReadWrite()))
          )
)

Of course, you can use ReadOnly() or NonStrictReadWrite() if you wish.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文