NHibernate 在二级缓存中找不到命名查询结果集

发布于 2024-08-31 10:33:07 字数 821 浏览 0 评论 0原文

我有一个简单的单元测试,我使用相同的参数执行相同的 NHibernate 命名查询 2 次(每次不同的会话)。这是一个简单的 int 参数,由于我的查询是一个命名查询,我假设这两个调用是相同的,并且结果应该被缓存。

事实上,我可以在日志中看到结果正在被缓存,但使用不同的键。因此,我的第二次查询结果在缓存中从未找到。

这是我的日志中的一个片段(注意键有何不同):(

第一个查询)

调试 NHibernate.Caches.SysCache2.SysCacheRegion [(null)] <(null)> - 添加新数据:key= [snipped]...参数:['809'];命名的 参数:{}@743460424 & value=System.Collections.Generic.List`1[System.Object]

(第二个查询)

调试 NHibernate.Caches.SysCache2.SysCacheRegion [(null)] <(null)> - 添加新数据:key=[snipped]...参数:['809'];命名的 参数:{}@704749285 & value=System.Collections.Generic.List`1[System.Object]

我已将 NHibernate 设置为使用查询缓存。我将这些查询设置为可缓存=true。不知道还能去哪里看。有人有什么建议吗?

谢谢
-麦克风

I have a simple unit test where I execute the same NHibernate named query 2 times (different session each time) with the identical parameter. It's a simple int parameter, and since my query is a named query I assume these 2 calls are identical and the results should be cached.

In fact, I can see in my log that the results ARE being cached, but with different keys. So, my 2nd query results are never found in cache.

here's a snip from my log (note how the keys are different):

(first query)

DEBUG NHibernate.Caches.SysCache2.SysCacheRegion [(null)] <(null)> -
adding new data: key= [snipped]... parameters: ['809']; named
parameters: {}@743460424 &
value=System.Collections.Generic.List`1[System.Object]

(second query)

DEBUG NHibernate.Caches.SysCache2.SysCacheRegion [(null)] <(null)> -
adding new data: key=[snipped]... parameters: ['809']; named
parameters: {}@704749285 &
value=System.Collections.Generic.List`1[System.Object]

I have NHibernate set up to use the query cache. And I have these queries set to cacheable=true. Don't know where else to look. Anyone have any suggestions?

Thanks
-Mike

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

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

发布评论

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

评论(1

如梦初醒的夏天 2024-09-07 10:33:07

好吧 - 我想通了。我正在使用以下语法执行我的命名查询:(

IQuery q = session.GetNamedQuery("MyQuery")
                .SetResultTransformer(Transformers.AliasToBean(typeof(MyDTO)))
                .SetCacheable(true)
                .SetCacheRegion("MyCacheRegion");

我可能会补充说,NHibernate文档如何告诉你如何做到这一点。但我离题了;))

如果你使用创建一个为每个查询添加新的 AliasToBean 转换器,那么每个查询对象(这是缓存的关键)将是唯一的,并且您永远不会获得缓存命中。所以,简而言之,如果你按照 nhib 文档所说的那样做,那么缓存将不起作用。

相反,在静态成员变量中创建一次变压器,然后将其用于查询,并且缓存将起作用 - 像这样:

private static IResultTransformer myTransformer = Transformers.AliasToBean(typeof(MyDTO))

...

IQuery q = session.GetNamedQuery("MyQuery")
                    .SetResultTransformer(myTransformer)
                    .SetCacheable(true)
                    .SetCacheRegion("MyCacheRegion");

Okay - i figured this out. I was executing my named query using the following syntax:

IQuery q = session.GetNamedQuery("MyQuery")
                .SetResultTransformer(Transformers.AliasToBean(typeof(MyDTO)))
                .SetCacheable(true)
                .SetCacheRegion("MyCacheRegion");

( which, I might add, is EXACTLY how the NHibernate docs tell you how to do it.. but I digress ;) )

If you use create a new AliasToBean Transformer for every query, then each query object (which is the key to the cache) will be unique and you will never get a cache hit. So, in short, if you do it like the nhib docs say then caching wont work.

Instead, create your transformer one time in a static member var and then use that for your query, and caching will work - like this:

private static IResultTransformer myTransformer = Transformers.AliasToBean(typeof(MyDTO))

...

IQuery q = session.GetNamedQuery("MyQuery")
                    .SetResultTransformer(myTransformer)
                    .SetCacheable(true)
                    .SetCacheRegion("MyCacheRegion");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文