Home > Backend Development > PHP Tutorial > The principle of easyswoole starting TableManager+Cache tool

The principle of easyswoole starting TableManager+Cache tool

little bottle
Release: 2023-04-06 11:52:01
forward
3097 people have browsed it

The main content of this article is to describe the easyswoole code to understand how to start TableManager and the principle of the Cache tool. It has certain reference value and interested friends must understand it.

EasySwoole is a memory-resident PHP framework developed based on Swoole Server. It is designed for APIs and gets rid of the performance loss caused by traditional PHP operating mode in process arousal and file loading. EasySwoole highly encapsulates Swoole Server while still maintaining the original features of Swoole Server. It supports simultaneous monitoring of HTTP, customized TCP, and UDP protocols, allowing developers to write multi-process, asynchronous, and highly available applications with the lowest learning cost and effort. Serve.

swoole_table is an ultra-high-performance, concurrent data structure based on shared memory and locks. Used to solve multi-process/multi-thread data sharing and synchronous locking problems.

TableManager mainly does the following things
add method
If there is this table name in the $list array ($name is a table name or collection name), initialize swoole_table, and then configure Create an array of field types

if(!isset($this->list[$name])){
    $table = new Table($size);
    foreach ($columns as $column => $item){
        $table->column($column,$item['type'],$item['size']);
    }
    $table->create();
    $this->list[$name] = $table;
}
Copy after login

get method
Directly returns an instance of swoole_table.

There are many places to use it
As mentioned above, when the system sets the Cache component Cache::getInstance()

the construction method does the following things

$num = intval(Config::getInstance()->getConf("EASY_CACHE.PROCESS_NUM"));//Config默认配置是1,如果配置为小于等于0则不开启Cache
if($num <= 0){
   return;
}
$this->cliTemp = new SplArray();
//若是在主服务创建,而非单元测试调用
if(ServerManager::getInstance()->getServer()){
    //创建table用于数据传递
    TableManager::getInstance()->add(self::EXCHANGE_TABLE_NAME,[
        &#39;data&#39;=>[
            &#39;type&#39;=>Table::TYPE_STRING,
            &#39;size&#39;=>10*1024
        ],
        &#39;microTime&#39;=>[
            &#39;type&#39;=>Table::TYPE_STRING,
            &#39;size&#39;=>15
        ]
    ],2048);
    //创建了一个__Cache的swoole_table表,字段为 data String 10240,microTime String 15的表
    $this->processNum = $num;
    for ($i=0;$i < $num;$i++){
        ProcessManager::getInstance()->addProcess($this->generateProcessName($i),CacheProcess::class);
    }
}
Copy after login

ProcessManager is also a very important concept. In fact, it is a tool for managing task mapping.

Here you can see ProcessManager::getInstance()->addProcess($this->generateProcessName($i),CacheProcess::class)

In fact, here is passed ProcessManager allows the swoole service to add a process. swoole's addProcess method, document link https://wiki.swoole.com/wiki/page/390.html

Let's briefly explain the set method of Cache in advance to deepen the concept.

//讲解一下Cache的set方法加深概念
if(!ServerManager::getInstance()->isStart()){//兼容测试模式。也就是不开启服务的情景下直接是clitemp中取缓存数据
    $this->cliTemp->set($key,$data);
}
if(ServerManager::getInstance()->getServer()){
    $num = $this->keyToProcessNum($key);//这里是通过key然后hash到应该投放的Cache进程中去。
    $msg = new Msg();
    $msg->setCommand(&#39;set&#39;);
    $msg->setArg(&#39;key&#39;,$key);
    $msg->setData($data);
    //下面一句话还是挺复杂的,根据key名hash到ProcessManager对应的映射,然后获取到swoole_process的实例,以swoole的write函数向管道内写入数据。
    ProcessManager::getInstance()->getProcessByName($this->generateProcessName($num))->getProcess()->write(\swoole_serialize::pack($msg));
    //在写完数据后,在CacheProcess的onReceive方法中可以看到对应setCommand的操作细节。其实数据都被写到了一个Arr数组中。下篇接着讲一下Cache的实现细节。这节还是主要讲TableManager和它的相关作用.
}
Copy after login

Related tutorials: PHP video tutorial

The above is the detailed content of The principle of easyswoole starting TableManager+Cache tool. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template