


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)
1 2 3 4 5 6 7 8 9 10 |
|
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命令查看一下地址和占用大小
1 |
|
1 2 3 4 5 6 7 |
|
命令说明
key :共享内存的唯一的key值,共享内存通过该key来判断你读取的是哪一块内存。
shmid:当使用key来获取内存时,你获得的是这个id的值。它作为你操作内存块的标识。
owner:创建该共享内存块的用户
perms:该共享内存的读写权限,8禁止,可以是777,与文件的读写权限一致。
bytes:该内存块的大小
nattch:连接该内存块的进程数
status:当前状态,如:dest,即将删除等。
项目实际应用小案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
当然你要考虑的是,如果数据更新的话,那么内存段也要删除,并且更新数据......................通过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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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



PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.
