The principle of easyswoole starting TableManager+Cache tool
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; }
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,[ 'data'=>[ 'type'=>Table::TYPE_STRING, 'size'=>10*1024 ], 'microTime'=>[ 'type'=>Table::TYPE_STRING, 'size'=>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); } }
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('set'); $msg->setArg('key',$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和它的相关作用. }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The thing is actually like this. At that time, my leader gave me a perf hardware performance monitoring task. During the process of using perf, I entered the command perf list and I saw the following information: My task is to enable these cache events to be counted normally. But the point is, I have no idea what these misses and loads mean.

Using the cache can increase the speed of the computer because the cache shortens the waiting time of the CPU. Cache is a small but high-speed memory located between the CPU and the main memory DRAM. The function of Cache is to increase the rate of CPU data input and output; Cache has a small capacity but fast speed, while the memory speed is low but has a large capacity. By optimizing the scheduling algorithm, the performance of the system will be greatly improved.

Cache is called cache memory. It is a high-speed small-capacity memory between the central processing unit and the main memory. It is generally composed of high-speed SRAM. This kind of local memory is oriented to the CPU. It is introduced to reduce or eliminate the gap between the CPU and the memory. The impact of the speed difference between them on system performance. Cache capacity is small but fast, memory speed is low but capacity is large. By optimizing the scheduling algorithm, the performance of the system will be greatly improved.

Here is the tutorial for nginx reverse proxy caching: Install nginx: sudoaptupdatesudoaptinstallnginx Configure reverse proxy: Open nginx configuration file: sudonano/etc/nginx/nginx.conf Add the following configuration in the http block to enable caching: http{...proxy_cache_path /var/cache/nginxlevels=1:2keys_zone=my_cache:10mmax_size=10ginactive=60muse_temp_path=off;proxy_cache

Characteristics of cache: A one- or two-level high-speed, small-capacity memory set between the CPU and the main memory. The information is naturally lost when the computer is powered off. Characteristics of ROM: it can only read data from the memory, but cannot write information into it. The data will still exist after the computer is powered off. Characteristics of ram: it can read data from the memory and write information to the memory; it is used to store commands, programs and data required to run the program; information is naturally lost when the computer is powered off.

Preface Caching can effectively improve system performance and stability by storing frequently accessed data in memory, reducing the pressure on underlying data sources such as databases. I think everyone has used it more or less in their projects, and our project is no exception. However, when I was reviewing the company's code recently, the writing was very stupid and low. The rough writing is as follows: publicUsergetById(Stringid){Useruser=cache. getUser();if(user!=null){returnuser;}//Get user from the database=loadFromDB(id);cahce.put(id,user);returnu

5 options for nginx caching cache 1. One of the traditional caches (404) This method is to direct the 404 error of nginx to the backend, and then use proxy_store to save the page returned by the backend. Configuration: location/{root/home/html/;#Home directory expires1d;#Expiration time of the web page error_page404=200/fetch$request_uri;#404 directed to the /fetch directory} Location/fetch/{#404 directed here internal ;#Indicates that this directory cannot be directly accessed externally

The details are as follows: 1. Let’s talk about what is hard-coded cache? Before learning SpringCache, I often used caching in a hard-coded way. Let's take a practical example. In order to improve the query efficiency of user information, we use caching for user information. The sample code is as follows: @AutowireprivateUserMapperuserMapper; @AutowireprivateRedisCacheredisCache;//Query users publicUsergetUserById(LonguserId){//Define cache keyStringcacheKey= "userId_
