简体   繁体   English

NHibernate和潜在的缓存问题

[英]NHibernate and potential caching problems

Ok so I have an n tiered model. 好的,所以我有一个n层模型。 (WPF, Asp.Net, etc) to backend services via WCF. (WPF,Asp.Net等)通过WCF进行后端服务。 These services use NHibernate to communicate with the database. 这些服务使用NHibernate与数据库进行通信。 Some of these services will be run in InstanceContextMode.Single mode. 其中一些服务将在InstanceContextMode.Single模式下运行。

Questions: 问题:

  1. In singleton service instances should I try to utilize 1 session object for the entire time the wcf service is alive to get the most out of my cache? 在单例服务实例中,我应该在wcf服务处于活动状态的整个过程中尝试利用1个会话对象来最大程度地利用我的缓存吗?
  2. If I use 1 session instance in this singleton instance and never create new ones I assume I have to worry about eventually removing cached entities from the session or dumping it all together to avoid performance issues with the session? 如果我在此单例实例中使用1个会话实例,并且从不创建新实例,那么我认为我不得不担心最终从会话中删除缓存的实体或将其全部转储以避免会话性能问题?
  3. Is it a good idea at all to use the session in this way for a singleton wcf service? 以这种方式将会话用于单例wcf服务是个好主意吗? It seems like it would be if I want to utilize caching. 如果我想利用缓存,就好像是这样。
  4. Should I utilize 2nd level cache in a scenario like this? 我应该在这种情况下利用二级缓存吗?
  5. Outside of this scenario when should I avoid caching? 在这种情况之外,什么时候应该避免缓存? I would assume that I would want to avoid it in any sort of batching scenario where a large number of objects are created/updated and never really used again outside of the creation or updates. 我假设我想在任何创建/更新大量对象并且在创建或更新之外再也没有真正使用过的对象的批处理场景中避免使用它。
  6. Are items automatically cached in session when I create/read/update/delete or do I need to specify something in the mapping files or configuration? 创建/读取/更新/删除项目时,项目是否会自动缓存在会话中,还是需要在映射文件或配置中指定某些内容?

1-3: As far as I know, ISession objects are supposed to be light-weight, short-lived objects, which live only for the duration for which they're needed. 1-3:据我所知,ISession对象应该是重量轻,寿命短的对象,它们仅在需要它们的持续时间内存在。 I would advise AGAINST using the same ISession object for the whole lifetime of your service. 我建议AGAINST在您的服务的整个生命周期中都使用相同的ISession对象。
What I would suggest instead is using the same ISeessionFactory instance, and creating new ISessions from it as necessary (you can try something similar to Session-Per-Request pattern). 我建议使用的是相同的ISeessionFactory实例,并根据需要从中创建新的ISession(您可以尝试使用类似于Session-Per-Request模式的方法)。
If you enable 2nd level cache, you can have all the benefits of caching in this scenario. 如果启用第二级缓存,则在这种情况下可以享受缓存的所有好处。

5 Yep, pretty much. 5是的,差不多。 Also remember that 2nd level cache instance is per ISessionFactory instance . 还要记住,第二级缓存实例是每个ISessionFactory实例 that means that if you're using more than 1 ISessionFactory instance you'll have a lot of problems with your cache. 这意味着,如果您使用多个ISessionFactory实例,则您的缓存会遇到很多问题。

6 for 1st level cache you don't need to define anything. 对于1级缓存,您不需要定义6。
for 2nd level cache you need to enable the cache when you configure nHibernate (fluently, in my case): 对于二级缓存,您需要在配置nHibernate时启用缓存(以我为例):

.Cache(c => c.UseQueryCache()
                                    .ProviderClass(
                                    isWeb ? typeof(NHibernate.Caches.SysCache2.SysCacheProvider).AssemblyQualifiedName //in web environment- use sysCache2
                                        : typeof(NHibernate.Cache.HashtableCacheProvider).AssemblyQualifiedName //in dev environmet- use stupid cache
                                    )) 
                          )

and specify for each entity and each collection that you want to enable cache for them: 并为您要为其启用缓存的每个实体和每个集合指定:

mapping.Cache.ReadWrite().Region("myRegion");

and for a collection: 并进行收集:

mapping.HasMany(x => x.Something)
.Cache.ReadWrite().Region("myRegion");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM