Memcached client of php

Source: Internet
Author: User
Before installing memcache, we mentioned that the memcached client is called memcache. In fact, another client based on libmemcached is memcached. it is said that memcached has better performance and more functions. Memcachememcache official homepage: http://pecl.php.net/package/memcache
Memcached official homepage: http://pecl.php.net/package/memcached

The following are the process records for installing the PHP module of the Memcached version:

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

Add in php. ini
Extension = memcached. so
Complete

In addition:
When libmemcached is installed, if only./configure is used, the following message may be displayed:
Checking for memcached... No
Configure: error: "cocould not find memcached binary"

The two are almost identical in use.
Copy codeThe code is as follows:
$ Mem = new Memcache;
$ Mem-> addServer ($ memcachehost, '20140901 ');
$ Mem-> addServer ($ memcachehost, '20140901 ');
$ Mem-> set ('hx ', '9enjoy ');
Echo $ mem-> get ('hx ');

Copy codeThe code is as follows:
$ Md = new Memcached;
$ Servers = array (
Array ($ memcachehost, '20140901 '),
Array ($ memcachehost, '20140901 ')
);
$ Md-> addServers ($ servers );
$ Md-> set ('hx ', '9enjoy ');
Echo $ md-> get ('hx ');

Memcached has more methods than memcache, such as getMulti, getByKey, and addServers.
Memcached does not have the connect method of memcache, and does not support persistent connections at present.
Memcached supports Binary Protocol, but memcache does not, which means memcached has higher performance.
Memcache is native and supports both OO and non-OO interfaces. memcached uses libmemcached and only supports OO interfaces.
More detailed difference: http://code.google.com/p/memcached/wiki/PHPClientComparison


The memcached server is a centralized cache system, and the distributed implementation method is determined by the client.
The memcached distribution algorithm generally has two options:
1. based on the hash (key) result, the remainder of the number of modulo connections determines the node to store, that is, hash (key) % sessions. size (). This algorithm is simple and fast and performs well. However, this algorithm has a disadvantage: When a memcached node is added or deleted, the original cache data will expire on a large scale, and the hit rate will be greatly affected. if the number of nodes is large and the cache data is large, rebuilding the cache is too costly, so there is a second algorithm.
2. Consistent Hashing: Consistent Hash Algorithm. the process of searching nodes is as follows:
First, obtain the hash value of the memcached server (node) and configure it to 0 ~ 232 of the circle (continuum. Then, use the same method to obtain the hash value of the key for storing the data and map it to the circle. Search clockwise from the location where the data is mapped, and save the data to the first server. If the server cannot be found after the power of 2 is 32, it will be saved to the first memcached server.

Memcache uses the first method without any configuration. Memcached seems to be used (unconfirmed) to implement the first method ):
$ Md-> setOption (Memcached: OPT_HASH, Memcached: HASH_CRC );

The second consistent hash algorithm:

Add memcache in php. ini
Copy codeThe code is as follows:
Memcache. hash_strategy = consistent
Memcache. hash_function = crc32

Memcached is added in the program (unconfirmed)
Copy codeThe code is as follows:
$ Md-> setOption (Memcached: OPT_DISTRIBUTION, Memcached: DISTRIBUTION_CONSISTENT );
$ Md-> setOption (Memcached: OPT_HASH, Memcached: HASH_CRC );
Or
$ Mem-> setOption (Memcached: OPT_DISTRIBUTION, Memcached: DISTRIBUTION_CONSISTENT );
$ Mem-> setOption (Memcached: OPT_LIBKETAMA_COMPATIBLE, true );

Some references:
Memcached distribution test report (select hash function in case of consistent hash ):
Http://www.iteye.com/topic/346682
Difference between memcache and memcached in php module: http://www.bitsCN.com/article/27366.htm
PHP module: Memcached> Memcache: http://www.bitsCN.com/article/27367.htm

20110509 @ UPDATE:
If the following error message is displayed when libmemcached is installed:
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

Add -- disable-64bit CFLAGS = "-O3-march = i686" to 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.