简体   繁体   English

NHibernate应该在ISessionFactory.Dispose上清除分布式二级缓存吗?

[英]Should NHibernate clear distributed 2nd Level Cache on ISessionFactory.Dispose?

I'm working on a CacheProvider for Redis . 我正在为Redis开发CacheProvider I've stumbled upon something quite curious about the pre-built NHibernate Cache Providers . 我偶然发现了一些对预构建的NHibernate缓存提供程序非常好奇的东西。

When in a distributed environment, I'd expect each web server to share the same NHibernate cache. 在分布式环境中,我希望每个Web服务器共享相同的NHibernate缓存。 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!). 因此,如果删除了其中一个Web服务器,则还应该预期缓存不会受到影响(因为群集中的另一个 Web服务器仍将使用该缓存!)。

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 ! 但是,根据我的测试和这些提供程序的来源,每次关闭ISessionFactory (调用DisposeClose )时, 整个缓存区域都会被清除

For example, the Memcached Provider calls Clear() when Destroy() is called. 例如, Memcached Provider在调用Destroy()时调用Clear() In the Close() of ISessionFactory , each created ICache has Destroy() called and then finally the ICacheProvider has Stop() called. ISessionFactoryClose() ,每个创建的ICache都调用了Destroy() ,最后ICacheProviderStop() Therefore, even though Memcached is meant to be a distributed cache, it gets cleared when the ISessionFactory is disposed of! 因此,即使Memcached是一个分布式缓存,它也会在ISessionFactory被丢弃时被清除!

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! 每次关闭Web服务器时清除分布式缓存似乎会产生大量缓存未命中,从而失去整个缓存点!

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 . 我在想的是Destroy()应该用于清理任何资源 (比如Dispose() ......但它是从Hibernate带来的)并且这些缓存提供程序有一个严重的错误 For example, a Hibernate Memcached cache provdier I found does not clear() on destroy() . 例如, 我发现Hibernate Memcached缓存提供 者不会destroy()clear() destroy()

FWIW, I've posted this on the NHibernate Development Group. FWIW,我已经在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() . 调用Destroy() ,提供程序中调用的函数应为Dispose() This function merely disconnects the cache and not clears it so the data for other clients remains intact in the distributed cache. 此功能仅断开缓存而不清除缓存,因此其他客户端的数据在分布式缓存中保持不变。

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

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