Home >Backend Development >PHP Tutorial >Detailed explanation of memcache cache examples using PHP_PHP tutorial
A detailed explanation of an example of memcache caching using PHP. This article uses many available extensions that we may have rarely used before. The same is true for shared memory in PHP. Using some shared functions, developers can easily manipulate memory segments without installing any extensions.
Overview
Shared memory is an efficient way to exchange data between applications on the same machine. A process can create a memory segment that can be accessed by other processes, as long as it is assigned the correct permissions. Each memory segment has a unique ID (called a shmid) that points to an area of physical memory where other processes can operate on it. Once created and given the appropriate permissions, other processes on the same machine can operate on these memory segments: read, write, and delete.
This means that applications written in C can share information with applications written in other languages, such as Java™ or PHP. They can all share information as long as they have access to and understanding of that information. Shared memory is widely used in implementations for most languages, so access should not be an issue. To understand the information, we can use a standard format such as XML or JSON.
The use of shared memory is a fast way to exchange data between processes, mainly because the data is passed after the memory segment is created, without involving the kernel. This method is often called inter-process communication (IPC). Other IPC methods include pipes, message queues, RPC, and sockets. This ability to quickly and reliably exchange data between applications is useful when working with an ecosystem of applications that need to communicate with each other. Depending on the size of the ecosystem, the common method of using a database to exchange information between applications can often result in slow queries or even I/O blocking. With shared memory, there is no I/O to slow down developer progress.
The proposal of this article is very simple, learn how to use PHP to create and manipulate shared memory segments and use them to store data sets that can be used by other applications. Even if there are no plans to use shared memory to exchange data, it has many benefits in itself because it enables applications to stay away from I/O problems. Storing datasets directly in memory has many advantages, from web service data caching to session sharing. It is a very useful concept that every PHP developer should know.
Shared Memory and PHP
PHP has a rich set of extensions available, as does shared memory. Using some shared functions, developers can easily manipulate memory segments without installing any extensions.
Back to top
Create memory segment
Shared memory functions are similar to file operation functions, but instead of handling a stream, you will handle a shared memory access ID. The first example is the shmop_open function, which allows you to open an existing memory segment or create a new memory segment. This function is very similar to the classic fopen function, which opens a stream for file operations and returns a resource for use by other functions wishing to read from or write to the opened stream. Let's look at shmop_open in Listing 1.
Listing 1. shmop_open function
The code is as follows | Copy code | ||||
|
The first thing that appears in this function is the 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
• Mode “w”, which allows you to access read-write memory segments
• Mode "c", which creates a new memory segment, or if the memory segment already exists, attempts to open it for reading and writing
• Mode "n", which creates a new memory segment and fails if the memory segment already exists
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. Before writing to a memory segment, you must allocate the appropriate number of bytes above it.
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.
Back to top
Write data to memory segment
Use the shmop_write function to write data to a shared memory block. This function is simple to use and accepts only 3 parameters, as shown in Listing 2.
Listing 2. Using shmop_write to write data to a shared memory block
The code is as follows | Copy code | ||||
shmop_write($shmid, "Hello World!", 0); ?> |
This function is similar to the fwrite function, which takes two parameters: the open stream resource (returned by fopen) and the data you wish to write. The shmop_write function also performs this task.
Back to top
代码如下 | 复制代码 |
$stream = fopen('file.txt', 'r+'); ?> |
Read data from memory segment
Reading data from a shared memory segment is simple. All you need is an open memory segment and the shmop_read function. This function accepts some parameters and works like fread. See Listing 3 to read the contents of a PHP file.Listing 3. Using shmop_read to read the contents of a file
代码如下 | 复制代码 |
$shmid = shmop_open(864, 'c', 0755, 1024); ?> |
The code is as follows | Copy code |
<🎜>$stream = fopen('file.txt', 'r+');<🎜> fwrite($stream, "Hello World!");<🎜> echo fread($stream, 11);<🎜> <🎜>?> |
The code is as follows | Copy code |
<🎜>$shmid = shmop_open(864, 'c', 0755, 1024);<🎜> shmop_write($shmid, "Hello World!", 0);<🎜> echo shmop_read($shmid, 0, 11);<🎜> <🎜>?> |
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.
Fortunately, when using shared memory segments, the shmop_size function returns the size of a memory segment in bytes, similar to the filesize function. See Listing 5.
Listing 5. The shmop_size function returns the memory segment size in bytes
The code is as follows | Copy code | ||||||||||||||||||||||||||||||||||||||||||||
shmop_write($shmid, "Hello World!", 0); echo shmop_read($shmid, 0, $size);
Back to top Delete memory segment
We learned how to open, write, and read shared memory segments. To complete our CRUD class, we also need to learn how to delete a memory segment. This task can be easily accomplished using the shmop_delete function, which accepts only one parameter: the ID of the shared memory we wish to delete.
|