PHP has two memcache clients: phpmemcache and phpmemcached. Phpmemcache is implemented independently in php and is an old client. In practice, multiple problems have been found, with fewer functions and fewer attributes.
PHP has two memcache clients: php memcache and php memcached.
Php memcache is implemented independently using php. it is an old client. In practice, we have found multiple problems, with fewer functions and fewer attributes;
Php memcached is an extension of libmemcached based on native c. It is more comprehensive. we recommend that you replace it with php memcached.
1. Php memcache problems
1.1 distributed problems
By default, php memcache automatically switches the instance, so it sometimes gets old data and the value is erratic.
Questions shared by netizens:
During the past few days, I encountered a small problem when I was working on a product. The product used two distributed memcached servers. The number retrieved from a counter is occasionally incorrect, and is finally located on the failover mechanism of the php memcache client.
We know that in a memcached distributed environment, a key is allocated to a memcached through hash computing.
If memcache. allow_failover = 1 in php. ini, if a memcached has a problem in a distributed environment, it will automatically go to another memcached to try, and the above problem will occur. Therefore, if you want to set allow_failover = 0, the system will directly return a failure if it is not obtained from other mc instances, so as to avoid frequent instance switching when the network or server is abnormal, old data is obtained.
1.2 High Concurrency stability issues
Lessons learned from Sina Weibo:
Changing php memcache to php memcached greatly improves the stability of high concurrency;
In addition, there are more functions and more precise error codes.
Twitter cache experience
Multi-level Cache to reduce the impact of some cache nodes being down, and read/write operations are all cache;
Replace the memcached api with libmemcached (which facilitates multi-language access to memcached and ensures consistency among distributed rules .)
1.3 1 second timeout interval cannot be modified
The php memcache Client has a one-second timeout interval that cannot be modified:
Bool Memcache: connect (string $ host [, int $ port [, int $ timeout])
The third parameter can be set to timeout in seconds, but cannot be modified.
The following three methods to modify timeout are not valid:
1.3.1. memcache api Memcache: setServerParams cannot be modified;
1.3.2. modify the memcache source code vi php_memcache.h macro definition;
1.3.3. php. ini: default_socket_timeout = 60 is invalid for this timeout.
2. Comparison between memcache and memcached
The old Php memcache Client has very few options for setting attributes;
The error code is coarse-grained and difficult to locate after an error occurs;
And lack of functions:
There are primarily two clients used with PHP. One is the older, more widespread pecl/memcache and the other is the newer, less used, more feature rich pecl/memcached.
Both support the basics such as multiple servers, setting vaules, getting values, increment, decrement and getting stats.
Here are some more advanced features and information.
| 1234567891011121314151617 |
Project pecl/memcache pecl/memcachedFirst Release Date (beta) actively Developed Yes your Dependency None your Key Fixup1 Yes NoAppend/Prepend No YesAutomatic serialation2 Yes YesBinary Protocol No longer No YesCompression Yes YesCommunication Timeout Connect Only varous your Hashing Yes else Get No YesMulti-Get Yes YesSession support Yes YesSet/Get to a specific server No YesStores Numerics Converted to Strings Yes |
Note:
1 pecl/memcache will convert an invalid key into a valid key for you. pecl/memcached will return false when trying to set/get a key that is not valid.
2 You do not have to serialize your objects or arrays before sending them to the set commands. Both clients will do