Fail to display updated cache value on different pages
trytrysin Feb 28, 2019 9:21 PMDear All,
In brief I faced a problem on two different pages using same cache value, but display different value.
Please takes some time to advise what the problem is. Thanks.
I am working on a project that management expected multiple applications (war file) can share usage of a single cache (as all the war will sit on the same container, and only single node is provided).
i.e. I plan to prepare a Utility jar to create a local mode infinispan cache for different wars to use.
a singleton startup bean is built, try to ensure all applications using same cache source
package cmm.bean.impl; import javax.annotation.PreDestroy; import javax.ejb.Singleton; import javax.ejb.Startup; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.infinispan.Cache; import org.infinispan.configuration.cache.ConfigurationBuilder; import org.infinispan.manager.DefaultCacheManager; @Singleton @Startup public class testCacheManagerProvider { private static final Logger logger = LogManager.getLogger(testCacheManagerProvider.class); private DefaultCacheManager manager; private Cache testCache; public DefaultCacheManager getCacheManager() { if (manager == null) { manager = new DefaultCacheManager(); } return manager; } public Cache getCache() { if (manager == null) { manager = new DefaultCacheManager(); if (manager.cacheExists("testCache") ) { logger.info("testCache already exist"); } else { logger.info("testCache create"); manager.defineConfiguration("testCache", new ConfigurationBuilder().build() ); } testCache = manager.getCache("testCache"); } else { logger.info("TC Debug, cacheManager is NOT null"); } return testCache; } @PreDestroy public void cleanUp() { manager.stop(); manager = null; } }
another bean is built to trigger the provider
@Singleton @Startup public class testCacheApplicationBean { @Inject testCacheManagerProvider cacheManagerProvider; @Produces public Cache getTestCache() { return cacheManagerProvider.getCache(); } }
a utility bean is created to maintain the cache content
package cmm.bean; import java.util.List; import java.util.Map; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import javax.annotation.PostConstruct; import javax.ejb.EJB; import javax.ejb.Singleton; // import javax.ejb.Startup; import javax.inject.Inject; import org.apache.commons.collections4.MapUtils; // import org.infinispan.Cache; // import org.infinispan.manager.DefaultCacheManager; import cmm.bean.impl.testCacheApplicationBean; import cmm.dao.CSysPostDao; import cmm.dao.CSysRoleDao; import cmm.entity.CSysPost; import entity.CSysRole; @Singleton // @Startup public class CSysAppCacheService { @Inject private testCacheApplicationBean beanCache; @EJB(name = "postService") private CSysPostDao postService; @EJB(name = "roleService") private CSysRoleDao roleService; private static final Logger logger = LogManager.getLogger(CSysAppCacheService.class); protected Map> sysRoleMap; protected Map> sysPostMap; protected List sysRoleList; protected List sysPostList; @PostConstruct public void init() { preloadCache(); } // Pre-load cache when server startup public void preloadCache() { for (CacheKey ck : CacheKey.values()) { resetCache(ck); } } public void resetCache(CacheKey key) { if (key == null) { for (CacheKey ck : CacheKey.values()) { resetCache(ck); } } else if (key.equals(CacheKey.SYS_ROLE_LIST)) { this.sysRoleList = roleService.findAllRole(); setupCacheList(CacheKey.SYS_ROLE_LIST, sysRoleList); } else if (key.equals(CacheKey.SYS_POST_LIST)) { this.sysPostList = postService.findAllPost(); setupCacheList(CacheKey.SYS_POST_LIST, sysPostList); } } protected void setupCacheList(CacheKey key, List list) { beanCache.getTestCache().put(key, list); } public List
2 separate view class is built for front-end display (both are similiar but in different war file, so I put one class only)
package stf.view; import java.io.Serializable; import java.util.ArrayList; import java.util.List; // import java.util.Map; import javax.annotation.PostConstruct; import javax.ejb.EJB; // import javax.faces.bean.ManagedBean; import javax.faces.view.ViewScoped; // import javax.inject.Inject; import javax.inject.Named; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import cmm.bean.CSysAppCacheService; import cmm.bean.CacheKey; @ViewScoped @Named public class CacheView implements Serializable { /** * */ private static final long serialVersionUID = 2504596409107933525L; private static final Logger logger = LogManager.getLogger(CacheView.class); public static final String FNCT_ID = "SAMPLCACH"; private List cacheKeyList; private CacheKey selectedKey; private List
@EJB CSysAppCacheService appScopeCache;
@PostConstruct
public void init() {
List cacheKeyArrayList = new ArrayList();
for (CacheKey cacheKey : CacheKey.values()) {
cacheKeyArrayList.add(cacheKey.toString());
}
setCacheKeyList(cacheKeyArrayList);
appScopeCache.init();
}
public List getCacheKeyList() {
return cacheKeyList;
}
public void setCacheKeyList(List cacheKeyList) {
this.cacheKeyList = cacheKeyList;
}
public CacheKey getSelectedKey() {
return selectedKey;
}
public void setSelectedKey(CacheKey selectedKey) {
this.selectedList = appScopeCache.getCacheList(selectedKey);
}
public List
return selectedList;
}
public void setSelectedList(List
String keyString = String.valueOf(key);
setSelectedKey(key);
this.selectedList = selectedList;
}
public String reloadAction() {
this.cacheKeyList.clear();
appScopeCache.preloadCache();
List cacheKeyArrayList = new ArrayList();
for (CacheKey cacheKey : CacheKey.values()) {
cacheKeyArrayList.add(cacheKey.toString());
}
setCacheKeyList(cacheKeyArrayList );
return "cache.xhtml?faces-redirect=true";
}
public String reloadAction(CacheKey inputcacheKey) {
// init();
this.cacheKeyList.clear();
appScopeCache.resetCache(inputcacheKey);
List cacheKeyArrayList = new ArrayList();
for (CacheKey cacheKey : CacheKey.values()) {
cacheKeyArrayList.add(cacheKey.toString());
}
setCacheKeyList(cacheKeyArrayList );
return "cache.xhtml?faces-redirect=true";
}
public String clearAction() {
return "cache.xhtml?faces-redirect=true";
}
public String showDetailAction() {
return "cache.xhtml?faces-redirect=true";
}
}
The 1st time I load the 2 pages, they show the identical values
Then I updated the database, and reload cache via one of the page.
The page reload page get the updated value, while the other page (not reload one) still keep the old value.
Please advise how I should fix it.
Thanks.