PHP使用Redis隊(duì)列執(zhí)行定時(shí)任務(wù)實(shí)例講解
更新時(shí)間:2021年03月19日 16:57:20 作者:章全蛋
這篇文章主要介紹了PHP使用Redis隊(duì)列執(zhí)行定時(shí)任務(wù)實(shí)例講解,redis隊(duì)列是比較常用的功能,有感興趣的同學(xué)可以學(xué)習(xí)下
Redis類:
<?php namespace Utils; use Phalcon\Config\Adapter\Ini as ConfigIni; class Redis{ private static $redis1; private static $session; /** * 獲取一個(gè)單例的redis對象 * @param string $name * @return \Redis */ public static function getObj($name='redis1') { try{ if(!empty(self::$$name)){ return self::$$name; } $config = new ConfigIni(APP_PATH."/config".ENV."/redis.ini"); self::$$name = new \Redis(); self::$$name->connect($config[$name]['host'], $config[$name]['port'],2); if(isset($config[$name]['password']) && !empty($config[$name]['password'])){ self::$$name->auth($config[$name]['password']); } self::$$name->select($config[$name]['database']); }catch (\Exception $exception){ self::$$name = false; } return self::$$name; } };
定時(shí)任務(wù):
/** * 訂單任務(wù) */ public function orderAction() { error_reporting(E_ALL & ~E_NOTICE); $redis = Redis::getObj(); //獲取數(shù)據(jù)庫連接實(shí)例 $db = $this->getDI()->getShared('db'); while (true) { print_r(' -start- '); $order_status = 1; file_put_contents(APP_PATH . "/../domain_order.log", time()); try { //防止長時(shí)間無任務(wù)導(dǎo)致MySQL超時(shí) $db->query("select 1"); //出列 $order_info = $redis->lPop('order'); if (!$order_info) {//隊(duì)列為空時(shí)暫停 echo ' -empty- '; sleep(1); continue; } $order_info = json_decode($order_info, true); $model_order_info = NetUserOrder::findFirst(['order_sn = :order_sn:','bind'=>['order_sn'=>$order_domain_info_save->order_sn]]); //未支付 if($model_order_info->pay_status != 200){ echo 'no pay'; continue; } //已操作 if ($order_domain_info_save->order_status == 3) { echo ' -Operated- '; continue; } //事務(wù)開始 $db->begin(); ## 這里執(zhí)行訂單流程操作 ## $order_status = 3;//操作成功 //修改訂單狀態(tài) $order_domain_info_save->order_status = $order_status; $order_domain_info_save->operation_time = time(); $order_domain_info_save->update(); } //提交事務(wù) $db->commit(); printf('### succ order_id' . $order_info['id'] . ' ###'); } catch (\Exception $e) { //回滾事務(wù) $db->rollback(); $order_status = 2;//操作失敗 $order_domain_info_save->order_status = $order_status; $order_domain_info_save->operation_time = time(); $order_domain_info_save->update(); printf(' error ' . $e->getMessage() . ' '); //異常,發(fā)送通知 Log::error($e->getMessage()); $redis->hSet('order_domain_retry', 'domain_retry_' . $order_info['id'], json_encode($order_info)); }
Redis常用隊(duì)列方法:
//隊(duì)列第一個(gè) =>出列 $Redis->lPop($key); //入到 =>隊(duì)列最后 $Redis->rPush($key); //隊(duì)列最后一個(gè) =>出列 $Redis->rPop($key); //入到 =>隊(duì)列第一個(gè) $Redis->rPop($key); //返回整個(gè)列表的值,不出列 $redis->lRange($key,0,-1);
到此這篇關(guān)于PHP使用Redis隊(duì)列執(zhí)行定時(shí)任務(wù)實(shí)例講解的文章就介紹到這了,更多相關(guān)PHP使用Redis隊(duì)列執(zhí)行定時(shí)任務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在PHP中靈活使用foreach+list處理多維數(shù)組的方法
這篇文章主要介紹了在PHP中靈活使用foreach+list處理多維數(shù)組的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01py文件轉(zhuǎn)exe時(shí)包含paramiko模塊出錯(cuò)解決方法
這篇文章主要介紹了py文件轉(zhuǎn)exe時(shí)包含paramiko模塊出錯(cuò)解決方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08IOS 開發(fā)之NSDictionary轉(zhuǎn)換成JSON字符串
這篇文章主要介紹了IOS 開發(fā)之NSDictionary轉(zhuǎn)換成JSON字符串的相關(guān)資料,這里提供實(shí)例幫助大家學(xué)習(xí)理解這部分知識,需要的朋友可以參考下2017-08-08PHP笛卡爾積實(shí)現(xiàn)原理及代碼實(shí)例
這篇文章主要介紹了PHP笛卡爾積實(shí)現(xiàn)原理及代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12