PHP Shared memory segment implementation, using the SHMOP function for memory sharing

Source: Internet
Author: User

The so-called memory sharing: is a common memory segment of multiple applications, in a program can get the B program stored in this memory segment of the data, that is, data sharing.


Shmop is a set of functions provided by PHP, and after using-ENABLE-SHMOP in the configuration of PHP, you can use the functions provided by this function set.


Shmop is more friendly to Linux, but not for windows, it's not supported for CGI and CLI mode ~ a little awkward.


To say some of the functions he provides:

Ftok function: Generates a number or a key or keys. $key = Ftok (fileName, mode); In general, this filename can be directly used __file__ to point to the current file, mode is more flexible, is a string type, such as: "A", "B", "P"


Shmop_open ($key, $mode, $permission, $size); Open a memory segment

The key here is the key generated by Ftok, $mode is the mode of opening this memory segment. Including:

A: It allows access to read-only memory segments

W: It allows access to read-write memory segments

C: Create a new memory segment, if it already exists, try to read and write

N: Creates a new memory segment that fails if it already exists

$size refers to the size of the stated memory segment, in bytes,

Shmop_open returns an ID that is the memory segment ID that can ftok the generated key is not the same thing. Attention!


Shmop_write ($id, $stringData, $offset);

Write string data to this memory segment of ID, starting with $offset, offset is usually set to 0, note that this store can only be a string type. Note the conversion.


Shmop_read ($id, $offset, $limit);

Reads the data, starting from $offset to read $limit characters.


Shmop_size ($id);

Gets the total number of bytes stored.


Shmop_delete ($id)

Empty the contents of this memory segment


Shmop_close ($id)

Close this memory side, also called memory recycling ~


Well, the basic methods are introduced, we should all have tried, we should have these content in a package, so I encapsulated it, made a class, but also convenient for later expansion

GitHub Address:

Https://github.com/toulen/Tshmop


Composer Installation:

Composer require toulen/tshmop:1.0

This article is from the "Follow the Tao program" blog, please be sure to keep this source http://toulen.blog.51cto.com/12020200/1963404

PHP Shared memory segment implementation, using the SHMOP function for memory sharing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.