package processing; import javax.servlet.ServletContext; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class ChatCacheController { public static synchronized Map getChatCache(String chatKey, ServletContext context){ Map historyInfoPoint = null; Map> chatCache = null; if(context.getAttribute("chatCache") == null) { chatCache = new HashMap>(); context.setAttribute("chatCache", chatCache); } else{ chatCache = (Map>) context.getAttribute("chatCache"); } String chatkey; /* Iterator it = chatCache.keySet().iterator(); while(it.hasNext()){ chatkey = it.next(); System.out.println("chatkey:"+chatkey); } */ try{ historyInfoPoint = chatCache.get(chatKey); //historyInfoPoint = (Map) context.getAttribute("chat_key+"+userinfo.userName); }catch(Exception e){ //可整合 historyInfoPoint = null; } return historyInfoPoint; } public static synchronized void updateChatCache4Delete(ServletContext context){ String chatKey; long lastChatTime,now; Map history = new HashMap(); Map> chatCache = (Map>) context.getAttribute("chatCache"); Iterator it = chatCache.keySet().iterator(); while (it.hasNext()) { chatKey = it.next(); history = chatCache.get(chatKey); lastChatTime = Long.valueOf(history.get("lastUpdateTime")); now = System.currentTimeMillis(); if (now - lastChatTime > 300000) { it.remove(); } } context.setAttribute("chatCache", chatCache); } public static synchronized void updateChatCache4Insert(Map curInfoPoint,Map historyInfoPoint,ServletContext context){ historyInfoPoint.put("lastUpdateTime",String.valueOf(System.currentTimeMillis())); Map> keyCache = (Map>) context.getAttribute("chatCache"); keyCache.put(curInfoPoint.get("chat_key")[0],historyInfoPoint); context.setAttribute("chatCache",keyCache); } }