Should NHibernate flush the distributed second-level cache on the ISessionFactory.

I am working on a CacheProvider for Redis . I came across something pretty curious about the pre-built NHibernate Cache Providers .

When in a distributed environment, I expect each web server to use the same NHibernate cache. Therefore, if one of the web servers is deleted, it is also expected that the cache will not be affected (since the other web server in the cluster will still use this cache!).

However, according to my tests and the sources of these providers, every time the ISessionFactory closed (calls Dispose or Close ), the entire cache region is cleared !

For example, the Memcached provider calls Clear() when Destroy() called. In Close() of ISessionFactory each ISessionFactory created has Destroy() , and then finally ICacheProvider has Stop() called. Therefore, while Memcached is designed for distributed cache, it is cleared when the ISessionFactory is deleted!

Is this the expected behavior in a distributed environment? Clearing the distributed cache each time the web server is deleted seems to cause a large number of cache misses to fail and, consequently, to lose the entire cache point!

I think Destroy() should be used to clean up any resources (e.g. Dispose() ... but it has been ported from Hibernate), and these cache providers have a serious error . For example, the Hibernate Memcached cache provdier I found is not Clear() on Destroy() .

FWIW, I posted this on the NHibernate development team.

+4
source share
1 answer

You're right. The distributed cache should NOT behave because it defeats the target. When Destroy() is called, the function called in the provider must be Dispose() . This function simply disables the cache and does not clear it, so data for other clients remains untouched in the distributed cache.

+4
source

Source: https://habr.com/ru/post/1435638/


All Articles