java - EHCache 在 getCache 之后返回 null
问题描述
我正在将 xml 文件中的一些内容保存在 ArrayList 中,以显示在轮播中,因此每次有人进入网站时,都会下载 xml 等,我不希望这样,所以我将其保存在缓存中使用缓存。但是我遇到了一个问题,在获取存储的数组的方法中,缓存返回 null,那里什么都没有。我有点卡在这部分......也许我以错误的方式获取缓存。
public class xmlCache {
//getting the xml file in a Document
public static Document loadXMLDocument() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
return factory.newDocumentBuilder().parse(new URL("https://www.w3schools.com/xml/cd_catalog.xml").openStream());
}
//putting the xml content in a ArrayList
public static ArrayList<String> listXML(){
ArrayList<String> xmlList = new ArrayList<String>();
try {
Document doc = xmlCache.loadXMLDocument();
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("CD");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
xmlList.add(eElement.getElementsByTagName("TITLE").item(0).getTextContent() + "|" +
eElement.getElementsByTagName("ARTIST").item(0).getTextContent() + "|" +
eElement.getElementsByTagName("COUNTRY").item(0).getTextContent() + "|" +
eElement.getElementsByTagName("COMPANY").item(0).getTextContent() + "|" +
eElement.getElementsByTagName("YEAR").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return xmlList;
}
//putting the ArrayList in cache
public static void cachingXML () {
ArrayList<String> xmlList = xmlCache.listXML();
CacheManager cacheManager = newCacheManagerBuilder().withCache("basicCache", newCacheConfigurationBuilder(Long.class, ArrayList.class, heap(300).offheap(5, MB)))
.build(true);
Cache<Long, ArrayList> basicCache = cacheManager.getCache("basicCache", Long.class, ArrayList.class);
basicCache.put(1L, xmlList);
ArrayList<String> teste = new ArrayList<>();
// if i do that here, is ok, the return is ok
teste = basicCache.get(1L);
System.out.println(teste.size());
}
//accessing the ArrayList stored
public static ArrayList<String> getTicker() {
ArrayList<String> ticker = new ArrayList<>();
try(CacheManager cacheManager = newCacheManagerBuilder().withCache("basicCache", newCacheConfigurationBuilder(Long.class, ArrayList.class, heap(500).offheap(3, MB)))
.build(true)){
Cache<Long, ArrayList> basicCache = cacheManager.getCache("basicCache", Long.class, ArrayList.class);
//the return is false
System.out.println(basicCache.containsKey(1L));
}catch (Throwable e) {
e.printStackTrace();
}
return ticker;
}
public static void main(String[] args) throws MalformedURLException, SAXException, IOException, ParserConfigurationException {
//this one is ok
xmlCache.cachingXML();
//this one is not
xmlCache.getTicker();
}
}
解决方案
推荐阅读
- makefile - Gnu Make:调用并行make时,如果在构建过程中提供了先决条件,make会尝试重新制作这些吗?
- angular - 从谷歌驱动器而不是本地机器上传文件
- android - Appium Native View,如何点击按钮返回上一屏
- .htaccess - 配置正确的 htaccess 规则
- php - 从购物车中删除商品
- python - PyQt5文本标签在循环显示期间冻结
- javascript - JSON 对象减少为一个对象?
- tree - 什么是自下而上解析和减少解析树的正确算法?
- django - 如何向 Django RequestFactory 请求添加标头?
- amazon-web-services - Terraform 计划不包括我所有的 .tf 更改