PHP's memcached client memcached_php tips

Source: Internet
Author: User
Tags memcached
Memcache's official homepage: Http://pecl.php.net/package/memcache
Memcached's official homepage: http://pecl.php.net/package/memcached

The following is a process record for installing the memcached version of the 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

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 you install libmemcached, if you are using only./configure, you may be prompted:
Checking for memcached ... no
Configure:error: "Could not find memcached binary"

The two are almost identical in use.
Copy Code code as follows:

$mem = new Memcache;
$mem->addserver ($memcachehost, ' 11211 ');
$mem->addserver ($memcachehost, ' 11212 ');
$mem->set (' HX ', ' 9enjoy ');
echo $mem->get (' HX ');

Copy 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 ');

There are more memcached methods than memcache, such as Getmulti,getbykey,addservers.
Memcached does not have a Memcache connect method, and currently does not support long connections.
memcached support for Binary Protocol, which memcache does not support, means that memcached will have higher performance.
Memcache is a native implementation that supports OO and OO two sets of interfaces, Memcached uses libmemcached and supports only OO interfaces.
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.
There are generally two options for memcached distribution algorithms:
1, according to the results of the hash (key), the number of modular connections to determine 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 added or deleted, the original cache data will be large-scale failure, the hit rate is greatly affected, if the number of nodes, more cache data, the cost of rebuilding the cache is too high, so there is a second algorithm.
2, consistent hashing, consistent hashing algorithm, his lookup node process is as follows:
First find the hash value of the memcached Server (node) and configure it to the 0~232 Circle (Continuum). The same method is then used to find the hash value of the key that stores the data and map it to the circle. Then start looking clockwise from where the data maps to, saving the data to the first server you find. If the server is still not found after 2 of the 32-second party, it is saved to the first memcached server.

Memcache in the absence of any configuration, is the use of the first method. Memcached to implement the first method, it appears to be using (unacknowledged):
$MD->setoption (Memcached::opt_hash, MEMCACHED::HASH_CRC);

The second consistent hash algorithm:

Memcache in php.ini.
Copy Code code as follows:

Memcache.hash_strategy =consistent
Memcache.hash_function =CRC32

Memcached in program (not confirmed)
Copy 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 documentation:
memcached Distribution test Report (hash function selection in a consistent hash case):
http://www.iteye.com/topic/346682
PHP module Memcache and memcached difference: http://www.jb51.net/article/27366.htm
PHP module: Memcached > Memcache:http://www.jb51.net/article/27367.htm

20110509@ @UPDATE:
If the installation libmemcached has the following error message:
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
That is:./configure--prefix=/usr/local/libmemcached--with-memcached--disable-64bit cflags= "-o3-march=i686"

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.