Home >Backend Development >PHP Tutorial >Explain how to create Memcached client cache in PHP_PHP tutorial

Explain how to create Memcached client cache in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:40:46942browse

emcached is a key-value storage system that stores key data in memory, greatly reducing the time to access data. The benefits of using Memcached are self-evident. It not only reduces the pressure on the system to access the database, but also improves the system's response speed. As for what kind of systems need to use memcache, the author recommends that systems with large amounts of data and frequent access can use Memcached as the middle layer of the cache.

The disadvantages of using Memcached are not only the increased code size of the program, but also the fact that it cannot guarantee the real-time performance of the database. In addition, it will take extra time to initialize the database for the first time. But these disadvantages are very insignificant compared to its advantages.

PHP has an extension for operating Memcached. It provides a set of functions to operate the Memcached server. They are simply classified into categories:
1. Memcached connection functions (connect, pconnect)
2. memcahced operation function (set, get, delete, replace, flush)
3. Multi-server configuration function (addServer)
4. Status monitoring function (getStats....)

below Extract a piece of code to give you an intuitive impression: (assuming Memcached is installed on 172.10.10.10, port number 12121)
$memcache = new Memcached();
$memcahce->connect(172.10.10.10, 12121);
$memcache->set(Key, Value);
$memcache->get(Key);
The above mainly completes the simple operation process of Memcached: connect to the Memcached server, set Value, value (the value of Key is Value);

Here we need to explain the addServer function and the connect function. addServer puts multiple servers in the connection pool, while connect only connects to a certain server. ; If you use addServer and then connect, only one server will be used here.

After introducing the use of Memcached, let me introduce you to how to write your own PHP Memcached client.

Memcached is a server-side program. We can naturally use the socket program in PHP to connect and communicate accordingly to complete the data storage operation. To use PHP to communicate with Memcached, you first need to know the communication protocol of Memcached. The relevant information can be found in doc/protocol.txt of the Memcached source code.

Here I use the get command to show you the process
This command mainly extracts data from the data. The input format is: get key
If the server does not have this value, it returns: END
If this value exists, return: VALUE key Data block

The following code is a simple simulated client operation
$fp = fsocketopen(172.10.10.10, 12121, $errorno, $errstr, 1);
if(!$fp)
echo "$errstr";
else
{
$out = "get key ";
fwrite($out);
while(!feof($fp))
$str . = fgets($fp);
if(stripos($str, END) ===0)
exit("NO value find")
$arr = implode( , $str);
echo $arr[1];
}
Briefly explain the above code and use fsocketopen to open the server socket communication interface, and then send the get key command to it, then obtain the returned data, and parse the returned data. There is no exception handling here, it needs to be filled in during programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486177.htmlTechArticleemcached is a key-value storage system that stores key data in memory, greatly reducing the time to access data. . The benefits of using Memcached are self-evident. It not only reduces system access...
Statement:
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