问题描述
将Singleton对象重构为集群环境的最佳策略是什么?
What is the best strategy to refactor a Singleton object to a cluster environment?
我们使用Singleton从数据库中缓存一些自定义信息.它的主要是只读,但是在发生某些特定事件时会刷新.
We use Singleton to cache some custom information from Database. Its mostly read-only but gets refreshed when some particular event occurs.
现在,我们的应用程序需要部署在集群环境中.根据定义,每个JVM将具有其自己的Singleton实例.因此,当在单个节点上发生刷新事件并刷新其缓存时,JVM之间的缓存可能不同步.
Now our application needs to be deployed in a Clustered environment. By definition, each JVM will have its own Singleton instance. So the cache may be out-of-sync between the JVM's when a refresh event occurs on a single node and its cache is refreshed.
保持缓存同步的最佳方法是什么?
What is the best way to keep the cache's in sync?
谢谢.
缓存主要用于向UI提供自动完成列表(出于性能原因),我们使用Websphere.因此欢迎任何与Websphere相关的技巧.
The cache is mainly used to provide an autocomplete list (performance reasons) to UI and we use Websphere. So any Websphere related tips welcome.
推荐答案
最简单的方法是:
-
在单例缓存中添加一个到期计时器,以便每隔一段时间就会清除一次缓存,随后的调用会从源(例如数据库)中获取更新的数据
Add an expiry timer to your singleton cache so that every so often the cache gets purged and subsquent calls fetch the updated data from source (e.g. a database)
使用诸如JMS topic/tibRV之类的东西实现缓存的通知机制.获取每个缓存实例以订阅并响应此主题上广播的任何更改消息.
Implement a notification mechanism for the cache using something like a JMS topic/tibRV. Get each cache instance to subscribe and react to any change messages broadcast on this topic.
这篇关于集群环境中的单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!