memcache入门
写在前面 简介 memcache是一款开源软件,由LiveJournal的Brad Fitzpatrick开发,以BSD license授权发布。 是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个巨大的hash表,将数据(包括图像、视频、文件以及数据库检索的结果等)调用到内存中,然后从内存中读取,从而大大提高读取速度。 特性 非持久性存储 定位于分布式存储,不适用于单机系统 不支持List、Array等复杂的数据 Memcache和Memcached 这款开源软件的项目名叫Memcache,Memcached (Memcached-Daemon的简称) 是软件的主程序名。 在php中,有两个为memcache开发的扩展(客户端):Memcache扩展和memcached扩展。后者比前者要新,功能也比较多。推荐安装后者。 部署环境 安装memcache服务端 sudo apt-get install memcached #for Ubuntu yum install memcached #for CentOS 安装php扩展(客户端) 这里以安装memcached扩展为例,从官网下载最新版本,解压并cd到目录 wget http://pecl.php.net/get/memcached-2.2.0.tgz tar -zxvf memcached-2.2.0.tgz cd memcached-2.2.0 编译安装 phpize #生成configure配置文件。若找不到命令请用绝对路径 /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config #配置 make #编译 make install #安装。需要root权限 安装完后会提示安装的目录: Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/ 编辑php.ini文件 #找到extension_dir,如果没有设置扩展的路径,则把上面memcached.so所在的路径粘贴进去 extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/" #找到;extension=php_XXX.dll的地方,在最下面添加上 extension=memcached.so 最后,重启web服务器和php-fpm 看看phpinfo()是否将memcached添加成功。我这里为了做测试编译了两个扩展,实际编译后者就可以。 简单使用 这里只介绍php中memcached类的使用。更多请参考手册 <?php $md = new memcached; //向服务器池添加服务器,也可以使用addServers方法一次添加多个 //public bool Memcached::addServer ( string $host , int $port [, int $weight = 0 ] ) $md->addServer('127.0.0.1',11211); //$expiration默认为0,永不超时。但即使为0,当内存不够用的时候,memcache也会将不常使用的数据删掉 //expiration不能超过60×60×24×30(30天时间的秒数);如果大于这个值,服务端会将其作为一个真实的Unix时间戳来处理而不是 自当前时间的偏移 //public bool Memcached::add ( string $key , mixed $value [, int $expiration ] ) $md->add('addItem','this is content one by add'); $md->add('addItem','this is content two by add');//false.若使用add方法添加已经存在的key,无法添加成功 //public mixed Memcached::get ( string $key [, callback $cache_cb [, float &$cas_token ]] ) echo $md->get('addItem');//after add:this is content one by add //public bool Memcached::delete ( string $key [, int $time = 0 ] ) $md->delete('addItem'); echo $md->get('addItem');//空 //使用set方法重复添加同样的key是合法的,且后面的数据会覆盖前面的数据 //public bool Memcached::set ( string $key , mixed $value [, int $expiration ] ) $md->set('tip','hello');//tip='hello' $md->set('tip','world');//tip='world' //若使用追加方法,必须将OPT_COMPRESSION(压缩)设置为false $md->setOption(Memcached::OPT_COMPRESSION,false); $md->set('tip','hello');//tip='hello' //向指定元素后面追加一个字符串 //public bool Memcached::append ( string $key , string $value ) $md->append('tip',' world');//tip='hello world' $arr = [ 'name' => 'foam', 'age' => '22', ]; //同时存储多个数据 //public bool Memcached::setMulti ( array $items [, int $expiration ] ) $md->setMulti($arr); echo $md->get('name');//foam echo $md->get('age');//22 $md->set('count','-99');//count=-99 //自增 //public int Memcached::increment ( string $key [, int $offset = 1 ] ) $md->increment('count');//false.对于负数和非整数是无法自增的 $md->set('count',0); $md->increment('count');//count=1 $md->increment('count',100);//count=101 //自减 //public int Memcached::decrement ( string $key [, int $offset = 1 ] ) $md->decrement('count');//count=100 $md->decrement('count',50);//count=50 $md->decrement('count',60);//count=0.最小只能减到0 //清空所有数据 //public bool Memcached::flush ([ int $delay = 0 ] ) $md->flush(); $md->get('tip');//false $md->get('count');//false 其他 Memcache的分布式缓存 Memcache尽管说是分布式缓存服务器,但其分布式却是由客户端实现的。 保存数据时,客户端会根据数据的键使用特定的算法(不同的客户端,算法不同)选择要保存的服务器,将其存入其中。获取数据时也是使用相同的算法在所在的服务器获取。这些客户端算法一般在Hash的基础上进行改良,保证其数据的分散性。 具体策略就不展开说明了。 ...