Memcache official homepage: php Tutorial. net/package/memcache "> 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"
Install the PHP module of Memcached
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 and add:
Extension = "memcached. so"
The installation is complete. You can run the following command to confirm the installation:
Php-m | grep mem
Demonstrate new features of Memcached
Assume that the counter initial value is an integer. If the increment method is not used, add one at a time through get/set.
In the Memcache version, we can only perform the following operations:
$ M = new Memcache ();
$ M-> addServer ('localhost', 11211 );
$ V = $ m-> get ('counter ');
$ M-> set ('counter ', $ v + 1 );
Because the get/set actions cannot be operated as one atom, when multiple processes are simultaneously processed, the possible loss may occur. What is even more annoying is that, you do not know when the loss occurs.
Let's take a look at how we did this 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 a function provided by Memcached. To put it bluntly, it is an optimistic lock function. If you drop the $ token value var_dump, you will find that $ token is actually a version number, if the $ token version obtained through get does not match the cas version, it indicates that another operation has been updated. At this time, the cas operation will fail. As for how to continue the operation, it depends on yourself.
Note: If you want to manually reproduce the conflict, you can sleep between get and cas for several seconds, copy the two scripts, and execute them successively.
By the way, the hash settings of the recommended Memcached version module are as follows:
$ Md-> setOption (Memcached: OPT_DISTRIBUTION, Memcached: DISTRIBUTION_CONSISTENT );
$ Md-> setOption (Memcached: OPT_HASH, Memcached: HASH_CRC );
The two are almost identical in use.
Copy the code as follows:
$ Mem = new Memcache;
$ Mem-> addServer ($ memcachehost, '20140901 ');
$ Mem-> addServer ($ memcachehost, '20140901 ');
$ Mem-> set ('hx ', '9enjoy ');
Echo $ mem-> get ('hx ');
Copy the code 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 the code as follows:
Memcache. hash_strategy = consistent
Memcache. hash_function = crc32
Memcached is added in the program (unconfirmed)
Copy the code 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.111cn.net/article/27366.htm
PHP module: Memcached> Memcache: http://www.111cn.net/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"
Analysis
Memcache: http://cn2.php.net/manual/en/book.memcache.php
Memcached: http://cn2.php.net/manual/en/book.memcached.php
2. Memcache is native and supports both OO and non-OO interfaces. Memcached uses libmemcached and only supports the OO interface.
3. memcached also has a lot to praise, that is, the flag is not set during the operation, but has a unified setOption (). Memcached implements more memcached protocols.
4. memcached supports Binary Protocol, but memcache does not. This means that memcached has higher performance. However, memcached does not currently support persistent connections.
The following table compares the extensions of memcache and memcached on the php client.
Http://code.google.com/p/memcached/wiki/PHPClientComparison
Another point of interest is the algorithm used. We all know that the consistent hash algorithm is an algorithm that has little impact on data stored in memcached when a storage node is added or deleted. This algorithm can be used in both extension libraries of php, but the setting method is different.
Memcache
Modify php. ini to 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: DISTRIBUTION_CONSISTENT );
$ Mem-> setOption (Memcached: OPT_LIBKETAMA_COMPATIBLE, true );