diff --git a/src/RpcJsonp.php b/src/RpcJsonp.php index 30f956a..4737b9f 100644 --- a/src/RpcJsonp.php +++ b/src/RpcJsonp.php @@ -113,6 +113,7 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa */ public function register(OnServerBeforeStart $server) { + $this->manager->tableInit(); $this->manager->register(); } diff --git a/src/RpcManager.php b/src/RpcManager.php index 2a48035..e31e845 100644 --- a/src/RpcManager.php +++ b/src/RpcManager.php @@ -2,10 +2,12 @@ namespace Kiri\Rpc; +use Kiri\Abstracts\Config; use Kiri\Consul\Agent; use Kiri\Consul\Health; use Kiri\Kiri; use ReflectionException; +use Swoole\Table; class RpcManager { @@ -17,6 +19,17 @@ class RpcManager private array $_rpc = []; + private Table $table; + + + public function tableInit() + { + $this->table = new Table((int)Config::get('rpc.total', 10)); + $this->table->column('content', Table::TYPE_STRING); + $this->table->create(); + } + + /** * @param $serviceName * @return void @@ -30,12 +43,10 @@ class RpcManager return; } $body = json_decode($lists->getBody(), true); - - $file = storage('.rpc.clients.' . md5($serviceName), 'rpc'); if (!empty($body) && is_array($body)) { - file_put_contents($file, json_encode(array_column($body, 'Service'))); + $this->table->set($serviceName, ['content' => json_encode(array_column($body, 'Service'))]); } else { - file_put_contents($file, json_encode([])); + $this->table->set($serviceName, ['content' => json_encode([])]); } } @@ -59,10 +70,10 @@ class RpcManager public function getServices($serviceName): array { $file = storage('.rpc.clients.' . md5($serviceName), 'rpc'); - if (!file_exists($file)) { + if (!$this->table->exist($file)) { return []; } - $content = json_decode(file_get_contents($file), true); + $content = json_decode($this->table->get($serviceName), true); if (empty($content) || !is_array($content)) { return []; }