企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
1.不要相信任务数据,包括请求的外部接口,特别是超时者部分,尽可能的交给task完成。 2.原来可以在入口文件哪里使用超全局变量声明已经实例化的server服务对象,然后就其他文件使用了。 3.异步任务(协程)里面不能再异步(开启协程任务),只能是同步(task任务下,redis只能使用同步的模式,使用异步的模式会出错!)。 4.在使用task任务下,不能使用thinkphp6.0里面的门面(`Facade`),报错原因是因为找不到这个类(因为没有加载到这里)。 5.所有耗时的任务叫交给task完成。 6.使用同步redis下的单例模式,指的是同一个请求下只连接一次,后面在这个请求里面不断重复的都是这个实例,如: [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") ~~~ <?php namespace app\common\controller; use think\facade\Config; use app\common\controller\Redis; //redis单例模式 class Predis { public $redis; /** * 定义单例模式的变量 */ private static $_instance = null; public function __construct() { $this -> redis = new \Redis(); //$result = $this -> redis->connect(Config::get('redis.host'), Config::get('redis.port'), Config::get('redis.timeOut')); $result = $this -> redis->connect('127.0.0.1', 6379, 5); if($result === false) { throw new \Exception('redis connect error'); } } /** * 1、单例类只能有一个实例。 * 2、单例类必须自己创建自己的唯一实例。 * 3、单例类必须给所有其他对象提供这一实例。 */ public static function getInstance() { if(empty(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; } /** * 存储数据 */ public function set($key, $value, $time=0) { if(empty($key)) { return ""; } if(is_array($value)) { $value = json_encode($value); } if(!$time) { return $this -> redis -> set($key, $value); } return $this -> redis -> setex($key, $time, $value); } /** *获取数据 */ public function get($key) { if(empty($key)) { return ""; } $res = $this -> redis -> get($key); return $res; } } ~~~ [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") 使用: ~~~ Predis::getInstance()->set(Redis::smsKey($data['phone']), $data['code']); ~~~ 7.在整理task功能的时候,有注意到一个有趣的事情:就是通过一个变量值来调用这个对象的方法,如: 1234567&nbsp;&nbsp;$obj =&nbsp;new&nbsp;app\common\task\Task();$method = $data['method'];if(empty($method)){&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;"method for null";}$flag = $obj -&gt; $method($data['data']); 8.能面向对象的就尽量面向对象,尽可能避免面向过程,主要是对以后维护不好维护。 9.如果需要task来处理事情的话,记得在set的时候写明task是多少,不然无法开启task任务,如: [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") ~~~ $this->http->set( [ 'enable_static_handler' => true, 'document_root' => '/www/wwwroot/tp6/public/index', 'worker_num' => 4,//worker的数量 'task_worker_num' => 4,//task的数量 ] ); ~~~ [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") 10.server里面的事件也有对应的方法,事件!=方法,你要先注册这个事件,才能使用这个方法!具体请参考server的task方法的使用! 11.使用ob\_end\_clean()方法必须要有输出才行,不然会报错!