比如说PECL里有两个Memcached的模块,Memcache和Memcached,目前大部分PHP环境里使用的是名字里不带d的Memcache版本,这个版本释出的比较早,是一个原生版本,与之对应的带d的Memcached版本则是建立在libmemcached的基础上,所以说Memcached版本的功能更全一些。
安装Memcached版本的PHP模块
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
打开php.ini加上:
extension = "memcached.so"
这样安装就结束了,你可以通过下列命令来确认:
php -m grep mem
演示Memcached版本的新功能
先虚构一个问题,假设counter初始值是一个整数,不使用increment方法,通过get/set完成每次加一。
在Memcache版本里,我们只能按照大致如下的方式来进行:
$m = new Memcache();
$m->addServer('localhost', 11211);
$v = $m->get('counter');
$m->set('counter', $v + 1);
由于get/set这两个动作无法作为一个原子来操作,所以当多个进程同时处理时,会出现丢失的可能,更让人恼火的是,你根本就不知道什么时候出现丢失。
再看看Memcached版本里,我们是如何做的:
$md = new Memcached();
$md->addServer('localhost', 11211);
$v = $md->get('counter', null, $token)
$md->cas($token, 'counter', $v + 1);
cas是Memcached版本里提供的功能,说白了就是一个乐观锁的功能,如果你把$token的值var_dump出来,就会发现$token其实就是一个版本号,如果通过get得到的$token版本号在cas的时候不对应,就说明已经有别的操作更新了,此时cas操作会失败,至于如何继续操作,就看你自己了。
注:如果你想手动重现一下冲突的情况,可在get和cas之间sleep若干秒,并拷贝两份脚本,先后执行。
顺便说一句,推荐的Memcached版本模块的哈希设置如下:
$md->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
$md->setOption(Memcached::OPT_HASH, Memcached::HASH_CRC);
总结
Memcached版本还有很多Memcache没有的功能,比如通过getByKey, setByKey等自动支持多个服务器,就不赘述了,该用哪个扩展已经不言自明了。
补充:http://code.google.com/p/memcached/wiki/PHPClientComparison

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
