


PHP simply uses the shmop function to create shared memory to reduce server load
This article mainly introduces how PHP simply uses the shmop function to create shared memory to reduce server load. It has a certain reference value. Now I share it with you. Friends in need can refer to it
In the previous article This blog [Understand the concept, advantages and disadvantages of shared memory] has already explained the concept of shared memory. Let's simply use shared memory (in fact, you can also use other tools, such as redis)
PHP has two sets of interfaces for memory sharing. One is shm, which is actually variable sharing and will serialize object variables before storing them. It is quite convenient to use, but serialized storage is meaningless for memory access operations that prioritize efficiency. The other one is shmop, which is common to Linux and Windows, but its function is weaker than shm. On Linux, these functions are directly implemented by calling the shm* series of functions, while on Winodows, they are also implemented by encapsulating system functions. made the same call. What I use here first is shmop.
To create a shared memory segment, you need to use the function shmop, then you need to enable the extension. You can refer to [Enable shmop extension for PHP to implement shared memory].
shmop main function
shmop_open (create or open a shared memory block), shmop_write (write data to a shared memory block), shmop_read (read data from a shared memory block), shmop_size (get the size of a shared memory block), shmop_close (close shared memory block), shmop_delete (delete shared memory block)
<?php //创建一块共享内存 $shm_key = 0x4337b101; $shm_id = @shmop_open($shm_key, 'c', 0644, 1024); //读取并写入数据 $data = shmop_read($shm_id, 0, 1024); shmop_write($shm_id, json_encode($data), 0); $size = shmop_size($shm_id); //获取内存中实际数据占用大小 //关闭内存块,并不会删除共享内存,只是清除 PHP 的资源 shmop_close($shm_id);
shmop_open (create memory segment)
The first thing that appears in this function is System ID parameter. This is a number that identifies the shared memory segment in the system. The second parameter is the access mode, which is very similar to the access mode of the fopen function. You can access a memory segment in 4 different modes:
Mode "a", which allows you to access read-only memory segments, read-only access
Mode "w", which allows you to access read-only memory segments Memory segment for reading and writing, read and write
Mode "c", it creates a new memory segment, or if the memory segment already exists, try to open it for reading and writing
Mode "n", it creates a new memory If the segment with the same key already exists, the creation will fail. This is for the sake of safe use of shared memory.
The third parameter is the permissions of the memory segment. You must provide an octal value here.
The fourth parameter provides the memory segment size in bytes. Since the shared memory segment used is of fixed length, the length of the data must be calculated when storing and reading, otherwise the writing may fail or a null value may be read. .
Please note that this function returns an ID number that can be used by other functions to operate on this shared memory segment. This ID is the shared memory access ID, unlike the system ID, which is passed as a parameter. Be careful not to confuse the two. On failure, shmop_open will return FALSE. When creating a memory block, it is recommended to use constants instead of variables for the key parameter, otherwise it is likely to cause memory leaks.
shmop_write (write data to the memory segment)
This function is similar to the fwrite function, which has two parameters: the open stream resource (returned by fopen) and the data you wish to write. The shmop_write function also performs this task.
The first parameter is the ID returned by shmop_open, which identifies the shared memory block you are operating on. The second parameter is the data you wish to store, and the final third parameter is the position where you wish to start writing. By default, we always use 0 to indicate where to start writing. Note that this function returns FALSE on failure and the number of bytes written on success.
shmop_read(Read data from memory segment)
Reading data from shared memory segment is very simple. All you need is an open memory segment and the shmop_read function. This function accepts some parameters and works like fread.
Please pay attention to the parameters here. The shmop_read function will accept the ID returned by shmop_open, which we already know, but it also accepts two other parameters. The second argument is the location in the memory segment you wish to read from, and the third is the number of bytes you wish to read. The second parameter can always be 0, indicating the beginning of the data, but the third parameter can be problematic because we don't know how many bytes we want to read.
This is very similar to what we do in the fread function, which accepts two parameters: the open stream resource (returned by fopen) and the number of bytes you wish to read from the stream. Use the filesize function (which returns the number of bytes in a file) to read it completely.
shmop_size (returns the actual size of the memory segment data)
For example, we have opened a memory space with a length of 100 bytes, but the actual length of the stored data is only 90, then the value returned by using shmop_size is 90.
shmop_delete (delete memory segment)
该函数仅接受一个参数:我们希望删除的共享内存 ID,这不会实际删除该内存段。它将该内存段标记为删除,因为共享内存段在有其他进程正在使用它时无法被删除。shmop_delete 函数将该内存段标记为删除,阻止任何其他进程打开它。要删除它,我们需要关闭该内存段。在创建内存块时建议key参数用常量而不用变量,否则很有可能造成内存泄露。
shmop_close(关闭内存段)
我们在对内存段进行读取和写入,但完成操作后,我们必须从它解除,这非常类似于处理文件时的 fclose 函数。打开包含一个文件的流并在其中读取或写入数据后,我们必须关闭它,否则将发生锁定。
简单测试结果查看
我是在LNMP环境下操作的,如果你也和我一样,在执行完简单的操作之后,可以使用linux命令查看一下地址和占用大小
# ipcs -m
[root@bogon ~]# ipcs -m ------ Shared Memory Segments -------- key shmid owner perms bytes nattch status 0x00000000 0 gdm 600 393216 2 dest 0x00000000 32769 gdm 600 393216 2 dest 0x4337b101 884750 nobody 644 1024 0
命令说明
key :共享内存的唯一的key值,共享内存通过该key来判断你读取的是哪一块内存。
shmid:当使用key来获取内存时,你获得的是这个id的值。它作为你操作内存块的标识。
owner:创建该共享内存块的用户
perms:该共享内存的读写权限,8禁止,可以是777,与文件的读写权限一致。
bytes:该内存块的大小
nattch:连接该内存块的进程数
status:当前状态,如:dest,即将删除等。
项目实际应用小案例
<?php /** * 将领技能 */ class Generalskill_model extends CI_Model { private $_memory_key = 0x4337b001; //共享内存地址key private $_memory_size = 1048576; //开辟共享内存大小 //最好根据实际数据长度大小定义。 public function __construct() { parent::__construct(); } public function get_skill_list() { $data = []; $shmid = @shmop_open($this->_memory_key, 'a', 0644, $this->_memory_size); if ($shmid === FALSE) { $shmid = @shmop_open($this->_memory_key, 'c', 0644, $this->_memory_size); $data = $this->return_skill_list(); shmop_write($shmid, json_encode($data), 0); @shmop_close($shmid); return $data; } $data = json_decode(preg_replace('/[\x00-\x1F\x80-\x9F]/u', '', trim(shmop_read($shmid, 0, $this->_memory_size))), true); @shmop_close($shmid); return $data; } public function return_skill_list() { //这里是一个超大的数组,其实就是把这个数组json化,然后存入共享内存段。 其实可以用redis等其他缓存...这里我就是为了不用redis等其他nosql才用的shmop return array ( 1 => array ('id' => '1','animation' => '13','skill_type' => '1','power_type' => '1','site' => '1','type' => '1','paramete' => '0','paramete2' => '0','paramete3' => '0','chance' => '0','ratio' => '1', ), 2 => array ('id' => '2','animation' => '3','skill_type' => '2','power_type' => '1','site' => '1','type' => '1','paramete' => '0','paramete2' => '0','paramete3' => '0','chance' => '0','ratio' => '2', ),..........................................
当然你要考虑的是,如果数据更新的话,那么内存段也要删除,并且更新数据......................通过shmop_delete可以删除 。这就需要你们自己根据项目应用来考虑了
还有就是我这里只是为了简单的读,并没有出现复杂的读写,否则可能会出现进程互斥等意想不到的冲突~如果复杂,那么就可以考虑信号量了~
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
The above is the detailed content of PHP simply uses the shmop function to create shared memory to reduce server load. For more information, please follow other related articles on the PHP Chinese website!

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),