I'm working on a CacheProvider
for Redis. I've stumbled upon something quite curious about the pre-built NHibernate Cache Providers.
When in a distributed environment, I'd expect each web server to share the same NHibernate cache. Therefore, if one of the web servers is removed, it would also be expected that the cache isn't affected (because another web server in the cluster would still be using that cache!).
However, according to my tests and the source of these providers, each time an ISessionFactory
is closed (calling Dispose
or Close
), the whole cache region is cleared!
For example, the Memcached Provider calls Clear()
when Destroy()
is called. In the Close()
of ISessionFactory
, each created ICache
has Destroy()
called and then finally the ICacheProvider
has Stop()
called. Therefore, even though Memcached is meant to be a distributed cache, it gets cleared when the ISessionFactory
is disposed of!
Is this really the expected behavior in a distributed environment? Clearing the distributed cache each time a web server is taken down seems like it would generate a lot of cache misses and therefore losing the whole point of caching!
What I'm thinking is that Destroy()
should be used to clean up any resources (like a Dispose()
... but it was brought over from Hibernate) and these cache providers have a serious bug. For example, a Hibernate Memcached cache provdier I found does not clear()
on destroy()
.
FWIW, I've posted this on the NHibernate Development Group.
You are right. A distributed cache should NOT behave like this because it defeats the purpose. When Destroy()
is called, the function called in the provider should be Dispose()
. This function merely disconnects the cache and not clears it so the data for other clients remains intact in the distributed cache.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With