簡(jiǎn)單定時(shí)任務(wù)解決方案:使用redis的keyspace notifications(鍵失效后通知事件) 需要注意此功能是在redis 2.8版本以后推出的,因此你服務(wù)器上的reids最少要是2.8版本以上;
(A)業(yè)務(wù)場(chǎng)景:
1、當(dāng)一個(gè)業(yè)務(wù)觸發(fā)以后需要啟動(dòng)一個(gè)定時(shí)任務(wù),在指定時(shí)間內(nèi)再去執(zhí)行一個(gè)任務(wù)(如自動(dòng)取消訂單,自動(dòng)完成訂單等功能)
2、redis的keyspace notifications 會(huì)在key失效后發(fā)送一個(gè)事件,監(jiān)聽(tīng)此事件的的客戶端就可以收到通知
(B)服務(wù)準(zhǔn)備:
1、修改reids配置文件(redis.conf)【window系統(tǒng)配置文件為:redis.windows.conf 】
redis默認(rèn)不會(huì)開(kāi)啟keyspace notifications,因?yàn)殚_(kāi)啟后會(huì)對(duì)cpu有消耗
備注:E:keyevent事件,事件以__keyevent@<db>__為前綴進(jìn)行發(fā)布;
x:過(guò)期事件,當(dāng)某個(gè)鍵過(guò)期并刪除時(shí)會(huì)產(chǎn)生該事件;
原配置為:
notify-keyspace-events ""
更改 配置如下:
notify-keyspace-events "Ex"
保存配置后,重啟Redis服務(wù),使配置生效
[root@chokingwin etc]#service redis-server restart /usr/local/redis/etc/redis.conf Stopping redis-server: [ OK ] Starting redis-server: [ OK ]
window系統(tǒng)重啟redis ,先切換到redis文件目錄,然后關(guān)閉redis服務(wù)(redis-server --service-stop),再開(kāi)啟(redis-server --service-start)
C)文件代碼:
phpredis實(shí)現(xiàn)訂閱Keyspace notification,可實(shí)現(xiàn)自動(dòng)取消訂單,自動(dòng)完成訂單。以下為測(cè)試?yán)?/p>
創(chuàng)建4個(gè)文件,然后自行修改數(shù)據(jù)庫(kù)和redis配置參數(shù)
db.class.php
<?phpclass mysql{ private $mysqli; private $result; /** * 數(shù)據(jù)庫(kù)連接 * @param $config 配置數(shù)組 */ public function connect() { $config=array( 'host'=>'127.0.0.1', 'username'=>'root', 'password'=>'168168', 'database'=>'test', 'port'=>3306, ); $host = $config['host']; //主機(jī)地址 $username = $config['username'];//用戶名 $password = $config['password'];//密碼 $database = $config['database'];//數(shù)據(jù)庫(kù) $port = $config['port']; //端口號(hào) $this->mysqli = new mysqli($host, $username, $password, $database, $port); } /** * 數(shù)據(jù)查詢 * @param $table 數(shù)據(jù)表 * @param null $field 字段 * @param null $where 條件 * @return mixed 查詢結(jié)果數(shù)目 */ public function select($table, $field = null, $where = null) { $sql = "SELECT * FROM `{$table}`"; //echo $sql;exit; if (!empty($field)) { $field = '`' . implode('`,`', $field) . '`'; $sql = str_replace('*', $field, $sql); } if (!empty($where)) { $sql = $sql . ' WHERE ' . $where; } $this->result = $this->mysqli->query($sql); return $this->result; } /** * @return mixed 獲取全部結(jié)果 */ public function fetchAll() { return $this->result->fetch_all(MYSQLI_ASSOC); } /** * 插入數(shù)據(jù) * @param $table 數(shù)據(jù)表 * @param $data 數(shù)據(jù)數(shù)組 * @return mixed 插入ID */ public function insert($table, $data) { foreach ($data as $key => $value) { $data[$key] = $this->mysqli->real_escape_string($value); } $keys = '`' . implode('`,`', array_keys($data)) . '`'; $values = '/'' . implode("','", array_values($data)) . '/''; $sql = "INSERT INTO `{$table}`( {$keys} )VALUES( {$values} )"; $this->mysqli->query($sql); return $this->mysqli->insert_id; } /** * 更新數(shù)據(jù) * @param $table 數(shù)據(jù)表 * @param $data 數(shù)據(jù)數(shù)組 * @param $where 過(guò)濾條件 * @return mixed 受影響記錄 */ public function update($table, $data, $where) { foreach ($data as $key => $value) { $data[$key] = $this->mysqli->real_escape_string($value); } $sets = array(); foreach ($data as $key => $value) { $kstr = '`' . $key . '`'; $vstr = '/'' . $value . '/''; array_push($sets, $kstr . '=' . $vstr); } $kav = implode(',', $sets); $sql = "UPDATE `{$table}` SET {$kav} WHERE {$where}"; $this->mysqli->query($sql); return $this->mysqli->affected_rows; } /** * 刪除數(shù)據(jù) * @param $table 數(shù)據(jù)表 * @param $where 過(guò)濾條件 * @return mixed 受影響記錄 */ public function delete($table, $where) { $sql = "DELETE FROM `{$table}` WHERE {$where}"; $this->mysqli->query($sql); return $this->mysqli->affected_rows; }}
新聞熱點(diǎn)
疑難解答
圖片精選