PHP achieves shared high-speed memory data pool (non-MYSQL database)-application similar to asp

Source: Internet
Author: User
Tags crc32 zts

Why is this shared memory pool implemented?
Because every PHP request, the server calls PHP once. the EXE program parses the PHP file. then the program will be closed. therefore, there is no shared memory mechanism. friends who know asp should know that application can be used for shared memory cache in asp, but not in php. So we will study to make php have this capability.
What is the purpose of this shared memory pool?
Webgame is the simplest function to chat with. This is the data that all users need to share. The general processing method is to write text or database. This is very inefficient because of the PHP program, 80% of the time is waiting for the database to return data. For chat programs with relatively high real-time requirements, it is impossible to occupy the MYSQL database time. The data does not need to be stored permanently, however, the cache is required, so the high-speed shared memory function comes out.

Php has two sets of functions that use shared memory, one is the encapsulation of the System v ipc function, and the other is shmop.
Both do not need to install external library files.
The former can only be used in linux, and you need to add the-enable-sysvshm option when installing php;
The latter can be used in both linux and windows (not supported by win98 after win2k), but in windows, it can work normally only when php is in ISAPI running mode, add-enable-shmop when installing php.

The use of these two functions is quite simple. The following describes the simple usage. For more information, see the php manual.

System V shared memory usage:

$ Key = 12345; // The key of the shared memory
$ Memsize = 100; // size of the shared memory, in bytes
$ Perm = 0666; // shared memory access permission. For details, refer to linux permissions.
$ Var_key = 345; // key of a variable in shared memory
$ Shmid = shm_attach ($ key, $ memsize, $ perm); // create a shared memory
Shm_put_var ($ shmid, $ var_key, "abc"); // insert a shared memory variable. The key is $ var_key and the value is "abc"
Shm_detach ($ shmid); // disable shared memory

Run the above php program to create a shared memory with a key of 12345 and a size of 100 bytes, which contains a variable with a value of "abc. In the linux Command Line, enter ipcs to check the shared memory we created:

-- Shared Memory Segments ---
Key shmid owner perms bytes nattch status
0x00003039 262144 daemon 666 100 0

0 × 00003039 is the hexadecimal form of 12345.
Let's write another php code that accesses the shared memory and deletes the shared memory:

$ Shmid = shm_attach (12345); // shared memory with 12345 access key
Echo shm_get_var ($ shmid, 345); // print out the variable with the key of 345 in the shared memory. abc is displayed here.
Shm_remove ($ shmid); // Delete the shared memory

When you run php, abc is displayed and the shared memory is deleted. Then, run ipcs to see that the shared memory does not exist.

Shared memory usage of shmop:

$ Key = 12345; // The key of the shared memory
$ Memsize = 100; // size of the shared memory, in bytes
$ Perm = 0666; // shared memory access permission. For details, refer to linux permissions.
$ Offset = 0; // the shared memory offset address. 0 indicates the starting address of the shared memory.
$ Shmid = shmop_open ($ key, "c", $ perm, $ memsize); // create a shared memory. The second parameter c indicates creation.
$ Shm_bytes_written = shmop_write ($ shm_id, "abc", 0); // write "abc" to the shared memory
Echo $ shm_bytes_written; // print the Data Length written to the shared memory. 3 is displayed here.
Shmop_close ($ shm_id); // close the shared memory.

Running this php will create a shared memory with a key of 12345 and a size of 100 bytes, which is written into the string "abc.
We are writing a php code to access this shared memory:

$ Shm_id = shmop_open (12345, "w", 0, 0); // open the shared memory with the key of 12345. The second parameter w indicates that the shared memory is opened in read/write mode, enable the existing shared memory. The third and fourth parameters must be 0.
$ Shm_data = shmop_read ($ shm_id, 0, 3); // read 3 bytes of data from the shared memory. The second parameter is the offset address, and 0 indicates the starting address of the shared memory.
Echo $ shm_data; // print the shared memory data returned by the previous function.
Shmop_delete ($ shm_id); // deletes the shared memory.

Running this php will print out abc and delete the original shared memory.

Summary:

These two functions are easy to use. The only advantage of shmop is that it can be used in windows. In linux, I strongly recommend using the shm _ * function, because that set of functions is quite convenient to insert, update, and read the variables in the shared memory, shmop needs to plan its own shared memory storage structure, which is a little more complicated to use.
In addition, in the above example, I directly use the number 12345 as the shared memory key. In fact, a more standard approach is to use the ftok function to convert a path to the ipc key. For more information, see the php manual.

The above two solutions are not ideal. The one that is easy to use only supports LINUX. Therefore, we use the third set of shared memory.

Another one is the distributed cache system Memcached [key-value memory cache system].
Let's take a look at the introduction below.

There are two ways to use Memcached in PHP: one is to install the memcache extension of PHP (in fact there is another memcached extension, which is encapsulated based on the popular libmemcached library ), the extension is written in c, which is highly efficient and needs to be installed on the server. The other method is to directly use the php-memcached-client class library of the client, but I haven't found an official website after searching for it online for a long time. So, install an extension. Suppose php is installed in the/home/admin/php Directory:

wget http://pecl.php.net/get/memcache-2.2.5.tgzgzip -d memcache-2.2.5.tgztar xvf memcache-2.2.5.tarcd memcache-2.2.5/home/admin/php/bin/phpizeConfiguring for:PHP Api Version:         20041225Zend Module Api No:      20060613Zend Extension Api No:   220060519./configure --enable-memcache --with-php-config=/home/admin/php/bin/php-config --with-zlib-dirInstalling shared extensions:     /home/admin/php/lib/php/extensions/no-debug-non-zts-20060613/

Note the information returned by the last line, and add the following two lines to/home/admin/php/lib/php. ini.

extension_dir = "/home/admin/php/lib/php/extensions/no-debug-non-zts-20060613/"extension=memcache.so

Restart the web server. If the installation is successful, you can use phpinfo () to obtain information about the extension:

Memcache support Enabled
Active persistent connections 0
Version 2.2.5
Revision $ Revision: 1.111 $
Directive Local Value Master Value
Memcache. allow_failover 1 1
Memcache. chunk_size 8192 8192
Memcache. default_port 11211 11211
Memcache. default_timeout_ms 1000 1000
Memcache. hash_function Crc32 Crc32
Memcache. hash_strategy Standard Standard
Memcache. max_failover_attempts 20 20

You can set the above parameters in php. ini. The following is a php test code on the official website:

<?php$memcache = new Memcache;$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");$version = $memcache->getVersion();echo "Server's version: ".$version."\n";$tmp_object = new stdClass;$tmp_object->str_attr = 'test';$tmp_object->int_attr = 123;$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");echo "Store data in the cache (data will expire in 10 seconds)\n";$get_result = $memcache->get('key');echo "Data from the cache:\n";var_dump($get_result);?>

The output after running is as follows:

Server's version: 1.2.6Store data in the cache (data will expire in 10 seconds) Data from the cache: object (stdClass) #3 (2) {[& quot; str_attr & quot;] => string (4) & quot; test & quot; [& quot; int_attr & quot;] = & quot; int (123 )}
Well, this thing can be used like a SESSION, which is exactly the high-speed shared memory pool we need. It is cross-platform.

Related Article

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.