【发布时间】:2017-01-05 15:18:45
【问题描述】:
我想让我的 hazelcast 地图在 5 分钟后清空,以便我可以将更新的 Db 记录放入其中。但它没有变空并且仍然包含旧记录。下面是我在 hazelcast 缓存服务器中的 hazlecast.xml 文件。
<map name="CdnConfig">
<in-memory-format>BINARY</in-memory-format>
<backup-count>0</backup-count>
<async-backup-count>0</async-backup-count>
<time-to-live-seconds>300</time-to-live-seconds>
<max-idle-seconds>0</max-idle-seconds>
<eviction-policy>LRU</eviction-policy>
<max-size policy="PER_NODE">1000</max-size>
<eviction-percentage>25</eviction-percentage>
<min-eviction-check-millis>5000</min-eviction-check-millis>
</map>
我的 Java 代码是这样的:
List<CdnConfigDto> dataList=(List<CdnConfigDto>)hazelCastCache.getDataFromCache("CdnConfig", key);
if (dataList != null) {
LOGGER.info("HazelcastCache conatains records ");
} else {
LOGGER.info("HazelcastCache does not contains records ");
dataList = configurationService.getDataFromDB();
hazelCastCache.addDataToCache("CdnConfig", key, dataList);
}
榛树的声明:
private static HazelcastInstance hazelcastInstance;
private static HazelCastCache instance = null;
public static HazelCastCache getInstance() {
return instance;
}
public static void initCache() {
if (instance == null) {
synchronized (HazelCastCache.class) {
if (instance == null) {
instance = new HazelCastCache();
}
}
}
}
private HazelCastCache() {
hazelcastInstance = HazelcastCacheInstance.getHazelcastInstance();
}
getDataFromCache()方法实现:--
public Object getDataFromCache(String mapName, Object key) {
Object data = null;
IMap<Object, Object> hazelMap = hazelcastInstance.getMap(mapName);
if (null != hazelMap) {
data = hazelMap.get(key);
}
return data;
}
【问题讨论】:
-
hazelcast.xml或hazlecast.xml? -
能把
hazelCastCache的声明和getDataFromCache&addDataToCache的方法实现贴出来吗? -
嗨!新年快乐..实际上我采取了另一条路线,而不是依赖驱逐策略,只要有任何 CRUD 操作,我都会进行缓存重置。但仍然想知道为什么上述方法不起作用。 @Desai:我已经在上面发布了代码..@noctarius:它的 hazelcast.xml
标签: java caching hazelcast hazelcast-imap