如何在 Fluent NHibernate 上设置 SysCache?

发布于 2024-09-27 12:40:20 字数 1051 浏览 3 评论 0原文

使用这个,我可以告诉 Fluent NHibernate 使用 SysCache 作为二级缓存提供者:

MsSqlConfiguration.MsSql2008.ShowSql().ConnectionString(x =>
            {
                x.Server(@"localhost\ANDREPC");
                x.Database("mydb");
                x.TrustedConnection();
            }).Cache(c => c.ProviderClass<SysCacheProvider>().UseQueryCache())

此外,SysCache 的配置必须放在 Web.Config 上:

<configuration>
 <configSections>
  <section name="syscache" type="NHibernate.Caches.SysCache.SysCacheSectionHandler,NHibernate.Caches.SysCache" />
 </configSections>

 <syscache>
  <cache region="foo" expiration="500" priority="4" />
  <cache region="bar" expiration="300" priority="3" />
 </syscache>
</configuration>

现在怎么办?这些区域意味着什么?如何将区域与类型关联起来?我该如何让它发挥作用?我的 jMeter 测试表明,经过上述配置后,我的应用程序比以前慢了 7%。我需要了解 SysCache 并学习如何继续配置。

谢谢。

PS:官方SysCache文档在这里,它不是解释性的

Using this, I can tell Fluent NHibernate to use SysCache as a 2nd Level Cache Provider:

MsSqlConfiguration.MsSql2008.ShowSql().ConnectionString(x =>
            {
                x.Server(@"localhost\ANDREPC");
                x.Database("mydb");
                x.TrustedConnection();
            }).Cache(c => c.ProviderClass<SysCacheProvider>().UseQueryCache())

Besides, SysCache's configurations must be placed on Web.Config:

<configuration>
 <configSections>
  <section name="syscache" type="NHibernate.Caches.SysCache.SysCacheSectionHandler,NHibernate.Caches.SysCache" />
 </configSections>

 <syscache>
  <cache region="foo" expiration="500" priority="4" />
  <cache region="bar" expiration="300" priority="3" />
 </syscache>
</configuration>

Now what? What does these regions mean? How do I associate a region with a type? How do I make this to work? My jMeter tests sugest that after the configuration above my application got 7% slower than before.. I need to understand SysCache and learn how to continue with the configuration.

Thanks.

PS: The official SysCache documentation is here and it's not explanatory

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

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

发布评论

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

评论(1

踏雪无痕 2024-10-04 12:40:20

根据您正在执行的操作,默认情况下我认为 L2 缓存仅缓存 ID、EG session.Get 或 session.Load 调用的项目。要使用 ICriteria 等缓存查询,您需要明确说明您希望缓存该查询。 EG

ICriteria criteria = Session.CreateCriteria( ).SetCacheable( true ).SetCacheRegion( "SomeNameHere" );

这里的一些名称是您的缓存区域。简而言之,这将缓存项组合在一起,为了保持简洁,我通常只输入类/实体的名称,例如“人”或“公司”。

设置类映射时,您可能总是想使用基类中的 Cache 属性。就像

Cache.ReadWrite( ).IncludeAll( ) ;

我个人发现的那样,如果没有这个,当执行查询时,它会缓存结果集中每个项目的 ID,而不是项目本身,因此这将使繁重的查询变得更快,但随后它必须为每个项目访问数据库item,因此如果您有一个非常简单的查询返回 100 个项目,那么您的数据库可能会被命中 100 次。我发现将上述内容添加到我的映射类中可以解决该问题。

希望这有帮助。

Depending on what you are doing, by default I think the L2 cache is only caching items called for by ID, E.G. session.Get or session.Load. To cache queries using ICriteria etc you need to specifically say you want that query to be cached. E.G.

ICriteria criteria = Session.CreateCriteria( ).SetCacheable( true ).SetCacheRegion( "SomeNameHere" );

The some name here is your cache region. In short this groups together cache items, Keeping this really breif I usually just put the name of the class/entity such as "Person" or "Company".

When setting up your class maps you might always want to play with the Cache property from the base class. Its something like

Cache.ReadWrite( ).IncludeAll( ) ;

I personally found that without this, when a query was executed it cached the ID's of each item in the resultset but not the items themselves, so this would make a heavy query fast, but then it has to hit the database for each item, so if you have a really simple query returning 100 items, your database could then get hit 100 times. I found that adding the above to my mapping class solved that problem.

Hopefully that helps.

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