PHP simply uses the shmop function to create shared memory to reduce server load

不言
Release: 2023-04-02 17:38:01
Original
3198 people have browsed it

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, &#39;c&#39;, 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);
Copy after login

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
Copy after login
[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
Copy after login

命令说明

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, &#39;a&#39;, 0644, $this->_memory_size);  

    if ($shmid === FALSE) {
        $shmid = @shmop_open($this->_memory_key, &#39;c&#39;, 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(&#39;/[\x00-\x1F\x80-\x9F]/u&#39;, &#39;&#39;, 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 (&#39;id&#39; => &#39;1&#39;,&#39;animation&#39; => &#39;13&#39;,&#39;skill_type&#39; => &#39;1&#39;,&#39;power_type&#39; => &#39;1&#39;,&#39;site&#39; => &#39;1&#39;,&#39;type&#39; => &#39;1&#39;,&#39;paramete&#39; => &#39;0&#39;,&#39;paramete2&#39; => &#39;0&#39;,&#39;paramete3&#39; => &#39;0&#39;,&#39;chance&#39; => &#39;0&#39;,&#39;ratio&#39; => &#39;1&#39;,
  ),
  2 => 
  array (&#39;id&#39; => &#39;2&#39;,&#39;animation&#39; => &#39;3&#39;,&#39;skill_type&#39; => &#39;2&#39;,&#39;power_type&#39; => &#39;1&#39;,&#39;site&#39; => &#39;1&#39;,&#39;type&#39; => &#39;1&#39;,&#39;paramete&#39; => &#39;0&#39;,&#39;paramete2&#39; => &#39;0&#39;,&#39;paramete3&#39; => &#39;0&#39;,&#39;chance&#39; => &#39;0&#39;,&#39;ratio&#39; => &#39;2&#39;,
  ),..........................................
Copy after login

当然你要考虑的是,如果数据更新的话,那么内存段也要删除,并且更新数据......................通过shmop_delete可以删除 。这就需要你们自己根据项目应用来考虑了

还有就是我这里只是为了简单的读,并没有出现复杂的读写,否则可能会出现进程互斥等意想不到的冲突~如果复杂,那么就可以考虑信号量了~

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

php操作共享内存shmop类及简单使用测试的代码

php实现共享内存进程通信函数(_shm)

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!