<?php ini_set('display_errors', 'on'); class chatClass { private $redis; //這個變量模擬用戶當前狀態,是否登陸,是否可查看 public $checkUserReadable = false; //構造函數連接redis數據庫 public function __construct() { $this -> redis = new Redis(); $this -> redis -> connect('127.0.0.1', '6379'); $this -> redis -> auth('***cnblogs.com/handle'); } /* 發送消息時保存聊天記錄 * 這裏用的redis存儲是list數據類型 * 兩我的的聊天用一個list保存 * * @from 消息發送者id * @to 消息接受者id * @meassage 消息內容 * * 返回值,當前聊天的總聊天記錄數 */ public function setChatRecord($from, $to, $message) { $data = array('from' => $from, 'to' => $to, 'message' => $message, 'sent' => time()/*, 'recd' => 0*/); $value = json_encode($data); //生成json字符串 $keyName = 'rec:' . $this -> getRecKeyName($from, $to); //echo $keyName; $res = $this -> redis -> lPush($keyName, $value); if (!$this -> checkUserReadable) {//消息接受者沒法馬上查看時,將消息設置爲未讀 $this -> cacheUnreadMsg($from, $to); } return $res; } /* * 獲取聊天記錄 * @from 消息發送者id * @to 消息接受者id * @num 獲取的數量 * * 返回值,指定長度的包含聊天記錄的數組 */ public function getChatRecord($from, $to, $num) { $keyName = 'rec:' . $this -> getRecKeyName($from, $to); //echo $keyName; $recList = $this -> redis -> lRange($keyName, 0, (int)($num)); return $recList; } /* * 當用戶上線時,或點開聊天框時,獲取未讀消息的數目 * @user 用戶id * * 返回值,一個全部當前用戶未讀的消息的發送者和數組 * 數組格式爲‘消息發送者id’=>‘未讀消息數目’ * */ public function getUnreadMsgCount($user) { return $this -> redis -> hGetAll('unread_' . $user); } /* * 獲取未讀消息的內容 * 經過未讀消息數目,在列表中取得最新的相應消息即爲未讀 * @from 消息發送者id * @to 消息接受者id * * 返回值,包括全部未讀消息內容的數組 * * */ public function getUnreadMsg($from, $to) { $countArr = $this -> getUnreadMsgCount($to); $count = $countArr[$from]; $keyName = 'rec:' . $this -> getRecKeyName($from, $to); return $this -> redis -> lRange($keyName, 0, (int)($count)); } /* * 將消息設爲已讀 * 當一個用戶打開另外一個用戶的聊天框時,將全部未讀消息設爲已讀 * 清楚未讀消息中的緩存 * @from 消息發送者id * @to 消息接受者id * * 返回值,成功將未讀消息設爲已讀則返回true,沒有未讀消息則返回false */ public function setUnreadToRead($from, $to) { $res = $this -> redis -> hDel('unread_' . $to, $from); return (bool)$res; } /* * 當用戶不在線時,或者當前沒有馬上接收消息時,緩存未讀消息,將未讀消息的數目和發送者信息存到一個與接受者關聯的hash數據中 * * @from 發送消息的用戶id * @to 接收消息的用戶id * * 返回值,當前兩個用戶聊天中的未讀消息 * */ private function cacheUnreadMsg($from, $to) { return $this -> redis -> hIncrBy('unread_' . $to, $from, 1); } /*生成聊天記錄的鍵名,即按大小規則將兩個數字排序 * @from 消息發送者id * @to 消息接受者id * * */ private function getRecKeyName($from, $to) { return ($from > $to) ? $to . '_' . $from : $from . '_' . $to; } } /* * 下面爲測試用的代碼 ,僞造數據模擬場景 * 假定有兩個用戶id爲2和3 ,2 向 3 發送消息 * $chat = new chatClass(); $chat -> checkUserReadable = true; for ($i = 0; $i < 20; $i++) { $chat -> setChatRecord('2', '3', 'message_' . $i); } echo 'get 20 chat records</br>'; $arr = $chat -> getChatRecord('2', '3', 20); for ($j = 0; $j < count($arr); $j++) { echo $arr[$j] . '</br>'; } $chat -> checkUserReadable = false; for ($m = 0; $m < 5; $m++) { $chat -> setChatRecord('2', '3', 'message_' . $m); } echo "</br>"; $umsg_1 = $chat -> getUnreadMsgCount(3); echo "Unread message counts "; echo "</br>"; print_r($umsg_1); echo "Unread message content </br> "; $umsgContent = $chat -> getUnreadMsg(2, 3); for ($n = 0; $n < count($umsgContent); $n++) { echo $arr[$n] . '</br>'; } echo "</br>"; $chat -> setUnreadToRead(2, 3); $umsg_2 = $chat -> getUnreadMsgCount(3); echo "</br>"; echo "Unread message counts "; echo "</br>"; print_r($umsg_2); * */ ?>