Emcached is a key-value storage system that stores key data in the memory, greatly reducing the data access time. The advantage of using Memcached is self-evident. it not only reduces the pressure on the system to access the database, but also increases the system response speed. As for what systems need to use memcache, I suggest you access SyntaxHighlighter. emcached as a key-value storage system, which stores key data in the memory, greatly reducing the time to access data. The advantage of using Memcached is self-evident. it not only reduces the pressure on the system to access the database, but also increases the system response speed. As for what systems need to use memcache, I suggest using Memcached as the cache intermediate layer because of large data volumes and frequent accesses.
The disadvantage of using Memcached is not only to increase the amount of code in the program, but also to ensure the real-time performance of the database. In addition, it takes additional time to initialize the database for the first time. However, these shortcomings are insignificant compared to their advantages.
PHP has an extension to operate on Memcached. It provides a set of functions to operate on the Memcached server. they are simply divided into the following classes:
1. Memcached connection functions (connect, pconnect)
2. memcahced operation functions (set, get, delete, replace, flush)
3. multi-server configuration function (addServer)
4. status monitoring function (getStats .....)
The following code is used to give you an intuitive impression: (assume that Memcached is installed on 172.10.10.10, and the port number is 12121)
$ Memcache = new Memcached ();
$ Memcahce-> connect (172.10.10.10, 12121 );
$ Memcache-> set (Key, Value );
$ Memcache-> get (Key );
The above describes the simple operation process of Memcached: connect to the Memcached server, set the Value, and set the Value (the Key Value is Value );
Here we need to explain the addServer function and connect function. addServer puts multiple servers in the connection pool, while connect only connects to a server. if addServer is used, connect is used, only one server is used here.
After introducing the use of Memcached, I will introduce how to write your own PHP Memcached client.
Memcached is a server program. we can use the socket program in PHP to connect and communicate with each other to complete data storage operations. To use PHP to communicate with Memcached, you first need to know the Memcached communication protocol. the relevant information can be found in the Memcached source code doc/protocol.txt.
Here I will use the get command to show you this process.
This command is used to extract data from the data. the input format is get key.
If the server does not have this value, return: END
If this VALUE exists, return: VALUE key <标记> <数据长度> Data Block
The following code is a simulated client operation:
$ Fp = fsocketopen (maid, 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, use fsocketopen to open the socket communication interface of the server, then send the get key command to it, then obtain the returned data, and parse the returned data. There is no exception handling here, and it should be filled in during programming.
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.