NHibernate caching issue - When to call Evict?

纵然是瞬间 提交于 2019-12-07 12:09:01

问题


I've run into an apparent caching issue where what's being returned by NHibernate is not matching up with what's in the DB.

I believe this is level2 cache data. It looks like I can use the Evict to do this, but when should the Evict method actually be called? For my particular application, the data is going to be unique to the user and chances are the data provided will only be used once*.

Can I disable Level2 caching for these sets of objects completely?

UPDATE 10/31

My scenario is this: I have a shopping cart where the customer is going to be adding and removing items. What I am doing is the following: before the updates to the cart are processed, I evict the Cart and CartProduct entities. Once this is done, I retrieve the list of CartProducts from the provider and return the view (this is happening in a .NET MVC Controller).

UPDATE 11/3

The shopping cart has since been finished, and I ran into an issue that appeared to be related to the same NHibernate issue, but in fact was an MVC issue. Deeper digging revealed that the HTML Helper extensions were overriding the value I supposed and replacing with what was in the Model state. So a double whammy on this one. Hope this helps someone.


回答1:


No, you cannot disable the cache for certain entities.

You do have several options:

  1. Use ISession.Clear() to clear all entities from NHibernate's cache.
    • http://nhibernate.info/doc/nh/en/index.html#performance-sessioncache
  2. Use ISession.Evict( obj ) to clear a specific entity from the cache.
    • http://nhibernate.info/doc/nh/en/index.html#performance-sessioncache
  3. Use ISessionFactory.Evict( typeof( obj ) ) to evict all entities/objects of a particular type from the cache. This would be the closest to what you are wanting in my opinion.
    • http://nhibernate.info/doc/nh/en/index.html#performance-sessioncache
  4. Use the IStatelessSession to fetch the objects/entities from the database as this completely bypasses all caches.
    • http://nhibernate.info/doc/nh/en/index.html#batch-statelesssession


来源:https://stackoverflow.com/questions/13131796/nhibernate-caching-issue-when-to-call-evict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!