PHP memcache and memcached Module installation Application _php Tutorial

Source: Internet
Author: User
Tags crc32
Memcache's official homepage: PHP tutorial. Net/package/memcache ">http://pecl.php.net/package/memcache
Official homepage of memcached: http://pecl.php.net/package/memcached





The following is a procedure record of the PHP module that I installed memcached version of:


Memcached php module

wget http://download.tangent.org/libmemcached-0.48.tar.gz
Tar zxf libmemcached-0.48.tar.gz
CD libmemcached-0.48
./configure--prefix=/usr/local/libmemcached--with-memcached
Make
Make install

Php memcached module 


wget http://pecl.php.net/get/memcached-1.0.2.tgz
Tar zxf memcached-1.0.2.tgz
CD memcached-1.0.2
/usr/local/webserver/php/bin/phpize
./configure--enable-memcached--with-php-config=/usr/local/webserver/php/bin/php-config--with-libmemcached-dir= /usr/local/libmemcached
Make
Make install



Join in the php.ini
Extension=memcached.so
Complete



Other:
When installing libmemcached, if you use only./configure, you may be prompted:
Checking for memcached ... no
Configure:error: "Could not find memcached binary"




Installing the memcached version of the PHP module



wget http://download.tangent.org/libmemcached-0.35.tar.gz
Tar zxf libmemcached-0.35.tar.gz
CD libmemcached-0.35
./configure
Make
Make install



wget http://pecl.php.net/get/memcached-1.0.0.tgz
Tar zxf memcached-1.0.0.tgz
CD memcached-1.0.0
Phpize
./configure
Make
Make install



Open php.ini Plus:



Extension = "memcached.so"



This completes the installation, which you can confirm with the following commands:



php-m | grep mem



New features for demo version memcached



First, make a fictitious question, assuming that the counter initial value is an integer, do not use the Increment method, complete each add one by Get/set.



In the Memcache version, we can only do it in the following ways:



$m = new Memcache ();
$m->addserver (' localhost ', 11211);
$v = $m->get (' counter ');
$m->set (' counter ', $v + 1);



Since get/set these two actions cannot be manipulated as an atom, when multiple processes are processed at the same time, there is a possibility of loss, and what is more annoying is that you do not know when the loss occurs.



And look at how we did it in the memcached version:



$MD = new Memcached ();
$MD->addserver (' localhost ', 11211);
$v = $md->get (' counter ', NULL, $token)
$MD->cas ($token, ' counter ', $v + 1);



CAS is provided in the memcached version of the function, White is an optimistic lock function, if you put $token value var_dump out, you will find that $token is actually a version number, if get the $token version number in the CAS does not correspond, This means that there are other operations updated, CAS operation will fail, as to how to proceed, it depends on your own.



Note: If you want to manually reproduce the conflict, you can sleep a few seconds between get and CAs, and copy two scripts and execute them successively.



By the way, the recommended hash setting for the Memcached version module is as follows:



$MD->setoption (memcached::opt_distribution, Memcached::D istribution_consistent);
$MD->setoption (Memcached::opt_hash, MEMCACHED::HASH_CRC);







They are almost identical in use.
Copy the code code as follows:
$mem = new Memcache;
$mem->addserver ($memcachehost, ' 11211 ');
$mem->addserver ($memcachehost, ' 11212 ');
$mem->set (' HX ', ' 9enjoy ');
echo $mem->get (' HX ');



Copy the code code as follows:
$MD = new Memcached;
$servers = Array (
Array ($memcachehost, ' 11211 '),
Array ($memcachehost, ' 11212 ')
);
$MD->addservers ($servers);
$MD->set (' HX ', ' 9enjoy ');
echo $md->get (' HX ');



memcached more than memcache, such as getmulti,getbykey,addservers and so on.
Memcached does not have a Memcache connect method and currently does not support long connections.
Memcached supports Binary Protocol, and Memcache does not support it, which means that memcached will have higher performance.
Memcache is a native implementation, support OO and non-oo two sets of interfaces coexist, memcached is the use of libmemcached, only support OO interface.
A more detailed distinction: Http://code.google.com/p/memcached/wiki/PHPClientComparison




