簡單定時任務解決方案:使用redis的keyspace notifications(鍵失效后通知事件) 需要注意此功能是在redis 2.8版本以后推出的,因此你服務器上的reids最少要是2.8版本以上;
(A)業務場景:
1、當一個業務觸發以后需要啟動一個定時任務,在指定時間內再去執行一個任務(如自動取消訂單,自動完成訂單等功能)
2、redis的keyspace notifications 會在key失效后發送一個事件,監聽此事件的的客戶端就可以收到通知
(B)服務準備:
1、修改reids配置文件(redis.conf)【window系統配置文件為:redis.windows.conf 】
redis默認不會開啟keyspace notifications,因為開啟后會對cpu有消耗
備注:E:keyevent事件,事件以__keyevent@<db>__為前綴進行發布;
x:過期事件,當某個鍵過期并刪除時會產生該事件;
原配置為:
notify-keyspace-events ""
更改 配置如下:
notify-keyspace-events "Ex"
保存配置后,重啟Redis服務,使配置生效
[root@chokingwin etc]#service redis-server restart /usr/local/redis/etc/redis.conf Stopping redis-server: [ OK ] Starting redis-server: [ OK ]
window系統重啟redis ,先切換到redis文件目錄,然后關閉redis服務(redis-server --service-stop),再開啟(redis-server --service-start)
C)文件代碼:
phpredis實現訂閱Keyspace notification,可實現自動取消訂單,自動完成訂單。以下為測試例子
創建4個文件,然后自行修改數據庫和redis配置參數
db.class.php
<?phpclass mysql{ private $mysqli; private $result; /** * 數據庫連接 * @param $config 配置數組 */ public function connect() { $config=array( 'host'=>'127.0.0.1', 'username'=>'root', 'password'=>'168168', 'database'=>'test', 'port'=>3306, ); $host = $config['host']; //主機地址 $username = $config['username'];//用戶名 $password = $config['password'];//密碼 $database = $config['database'];//數據庫 $port = $config['port']; //端口號 $this->mysqli = new mysqli($host, $username, $password, $database, $port); } /** * 數據查詢 * @param $table 數據表 * @param null $field 字段 * @param null $where 條件 * @return mixed 查詢結果數目 */ 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 獲取全部結果 */ public function fetchAll() { return $this->result->fetch_all(MYSQLI_ASSOC); } /** * 插入數據 * @param $table 數據表 * @param $data 數據數組 * @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; } /** * 更新數據 * @param $table 數據表 * @param $data 數據數組 * @param $where 過濾條件 * @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; } /** * 刪除數據 * @param $table 數據表 * @param $where 過濾條件 * @return mixed 受影響記錄 */ public function delete($table, $where) { $sql = "DELETE FROM `{$table}` WHERE {$where}"; $this->mysqli->query($sql); return $this->mysqli->affected_rows; }}
新聞熱點
疑難解答
圖片精選