Memcach is a powerful tool for improving the performance of web applications and Drupal applications. Recently, Memcache has been used to find many minor problems. Therefore, the author has summarized several issues for your reference.
1. Differences between Memcache and Memcached
This is a historical issue, but in short, the name is even better!
Therefore, memcached is recommended.
In addition, memcached has many new features, including getMulti/setMulti, support for object storage, and support for count + 1 operations, which are easier to use than memcache. You can try it, maybe more operations will be supported in the future to catch up with redis ..
Q & A about memcache at Drupal University: http: // ../apachesolr_search/memcache
As follows (from question de ):
# Memcache
$ 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 process simultaneously,
# There is a possibility of loss. What's even more annoying is that you don't know when it will happen.
# Memcached
$ Md = new Memcached ();
$ Md-> addServer ('localhost', 11211 );
$ V = $ md-> get ('counter', null, $ token)
$ Md-> cas ($ token, 'counter', $ v + 1 );
# Cas is a function provided in the Memcached version. To put it bluntly, it is an optimistic lock function,
# If you dumped 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 other operations have been updated,
# At this time, the cas operation will fail. As for how to continue the operation, it depends on yourself.
The differences between the two are as follows:
The following describes the memcached method:
2. Memcache hash policy
The general hash policy is a modulo, such as key = 10. If there are two services, 10% 2 = 0, the value is calculated and distributed on the first server.
The disadvantages are also obvious. For example, if a server is added, all the previous distributed policies are finished.
Therefore, the industry needs more powerful algorithms ~
Consistent hash Algorithm debuted!
To put it simply, the consistent hash algorithm first hash the server through a certain feature (such as IP/MAC address), so that the server will be in a range according to the distribution (which may be uneven, then hash the key, and then check that the next Server closest to the key serves as the storage bin for the key.
In this way, if a server is added, the re-allocated key is only the key between the newly added server and the last closest server, and the rest remain unchanged.
Initial server distribution:
Increase the distribution of servers:
The above description is in the vernacular, which may be relatively easy to use. The author's text level is limited. For details, see the following link:
Http://blogread.cn/it/article/5271
Http://blog.csdn.net/kongqz/article/details/6695417
3. Configure the Memcache hash policy
After installing Memcache, you can set a hash policy, memcache. hash_strategy. Currently, standard and consistent modes are available. The standard mode is %, that is, the modulo. Consistent is the hash consistency algorithm.
In Memcache, the hash policy is set in the PHP. Ini file.
[Memcache]
Memcache. allow_failover = 1
......
......
Memcache. hash_strategy = consistent
Memcache. hash_function = crc32
In Memcached, the hash policy is set in PHP parameters:
Mem = new memcached ();
$ Mem-& gt; setOption (Memcached: OPT_DISTRIBUTION, Memcached: DISTRIBUTION_CONSISTENT );
$ Mem-& gt; setOption (Memcached: OPT_LIBKETAMA_COMPATIBLE, true );
4. Postscript
Therefore, it is recommended to use consistent hash algorithm under normal circumstances, but there is no absolute solution. For example, for a small site, the memcache service will almost never increase, this is a relatively recommended standard algorithm. After all, the consistency algorithm has a problem of even distribution (which may have been solved ..)