Skip to content

Commit

Permalink
优化redis重复连接的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ywisax committed Dec 6, 2016
1 parent 4dd6bca commit ca44d79
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
4 changes: 2 additions & 2 deletions demo/controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
2 changes: 2 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
11 changes: 10 additions & 1 deletion src/async/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
36 changes: 36 additions & 0 deletions src/redis/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace tourze\workerman\yii2\redis;

class Connection extends \yii\redis\Connection
{

/**
* @inheritdoc
*/
public function open()
{
// 链接还生效的话,就不重连了
if ($this->getIsActive())
{
return;
}
parent::open();
}

/**
* @inheritdoc
*/
public function close()
{
// 不主动关闭redis连接
}

/**
* 真实关闭链接
*/
public function realClose()
{
parent::close();
}
}

0 comments on commit ca44d79

Please sign in to comment.