NHibernate 缓存唯一索引?

发布于 2024-07-22 06:34:57 字数 230 浏览 6 评论 0原文

我有一些类,它们具有该对象唯一的属性,例如,对于 User 类,属性 Username 永远不应该重复。

NHibernate 似乎不支持通过唯一键加载对象,然后以与 Load() 或 Get() 相同的方式缓存该对象。 我这样说对吗?

如果是的话,我只需要通过可能的扩展方法来扮演我自己的角色,就像 LoadByUniqueIndex(lambda property, object key) 那样。

I have a few classes which have a property which is unique to that object, for example for the User class the property Username should never be duplicated.

NHibernate doesn't seem to support loading of an object by unique key which is then cached in the same way that Load() or Get() does. Am I correct when I say that?

If I am then I will just have to role my own via possibly an extension method along the lines of LoadByUniqueIndex(lambda property, object key).

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

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

发布评论

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

评论(1

最后的乘客 2024-07-29 06:34:57

是的,你是对的,NH 不会直接从缓存中获取除 id 之外的任何实体。 请注意,除了 id 之外的所有内容都可能会更改,并且需要在数据库中查找。

缓存时要小心。 “过早的缓存是万恶之源”之类的。 说真的,如果您不确定是否会遇到明显的性能问题,请不要编写自己的缓存。

  • 编写将实体作为参数传递的代码,以避免多次加载同一实体。
  • 避免缓存的寿命比会话长。 如果您不通知所有更改,您将拥有过时的数据。
  • 避免静态缓存,而使用线程静态。 这避免了会话之间共享数据,这会破坏事务隔离。
  • 如果确实需要,请使用二级缓存。

如果您考虑拥有一个与应用程序运行时间一样长的静态缓存,我可以告诉您最好完全避免它。 只要付出合理的努力,它就无法正常工作。

Yes, you are right, NH doesn't get entities directly from the cache for anything except the id. Note that everything except the id could potentially change and needs to be looked up in the database.

Be careful when caching. "Premature caching is the root of all evil" or whatever. Seriously, if you are not sure that you get notable performance problems, don't write your own cache.

  • Write code that passes entities as arguments to avoid that the same entity is loaded several times.
  • Avoid caches that live longer than a session. You will have stale data if you don't notify all the changes.
  • avoid caches that are static, use thread static instead. This avoids sharing data between sessions, which would break the transaction isolation.
  • Use second level caches if you really need it.

If you consider to have a static cache that lives as long as the application runs, I can tell you that you better avoid it at all. It will not properly work with a reasonable amount of effort.

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