从单个 Web 服务器迁移到多个 Web 服务器负载平衡环境时的 Nhibernate 二级缓存问题
我之前的设置是一个 Web 服务器和一个数据库服务器。我使用 nhibernate 二级缓存来缓存内容,以避免大量调用数据库。这非常有效,因为我正在使用这个程序集
nhibernate.caches.syscache
并添加了此代码来打开二级缓存(使用常规的 syscache 提供程序):
return configuration
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<ApplicationMap>().Conventions.Add(typeof(Conventions)))
.ExposeConfiguration(
c => {
c.SetProperty("proxyfactory.factory_class", proxyFactory);
c.SetProperty("cache.provider_class", "NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache");
c.SetProperty("cache.use_second_level_cache", "true");
c.SetProperty("cache.use_query_cache", "true");
c.SetProperty("expiration", "86400");
})
.BuildSessionFactory();
我现在已经迁移到一个具有多个网络服务器的新环境,我正在尝试了解这的影响(我仍然有一个数据库服务器)。
由于缓存之前存储在网络服务器上,现在看来我在每个网络服务器上都有 2 个并行缓存,它们本身可能不同步,并可能导致更新过时等。
获得好处的最佳解决方案是什么我之前拥有的缓存,但也利用了这个新设置提供的 Web 服务器负载平衡的弹性?
My previous setup was a single web server and a single database server. I was using nhibernate 2nd level caching to cache stuff to avoid lots of calls going to the database. This has worked great as i was using this this assembly
nhibernate.caches.syscache
and added this code to turn on the second level caching (using the regular syscache provider):
return configuration
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<ApplicationMap>().Conventions.Add(typeof(Conventions)))
.ExposeConfiguration(
c => {
c.SetProperty("proxyfactory.factory_class", proxyFactory);
c.SetProperty("cache.provider_class", "NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache");
c.SetProperty("cache.use_second_level_cache", "true");
c.SetProperty("cache.use_query_cache", "true");
c.SetProperty("expiration", "86400");
})
.BuildSessionFactory();
I have now migrated over to a new environment that has multiple webservers and i am trying to understand the implications of this (I still have a single db server).
Since the cache was being stored on the webserver before, now it would seem like i have 2 parallel caches on each webserver which themselves may not be in sync and may cause out of date updates, etc.
What is the best solution to get the benefits of the caching i had before but also take advantage of the resiliency of the web server load balancing that is provided with this new setup?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Ayende 发表博客了解 NHibernate 中二级缓存的使用情况。您将需要在网络场场景中使用分布式缓存。例如 SysCache2(它依赖于 ASP.NET 缓存,可以配置为使用分布式提供程序)或 MemCache。这是一篇文章,说明如何配置 memcached。
Ayende blogged about 2nd level cache usage in NHibernate. You will need to use a distributed cache in a web farm scenario. For example SysCache2 (which relies on ASP.NET cache and which could be configured to use a distributed provider) or MemCache. Here's an article illustrating how you could configure memcached.
嗯,我已经使用了 memcached,它的工作方式就像一个魅力。您可能需要调整缓存的大小,但除此之外,它是没有麻烦的。
在每个 Web 服务器节点上设置一个 memached 节点,并在添加节点时维护该配置
Well i've used memcached and it works like a charm. You may have to tweak the size of the cache but other than that it's hassle-free.
Setup a memached node on each webserver node and maintain that configuration as you add up nodes