首页 > 解决方案 > 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();



  }

}

标签: javacachingehcache

解决方案


推荐阅读