From ca44d79be1f15148a7a4a08d3ae72ee08c6d47b5 Mon Sep 17 00:00:00 2001 From: ywisax <25803471@qq.com> Date: Tue, 6 Dec 2016 14:55:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96redis=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/controllers/ApiController.php | 4 ++-- src/Container.php | 2 ++ src/async/Task.php | 11 ++++++++- src/redis/Connection.php | 36 ++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/redis/Connection.php diff --git a/demo/controllers/ApiController.php b/demo/controllers/ApiController.php index f767df3..23cec45 100644 --- a/demo/controllers/ApiController.php +++ b/demo/controllers/ApiController.php @@ -50,8 +50,8 @@ public function actionGetUsers() public function actionTask() { //Task::pushTask("var_dump", [time()]); - //Task::pushTask("time"); - Task::pushTask('sleep', [10]); + Task::pushTask("time"); + //Task::pushTask('sleep', [10]); return time(); } } diff --git a/src/Container.php b/src/Container.php index 07fc458..af13a47 100644 --- a/src/Container.php +++ b/src/Container.php @@ -27,6 +27,7 @@ class Container extends \yii\di\Container 'yii\log\FileTarget' => 'tourze\workerman\yii2\log\FileTarget', 'yii\log\Logger' => 'tourze\workerman\yii2\log\Logger', 'yii\swiftmailer\Mailer' => 'tourze\workerman\yii2\mailer\SwiftMailer', + 'yii\redis\Connection' => 'tourze\workerman\yii2\redis\Connection', 'yii\web\Request' => 'tourze\workerman\yii2\web\Request', 'yii\web\Response' => 'tourze\workerman\yii2\web\Response', 'yii\web\Session' => 'tourze\workerman\yii2\web\Session', @@ -166,6 +167,7 @@ class Container extends \yii\di\Container 'yii\rbac\Role', 'yii\redis\Cache', 'yii\redis\Connection', + 'tourze\workerman\yii2\redis\Connection', 'yii\redis\LuaScriptBuilder', 'yii\redis\Session', 'yii\rest\Serializer', diff --git a/src/async/Task.php b/src/async/Task.php index 5f568b7..bc62ded 100644 --- a/src/async/Task.php +++ b/src/async/Task.php @@ -23,12 +23,21 @@ class Task */ public static $taskCountKey = 'task_count'; + /** + * @var Connection + */ + public static $redis; + /** * @return Connection */ public static function getRedis() { - return Yii::$app->get('redis'); + if ( ! self::$redis) + { + self::$redis = Yii::$app->get('redis'); + } + return self::$redis; } /** diff --git a/src/redis/Connection.php b/src/redis/Connection.php new file mode 100644 index 0000000..af7b625 --- /dev/null +++ b/src/redis/Connection.php @@ -0,0 +1,36 @@ +getIsActive()) + { + return; + } + parent::open(); + } + + /** + * @inheritdoc + */ + public function close() + { + // 不主动关闭redis连接 + } + + /** + * 真实关闭链接 + */ + public function realClose() + { + parent::close(); + } +}