EHCache在getCache之后返回null

时间:2018-07-05 20:47:17

标签: java caching ehcache

我要从ArrayList中的xml文件中保存一些内容,以显示在轮播中,因此每次有人进入网站,下载xml等时,我都不需要,所以我将其保存在缓存中使用ehcache。但是我有一个问题,在获取存储数组的方法中,缓存返回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();



  }

}

0 个答案:

没有答案