package com.qiwu.chatbotbackend.processing; import com.qiwu.chatbotbackend.GameEnvironment.GameEnv; import com.alibaba.fastjson.JSON; import com.qiwu.chatbotbackend.entity.ChatCache; import com.qiwu.chatbotbackend.entity.UserInfo; import javax.servlet.ServletContext; import java.io.*; import java.sql.SQLException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class ChatCacheController { /** * 通过chat_key获得后台保存的历史信息点 * chatCache:存储了chatKye相对应的对话记录(历史信息点) * * @return 历史信息点,对话过程中引擎提取的用户信息 */ // public static synchronized Map getChatCache(String chatKey, ServletContext context) { // // GameEnv gameEnv = new GameEnv(chatKey); // 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; // // // try { // historyInfoPoint = chatCache.get(chatKey); // //historyInfoPoint = (Map) context.getAttribute("chat_key+"+userinfo.userName); // } catch (Exception e) { // // //可整合 // historyInfoPoint = null; // // } // // return historyInfoPoint; // } /** * chatChche中每个chatkey都有对应的最后更新时间,如果最后更新时间已经是x分钟前(超时),就清除该chatKey的数据 */ // 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); // } /** * 根据chatKey更新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); // } public static boolean saveChatCache(String userName, HashMap infoPoints) throws IOException { String fileDir = System.getProperty("user.dir") + "/ChatCache"; String key; boolean saveFlag = true; try { File dir = new File(fileDir); if (!dir.exists()) dir.mkdir(); File file = new File(fileDir + "/" + userName + ".txt"); FileWriter fw = new FileWriter(file); Iterator infoIter = infoPoints.keySet().iterator(); while (infoIter.hasNext()) { key = infoIter.next(); fw.write(key + "-----" + infoPoints.get(key) + "\n"); } fw.close(); } catch (Exception e) { saveFlag = false; } return saveFlag; } public static boolean loadChatCache(String userName, ServletContext context) { String fileDir = System.getProperty("user.dir") + "/ChatCache"; String line, pair[]; Map infoPoints = new HashMap(); try { File file = new File(fileDir + "/" + userName + ".txt"); if (!file.exists()) { return false; } else { FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); while ((line = br.readLine()) != null) { pair = line.trim().split("-----"); infoPoints.put(pair[0], pair[1]); } // context.setAttribute(userName, ); fr.close(); } } catch (IOException e) { e.printStackTrace(); } return true; } public static synchronized ChatCache getChatCache(String chatKey, ServletContext context, UserInfo userinfo) throws Exception { Map chatCacheMap = null; ChatCache chatCache = null; if (context.getAttribute("chatCache") == null) { chatCacheMap = new HashMap(); chatCacheMap.put(chatKey, new ChatCache(userinfo)); context.setAttribute("chatCache", chatCacheMap); } else { chatCacheMap = (Map) context.getAttribute("chatCache"); } String chatkey; try { chatCache = chatCacheMap.get(chatKey); if(chatCache == null){ chatCache = new ChatCache(userinfo); chatCacheMap.put(chatKey, chatCache); } //historyInfoPoint = (Map) context.getAttribute("chat_key+"+userinfo.userName); } catch (Exception e) { //可整合 chatCache = new ChatCache(userinfo); } return chatCache; } /** * chatChche中每个chatkey都有对应的最后更新时间,如果最后更新时间已经是x分钟前(超时),就清除该chatKey的数据 */ public static synchronized void updateChatCache4Delete(String curChatKey, ServletContext context) { String chatKey; ChatCache chatCache = null; long lastChatTime, now; Map history = new HashMap(); Map chatCacheMap = (Map) context.getAttribute("chatCache"); Iterator it = chatCacheMap.keySet().iterator(); while (it.hasNext()) { chatKey = it.next(); if(curChatKey.equals(chatKey)) continue; history = chatCacheMap.get(chatKey).getInfoPoint(); lastChatTime = Long.valueOf(history.get("lastUpdateTime")); now = System.currentTimeMillis(); if (now - lastChatTime > 300000) { chatCache = chatCacheMap.get(chatKey); saveChatCache(chatKey, chatCache); it.remove(); } } context.setAttribute("chatCache", chatCacheMap); } /** * 根据chatKey更新chatCache的数据,并修改最后更新时间 */ public static synchronized void updateChatCache4Insert(Map curInfoPoint, Map historyInfoPoint, GameEnv gameEnv, ServletContext context) { historyInfoPoint.put("lastUpdateTime", String.valueOf(System.currentTimeMillis())); Map keyCacheMap = (Map) context.getAttribute("chatCache"); ChatCache chatCache = keyCacheMap.get(curInfoPoint.get("chat_key")); chatCache.setInfoPoint(historyInfoPoint); chatCache.setGameEnv(gameEnv); context.setAttribute("chatCache", keyCacheMap); } public static synchronized String saveChatCache(String chatKey, ChatCache chatCache){ if(chatCache == null){ return "chat_key不存在或者已失效"; }else { String jChatCache = JSON.toJSONString(chatCache); System.out.println(jChatCache); chatCache = JSON.parseObject(jChatCache,ChatCache.class); System.out.println(chatCache.getInfoPoint().size()); return "成功"; } } }