Nhibernate 中的加载与获取

发布于 2024-08-28 04:22:55 字数 476 浏览 3 评论 0原文

我的 Web 应用程序中的母版页执行身份验证并使用 Get 加载用户实体。

此后,每当用户控件或任何其他类需要用户对象时,我都会执行Load

通常 nhibernate 应该从缓存中加载对象,或者在调用 Load 时返回持久加载的对象。但这不是我的网络应用程序显示的行为。 NHprof 每当调用 Load 时总是显示 sql。如何验证 Load 的行为是否正确?

我使用S#arp架构框架。

The master page in my web application does authentication and loads up the user entity using a Get.

After this whenever the user object is needed by the usercontrols or any other class I do a Load.

Normally nhibernate is supposed to load the object from cache or return the persistent loaded object whenever Load of called. But this is not the behavior shown by my web application. NHprof always shows the sql whenever Load is called. How do I verify the correct behavior of Load?

I use the S#arp architecture framework.

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

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

发布评论

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

评论(2

少女的英雄梦 2024-09-04 04:22:55

实际上,在未标记为惰性的实体上调用 Load 会导致立即加载。这是因为非惰性实体永远不会被代理。在这种情况下,它的行为方式与 Get 相同。

Actually, calling Load on an entity not marked as lazy causes immediate loading. That is because non-lazy entities are never proxied. In this case, it just acts the same way as Get.

简单爱 2024-09-04 04:22:55

如果您使用 Get,则会对数据库进行命中。
如果您使用“加载”,则不会对数据库进行任何命中,但会使用“延迟加载”创建对象(在您的情况下为“用户”)。因此,当您检查属性时,它知道您需要数据,因此它会通过查询访问数据库以获取数据。

如果你想从缓存中获取一个对象,你需要考虑2个选项。
第一级缓存是在一个会话中使用的缓存。因此,当您关闭会话或在不同的会话中加载相同的对象时,您会获得额外的命中。
二级缓存适用于所有会话。如果一个会话获取该对象,则另一个会话从缓存中获取该对象。

所以你想要的可能是二级缓存。

If you use Get, then a hit to a database is made.
If you use Load, no hit to a database is made, but the object (User in your case) is created with 'lazy loading'. So when you check a property it knows that you want data so it hits the database with a query to get the data.

If you want to get an object from cache, you need to consider 2 options.
First level cache, is a cache that is in use in ONE session. So when you close a session or load the same object in a different session, you get additional hits.
Second level cache works accross all sessions. If one session gets the object, the other session gets it from cache.

So what you want is probably a second level cache.

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