Struts + ehcache不会一直保留对象

时间:2016-05-21 13:30:36

标签: java caching ehcache

我在Struts应用程序中使用ehcache。我在CacheListener启动应用程序时加载缓存。接下来如果我想从缓存中获取元素,我使用CacheExt。它工作正常,但一段时间后,缓存中的这个元素是null。有什么想法吗?

public class CacheListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        CacheExt.cm.addCache("cache");
        Cache cache = CacheExt.cm.getCache("cache");
        cache.put(new Element("USERS", new String[]{"User1", "Ala", "Zdzislaw"}));
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        CacheExt.cm.shutdown();
    }
}

public class CacheExt {

    static CacheManager cm = CacheManager.getInstance();

    public static Element getCachedObject(String name) {
        Cache cache = cm.getCache("cache");
        return cache.get(name);
    }
}

public class ActionClass {
    public String execute() {
        System.out.println(Arrays.toString((String[])CacheExt.getCachedObject("USERS").getObjectValue()));
    }
}

0 个答案:

没有答案