The memcached server is a centralized caching system, and the distributed implementation method is determined by the client.
Memcached's distribution algorithm generally has two options:
1, according to the result of the hash (key), the remainder of the modulus connection determines which node to store, that is, the hash (key)% sessions.size (), the algorithm is simple and fast, good performance. However, this algorithm has a disadvantage, that is, when the memcached node is added or deleted, the original cache data will be large-scale failure, the hit rate is greatly affected, if the number of nodes, cache more data, the cost of rebuilding the cache is too high, so there is a second algorithm.
2, consistent Hashing, the consistent hashing algorithm, his lookup node process is as follows:
The hash value of the memcached Server (node) is first calculated and configured on the 0~232 Circle (Continuum). It then uses the same method to find the hash value of the key that stores the data and maps it to the circle. It then searches clockwise from where the data is mapped, saving the data to the first server found. If the server is still not found after 2 32, it will be saved to the first memcached server.



Memcache in the case of no configuration, the first method is used. Memcached to implement the first method, it seems to be used (not confirmed):
$MD->setoption (Memcached::opt_hash, MEMCACHED::HASH_CRC);



The second consistent hashing algorithm:



Memcache in PHP.ini
Copy the code code as follows:
Memcache.hash_strategy =consistent
Memcache.hash_function =CRC32



Memcached in the program (not confirmed)
Copy the code code as follows:
$MD->setoption (memcached::opt_distribution, Memcached::D istribution_consistent);
$MD->setoption (Memcached::opt_hash, MEMCACHED::HASH_CRC);
Or
$mem->setoption (memcached::opt_distribution,memcached::D istribution_consistent);
$mem->setoption (memcached::opt_libketama_compatible,true);



Some reference documents:
memcached Distribution test Report (hash function selection in the case of consistent hashing):
http://www.iteye.com/topic/346682
PHP module Memcache and memcached differences: http://www.jb51.net/article/27366.htm
PHP Modules: Memcached > memcache:http://www.jb51.net/article/27367.htm



20110509@ @UPDATE:
If the installation libmemcached has the following error prompt:
MAKE[2]: * * * [CLIENTS/MS_CONN.O] Error 1
MAKE[2]: Leaving directory '/www/soft/libmemcached-0.48 '
MAKE[1]: * * * [all-recursive] Error 1
MAKE[1]: Leaving directory '/www/soft/libmemcached-0.48 '
Make: * * * [ALL] Error 2



--disable-64bit cflags= "-o3-march=i686" can be added at configure
i.e.:./configure--prefix=/usr/local/libmemcached--with-memcached--disable-64bit cflags= "-o3-march=i686"




Analysis



memcache:http://cn2.php.net/manual/en/book.memcache.php
memcached:http://cn2.php.net/manual/en/book.memcached.php
2.Memcache is a native implementation, supporting OO and non-oo two sets of interfaces coexist. The memcached is using libmemcached, which only supports OO interfaces.
The 3.memcached also has a very good place, that is, flag is not set at the time of operation, but with a unified setoption (). The memcached implements more memcached protocols.
The 4.memcached supports binary Protocol, while Memcache is not supported. This means that the memcached will have higher performance. However, Memcached currently does not support long connections.



Here is a table to compare PHP client extensions memcache and memcached
Http://code.google.com/p/memcached/wiki/PHPClientComparison



Another point is that people are more concerned about, that is, the algorithm used. As we all know, "consistent hash algorithm" is an algorithm that has less impact on data stored on memcached when a storage node is added or deleted. The algorithm can be used in PHP's two extension libraries, except that the Setup method is different.
Memcache
Modify PHP.ini Add:
[Memcache]
Memcache.allow_failover = 1
......
......
Memcache.hash_strategy =consistent
Memcache.hash_function =CRC32
......
......
Or use the Ini_set method in PHP:
Ini_set (' memcache.hash_strategy ', ' standard ');
Ini_set (' memcache.hash_function ', ' crc32 ');



Memcached
$mem = new memcached ();
$mem->setoption (memcached::opt_distribution,memcached::D istribution_consistent);
$mem->setoption (memcached::opt_libketama_compatible,true);







http://www.bkjia.com/PHPjc/444767.html www.bkjia.com true http://www.bkjia.com/PHPjc/444767.html techarticle Official homepage of memcache: PHP tutorial. Net/package/memcache ">http://pecl.php.net/package/memcache memcached official homepage:/HTTP// Pecl.php.net/package/memcached below is my install me ...


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