NHibernate 缓存唯一索引?
我有一些类,它们具有该对象唯一的属性,例如,对于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,你是对的,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.
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.