我在Spring-4应用程序中从另一个可缓存方法调用cacheable方法时遇到了问题,请阅读以下步骤以获得更多说明。
1) MyAppStartup class is called when server is started and it call convertXMLToObject method and store data in myInfo cache. 2) getFormList(String myId) method is called from some controller and ideally this method not invoke convertXMLToObject() method because in step-1, data already in cache but it is not working expected. 3) When second time getFormList(String myId) is called, it is not getting invoked and data is returned from cache i.e. cache is working fine for this method.@Component
public class MyAppStartup {
@Autowired
private MyHelperClass myHelperClass;
@EventListener(ContextRefreshedEvent.class)
public void contextRefreshedEvent() throws Exception {
logger.debug("Application Started :: Call to load XML information into Cache");
myHelperClass.convertXMLToObject();
}
}
@Service
public class MyHelperClass {
@Cacheable(value = "myInfoById", key = "{#myId}")
public List<XMLFormData> getFormList(String myId){
List<XMLFormData> xmlFormData = convertXMLToObject();
return xmlFormData;
}
@Cacheable(value = "myInfo")
public List<XMLFormData> convertXMLToObject() {
//code to read xml and populate into java pojo class and return list
}
}
//configuration in ehcache.xml
<cache name="myInfoById"
eternal="false"
overflowToDisk="false"
maxEntriesLocalHeap="1000"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
memoryStoreEvictionPolicy="LRU" />
<cache name="myInfo"
eternal="false"
overflowToDisk="false"
maxEntriesLocalHeap="1000"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
memoryStoreEvictionPolicy="LRU" />请帮帮忙,提前谢谢:)
发布于 2019-11-21 21:14:32
我得到了解决方案,而不是从另一个可缓存方法“convertXMLToObject(String myId)”调用可缓存方法“getFormList()”,只需从方法"getFormList(String myId)“被调用的地方调用convertXMLToObject()方法,并将所需的数据传递给方法"getFormList(String myId)”。
https://stackoverflow.com/questions/58957957
复制相似问题