Tutorial on creating memcached client cache _php PHP

Source: Internet
Author: User
Tags php memcached
Emcached is a key-value storage system that stores critical data in memory, greatly reducing the time it takes to access data. The benefit of using memcached is self-evident, it not only reduces the pressure of the system to access the database, but also improves the system response speed. As to what kind of system needs to use Memcache, the author suggests that the data volume is large and access to frequent systems can take memcached as the middle tier of the cache.

The disadvantage of using memcached in addition to increasing the code of the program, there is no guarantee of real-time database, and the first time the database will require additional time. But these shortcomings are negligible compared to the merits of it.

PHP has an extension of Operation Memcached, which provides a set of functions to manipulate the memcached server and simply split them into classes:
1.Memcached connection function (Connect, pconnect)
2.memcahced operation function (set, GET, delete, replace, flush)
3. Multi-server configuration function (Addserver)
4. Status monitoring function (GetStats ...)

Here's a snippet of code to give you an intuitive impression: (assuming memcached 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 flow of memcached: Connect memcached server, set value, value (value of key);

Here we need to explain the Addserver function and the Connect function, Addserver is to put multiple servers in the connection pool, and connect will only connect to one server, if you use the Addserver, and then use Connect, Only one server will be used here.

After introducing the use of memcached, the following shows you 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 the corresponding communication, complete data storage operations. To use PHP and memcached communication, first need to know the Memcached communication protocol, the relevant information can be found in memcached source code doc/protocol.txt.

Here I use the GET command to show you the process
This command mainly extracts data from the data, input format: 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 simulation of the 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];
}
Simply explain the above code, use Fsocketopen to open the server's socket interface, then send it a Get key command, then get the returned data and parse the returned data. There is no exception to the processing, in the programming time to fill in.

http://www.bkjia.com/PHPjc/486177.html www.bkjia.com true http://www.bkjia.com/PHPjc/486177.html techarticle emcached is a key-value storage system that stores critical data in memory, greatly reducing the time it takes to access data. The benefits of using memcached are self-evident, it not only reduces system access ...

  • 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.