| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6a9a86c commit 3606778
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -77,7 +77,7 @@ function connect() | |||
| 77 | 77 | try | |
| 78 | 78 | { | |
| 79 | 79 | $this->_redis = new \Redis(); | |
| 80 | - if ($this->config['pconnect']) | ||
| 80 | + if (!empty($this->config['pconnect'])) | ||
| 81 | 81 | { | |
| 82 | 82 | $this->_redis->pconnect($this->config['host'], $this->config['port'], $this->config['timeout']); | |
| 83 | 83 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,7 +37,7 @@ function _createObject() | |||
| 37 | 37 | { | |
| 38 | 38 | $object = $this->pool->pop(); | |
| 39 | 39 | //必须要 Swoole 2.1.1 以上版本 | |
| 40 | - if ($object->connected === false) | ||
| 40 | + if (property_exists($object, "connected") and $object->connected === false) | ||
| 41 | 41 | { | |
| 42 | 42 | continue; | |
| 43 | 43 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,77 @@ | |||
| 1 | + <?php | ||
| 2 | + | ||
| 3 | + namespace Swoole\Coroutine\Component\Hook; | ||
| 4 | + | ||
| 5 | + use Swoole\Coroutine\Component\Base; | ||
| 6 | + use Swoole\Coroutine\Context; | ||
| 7 | + use Swoole\Database\MySQLi as CoMysql; | ||
| 8 | + | ||
| 9 | + | ||
| 10 | + class MySQL extends Base | ||
| 11 | + { | ||
| 12 | + protected $type = 'mysql'; | ||
| 13 | + | ||
| 14 | + function __construct($config) | ||
| 15 | + { | ||
| 16 | + parent::__construct($config); | ||
| 17 | + \Swoole::getInstance()->beforeAction([$this, '_createObject'], \Swoole::coroModuleDb); | ||
| 18 | + \Swoole::getInstance()->afterAction([$this, '_freeObject'],\Swoole::coroModuleDb); | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + function create() | ||
| 22 | + { | ||
| 23 | + $db = new CoMySQL($this->config); | ||
| 24 | + if ($db->connect() === false) | ||
| 25 | + { | ||
| 26 | + return false; | ||
| 27 | + } | ||
| 28 | + else | ||
| 29 | + { | ||
| 30 | + return $db; | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + function query($sql) | ||
| 35 | + { | ||
| 36 | + /** | ||
| 37 | + * @var $db CoMySQL | ||
| 38 | + */ | ||
| 39 | + $db = $this->_getObject(); | ||
| 40 | + if (!$db) | ||
| 41 | + { | ||
| 42 | + return false; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + $result = false; | ||
| 46 | + for ($i = 0; $i < 2; $i++) | ||
| 47 | + { | ||
| 48 | + $result = $db->query($sql); | ||
| 49 | + if ($result === false) | ||
| 50 | + { | ||
| 51 | + $db->close(); | ||
| 52 | + Context::delete($this->type); | ||
| 53 | + $db = $this->_createObject(); | ||
| 54 | + continue; | ||
| 55 | + } | ||
| 56 | + break; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + return $result; | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + /** | ||
| 63 | + * 调用$driver的自带方法 | ||
| 64 | + * @param $method | ||
| 65 | + * @param array $args | ||
| 66 | + * @return mixed | ||
| 67 | + */ | ||
| 68 | + function __call($method, $args = array()) | ||
| 69 | + { | ||
| 70 | + $redis = $this->_getObject(); | ||
| 71 | + if (!$redis) | ||
| 72 | + { | ||
| 73 | + return false; | ||
| 74 | + } | ||
| 75 | + return $redis->{$method}(...$args); | ||
| 76 | + } | ||
| 77 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,40 @@ | |||
| 1 | + <?php | ||
| 2 | + | ||
| 3 | + namespace Swoole\Coroutine\Component\Hook; | ||
| 4 | + | ||
| 5 | + use Swoole\Coroutine\Component\Base; | ||
| 6 | + use Swoole\Component\Redis as CoRedis; | ||
| 7 | + | ||
| 8 | + class Redis extends Base | ||
| 9 | + { | ||
| 10 | + protected $type = 'redis'; | ||
| 11 | + | ||
| 12 | + function __construct($config) | ||
| 13 | + { | ||
| 14 | + parent::__construct($config); | ||
| 15 | + \Swoole::getInstance()->beforeAction([$this, '_createObject'],\Swoole::coroModuleRedis); | ||
| 16 | + \Swoole::getInstance()->afterAction([$this, '_freeObject'],\Swoole::coroModuleRedis); | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + function create() | ||
| 21 | + { | ||
| 22 | + return new CoRedis($this->config); | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + /** | ||
| 26 | + * 调用$driver的自带方法 | ||
| 27 | + * @param $method | ||
| 28 | + * @param array $args | ||
| 29 | + * @return mixed | ||
| 30 | + */ | ||
| 31 | + function __call($method, $args = array()) | ||
| 32 | + { | ||
| 33 | + $redis = $this->_getObject(); | ||
| 34 | + if (!$redis) | ||
| 35 | + { | ||
| 36 | + return false; | ||
| 37 | + } | ||
| 38 | + return $redis->{$method}(...$args); | ||
| 39 | + } | ||
| 40 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -82,6 +82,9 @@ function __construct($db_config) | |||
| 82 | 82 | case self::TYPE_COMYSQL: | |
| 83 | 83 | $this->_db = new Coroutine\Component\MySQL($db_config); | |
| 84 | 84 | break; | |
| 85 | + case self::TYPE_COHOOKMYSQL: | ||
| 86 | + $this->_db = new Coroutine\Component\HOOK\MySQL($db_config); | ||
| 87 | + break; | ||
| 85 | 88 | default: | |
| 86 | 89 | $this->_db = new Database\PdoDB($db_config); | |
| 87 | 90 | break; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -109,6 +109,8 @@ class Swoole | |||
| 109 | 109 | * @var bool | |
| 110 | 110 | */ | |
| 111 | 111 | static $enableCoroutine = false; | |
| 112 | + static $coroutineStreamHook = []; | ||
| 113 | + static $coroutineStreamHookSupported = ['redis','mysql']; | ||
| 112 | 114 | protected static $coroutineInit = false; | |
| 113 | 115 | const coroModuleDb = 1; | |
| 114 | 116 | const coroModuleRedis = 2; | |
@@ -317,6 +319,22 @@ static function go($func) | |||
| 317 | 319 | }); | |
| 318 | 320 | } | |
| 319 | 321 | ||
| 322 | + static function setCoroutineStreamHook($type = null) | ||
| 323 | + { | ||
| 324 | + if ($type) | ||
| 325 | + { | ||
| 326 | + if (!in_array($type, self::$coroutineStreamHookSupported)) | ||
| 327 | + { | ||
| 328 | + throw new RuntimeException("stream hook type [$type] not support"); | ||
| 329 | + } | ||
| 330 | + self::$coroutineStreamHook[$type] = $type; | ||
| 331 | + } | ||
| 332 | + foreach (self::$coroutineStreamHookSupported as $_type) | ||
| 333 | + { | ||
| 334 | + self::$coroutineStreamHook[$_type] = $_type; | ||
| 335 | + } | ||
| 336 | + } | ||
| 337 | + | ||
| 320 | 338 | /** | |
| 321 | 339 | * 执行Hook函数列表 | |
| 322 | 340 | * @param $type | |
@@ -490,16 +508,23 @@ protected function loadModule($module, $id = 'master') | |||
| 490 | 508 | { | |
| 491 | 509 | $object = require $user_factory_file; | |
| 492 | 510 | } | |
| 493 | - //Swoole 2.0 协程模式 | ||
| 511 | + //Swoole协程模式 | ||
| 494 | 512 | elseif (self::$enableCoroutine) | |
| 495 | 513 | { | |
| 496 | - $system_factory_2x_file = LIBPATH . '/factory_2x/' . $module . '.php'; | ||
| 514 | + if (!empty(self::$coroutineStreamHook[$module])) | ||
| 515 | + { | ||
| 516 | + $system_factory_file = LIBPATH . '/factory_hook/' . $module . '.php'; | ||
| 517 | + } | ||
| 518 | + else | ||
| 519 | + { | ||
| 520 | + $system_factory_file = LIBPATH . '/factory_2x/' . $module . '.php'; | ||
| 521 | + } | ||
| 497 | 522 | //不存在,继续使用 1.x 的工厂 | |
| 498 | - if (!is_file($system_factory_2x_file)) | ||
| 523 | + if (!is_file($system_factory_file)) | ||
| 499 | 524 | { | |
| 500 | 525 | goto get_factory_file; | |
| 501 | 526 | } | |
| 502 | - $object = require $system_factory_2x_file; | ||
| 527 | + $object = require $system_factory_file; | ||
| 503 | 528 | } | |
| 504 | 529 | //系统默认 | |
| 505 | 530 | else | |
@@ -763,33 +788,33 @@ function handlerServer(Swoole\Request $request) | |||
| 763 | 788 | /** | |
| 764 | 789 | * 加载所有模块 | |
| 765 | 790 | */ | |
| 766 | - function loadAllModules() | ||
| 791 | + function loadAllModules($type = null) | ||
| 767 | 792 | { | |
| 768 | - //redis | ||
| 769 | - $redis_conf = $this->config['redis']; | ||
| 770 | - if (!empty($redis_conf)) | ||
| 793 | + $support = ['db','redis','cache']; | ||
| 794 | + if (!empty($type) and in_array($type,$support)) | ||
| 771 | 795 | { | |
| 772 | - foreach ($redis_conf as $k => $v) | ||
| 796 | + $conf = $this->config[$type]; | ||
| 797 | + if (!empty($conf)) | ||
| 773 | 798 | { | |
| 774 | - $this->loadModule('redis', $k); | ||
| 775 | - } | ||
| 776 | - } | ||
| 777 | - //cache | ||
| 778 | - $cache_conf = $this->config['cache']; | ||
| 779 | - if (!empty($cache_conf)) | ||
| 780 | - { | ||
| 781 | - foreach ($cache_conf as $k => $v) | ||
| 782 | - { | ||
| 783 | - $this->loadModule('cache', $k); | ||
| 799 | + foreach ($conf as $k => $v) | ||
| 800 | + { | ||
| 801 | + $this->loadModule($type, $k); | ||
| 802 | + } | ||
| 784 | 803 | } | |
| 785 | 804 | } | |
| 786 | - //db | ||
| 787 | - $db_conf = $this->config['db']; | ||
| 788 | - if (!empty($db_conf)) | ||
| 805 | + else | ||
| 789 | 806 | { | |
| 790 | - foreach ($db_conf as $k => $v) | ||
| 807 | + | ||
| 808 | + foreach ($support as $_type) | ||
| 791 | 809 | { | |
| 792 | - $this->loadModule('db', $k); | ||
| 810 | + $conf = $this->config[$_type]; | ||
| 811 | + if (!empty($conf)) | ||
| 812 | + { | ||
| 813 | + foreach ($conf as $k => $v) | ||
| 814 | + { | ||
| 815 | + $this->loadModule($_type, $k); | ||
| 816 | + } | ||
| 817 | + } | ||
| 793 | 818 | } | |
| 794 | 819 | } | |
| 795 | 820 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,27 @@ | |||
| 1 | + <?php | ||
| 2 | + global $php; | ||
| 3 | + $configs = $php->config['db']; | ||
| 4 | + if (empty($configs[$php->factory_key])) | ||
| 5 | + { | ||
| 6 | + throw new Swoole\Exception\Factory("db->{$php->factory_key} is not found."); | ||
| 7 | + } | ||
| 8 | + $config = $configs[$php->factory_key]; | ||
| 9 | + | ||
| 10 | + $config['type'] = \Swoole\Database::TYPE_COHOOKMYSQL; | ||
| 11 | + if (!empty($config['passwd'])) | ||
| 12 | + { | ||
| 13 | + $config['password'] = $config['passwd']; | ||
| 14 | + unset($config['passwd']); | ||
| 15 | + } | ||
| 16 | + if (!empty($config['name'])) | ||
| 17 | + { | ||
| 18 | + $config['database'] = $config['name']; | ||
| 19 | + unset($config['name']); | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + //用于隔离多实例 | ||
| 23 | + $config['object_id'] = $php->factory_key; | ||
| 24 | + | ||
| 25 | + $db = new Swoole\Database($config); | ||
| 26 | + | ||
| 27 | + return $db; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,24 @@ | |||
| 1 | + <?php | ||
| 2 | + global $php; | ||
| 3 | + | ||
| 4 | + $config = $php->config['redis'][$php->factory_key]; | ||
| 5 | + if (empty($config) or empty($config['host'])) | ||
| 6 | + { | ||
| 7 | + throw new Exception("require redis[$php->factory_key] config."); | ||
| 8 | + } | ||
| 9 | + | ||
| 10 | + if (empty($config['port'])) | ||
| 11 | + { | ||
| 12 | + $config['port'] = 6379; | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + if (empty($config['timeout'])) | ||
| 16 | + { | ||
| 17 | + $config['timeout'] = 0.5; | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + //用于隔离多实例 | ||
| 21 | + $config['object_id'] = $php->factory_key; | ||
| 22 | + | ||
| 23 | + $redis = new Swoole\Coroutine\Component\Hook\Redis($config); | ||
| 24 | + return $redis; | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments