@Override
@ResponseBody
public JSONArray queryFriendList(String userid) throws Exception{
JSONObject resultJobj = new JSONObject();
JSONArray tempArray = new JSONArray();
List<String> listids =new ArrayList<String>();//用于存放不在緩存裡的使用者資訊
LinkedList<User> userliList =new LinkedList<User>();//存放使用者資訊
Loger.logTxt("chat",QueryFriendServiceImpl.class.getName()+"查詢好友清單業務開始.....");
try {
userliList=(LinkedList<User>) MemCacheUtil.get("friendOBJlist"+userid)==null?userliList:(LinkedList<User>) MemCacheUtil.get("friendOBJlist"+userid);//1、直接從緩存中查詢使用者好友obj資訊list
userliList=(LinkedList<User>) MemCacheUtil.get("friendOBJlist"+userid)
if(userliList.size()>0){//緩存中有值
//拿到最新使用者資訊
for (int i = 0; i < userliList.size(); i++) {
userliList.remove(userliList.get(i));//先移除對象
userliList.add((User)MemCacheUtil.get(String.valueOf(userliList.get(i).getUserId())));//再新增對象
// 如果有好友,好友資訊的相關字段
resultJobj.put("friendId", userliList.get(i).getUserId() + " ");
resultJobj.put("headPic", userliList.get(i).getHeadPic() + " ");
resultJobj.put("nickName", userliList.get(i).getNickName() + " ");
resultJobj.put("chatId", userliList.get(i).getChatId() + " ");
tempArray.add(resultJobj);
}
// tempArray.add(userliList);
} else {//2、緩存中不存在好友對象list ,去緩存中或資料中 拿到好友對應關系 list
// 此處緩存中拿到的好友關系list 始終是最新的,因為:在更新、修改、删除好友關系時,已經對此緩存資訊更新
List<FriendInfo> friendlist = (List<FriendInfo>) MemCacheUtil.get("t_chat_friendinfo"+userid);//從緩存中取出好友關系list
//緩存中沒有的則從資料庫中取出放入緩存中
if (friendlist == null) {
// 查詢好友清單
friendlist= friendInfoDAO.queryFriendList(userid);
//将查詢的值放入緩存中
MemCacheUtil.set("t_chat_friendinfo"+userid,friendlist);
}
//3、 周遊好友關系list 拿到每個好友對象資訊
for (int i = 0; i < friendlist.size() ; i++) {
Loger.logTxt("chat",QueryFriendServiceImpl.class.getName()+"檢視好友資訊");
//檢視緩存中好友的值
User friendUser = (User) MemCacheUtil.get(String.valueOf(friendlist.get(i).getFriendid()));
if (friendUser == null) {//當緩存中拿不到該好友資訊,則儲存其id
listids.add(String.valueOf(friendlist.get(i).getFriendid()));
}else {//如果拿到好友資訊 則 放入list
userliList.add(friendUser);
resultJobj.put("friendId", friendUser.getUserId() + " ");
resultJobj.put("headPic",friendUser.getHeadPic() + " ");
resultJobj.put("nickName",friendUser.getNickName() + " ");
resultJobj.put("chatId",friendUser.getChatId() + " ");
tempArray.add(resultJobj);
}
}
//3、對拿不到好友資訊的ids 去資料庫批量讀取其資訊
if(listids.size()>0){
//去資料庫拉去好友資訊obj
List<User> friendlist2=userDao.batchQueryByUserid(listids);//去資料庫 根據ids 批量查詢使用者資訊
//查詢出來的資料 存放 list
for (int i = 0; i < friendlist2.size(); i++) {
userliList.add(friendlist2.get(i));
resultJobj.put("friendId", friendlist2.get(i).getUserId() + " ");
resultJobj.put("headPic",friendlist2.get(i).getHeadPic() + " ");
resultJobj.put("nickName",friendlist2.get(i).getNickName() + " ");
resultJobj.put("chatId",friendlist2.get(i).getChatId() + " ");
tempArray.add(resultJobj);
}
}else{
//表示都存在緩存 或者無好友
}
if(userliList.size()>0){//得到最終某個使用者的所有好友對象資訊
MemCacheUtil.set("friendOBJlist"+userid, userliList);//存放使用者好友資訊list
}
// tempArray.add(userliList);
}
} catch (NullPointerException e) {
Loger.logTxt("chat",QueryFriendServiceImpl.class.getName()+"查詢好友清單,伺服器内部錯誤:" +e);
e.printStackTrace();
resultJobj.put("result", Constants.RETURN_FAILURE);
resultJobj.put("desc", Constants.RETURN_SERVER_ERROR);
} catch (Exception e) {
Loger.logTxt("chat",QueryFriendServiceImpl.class.getName()+"查詢好友清單,伺服器内部錯誤:" +e);
e.printStackTrace();
resultJobj.put("result", Constants.RETURN_FAILURE);
resultJobj.put("desc", Constants.RETURN_SERVER_ERROR);
}
return tempArray;
}