一、安装Memcached

yum  -y install memcached

最后出现Complete!就是安装成功了。

二. 验证安装memcached

memcached –h

会出现一堆关于memcached 命令的帮助信息,表示安装成功了,如果啥也不出来,还报错就是yum安装失败了。

三. 配置Memcached

vi /etc/sysconfig/memcached
文件中内容如下:
PORT=”11211″ //端口
USER=”memcached” //使用的用户名
MAXCONN=”1024″ //同时最大连接数
CACHESIZE=”64″ //使用的内存大小 单位为MB
OPTIONS=”" //附加参数

我暂时是没改什么参数,所以就直接退出了,如果你改了参数,需要重新重启服务修改才能生效。

四. 启动Memcached服务

chkconfig --level 2345 memcached on //开机启动
service memcached start

五. 使用memcached-tool检测memcached服务

memcached-tool 127.0.0.1:11211 stats

显示结果如下图:

此图,表示Memcached服务正常运行

六. 测试php可以正常使用Memcached

php使用Memcached需要安装php的Memcached拓展,我之前在配置服务器环境的时候,已经通过yum安装了Memcached的拓展,所以在此不再详述php安装Memcached拓展的方法。同志们可以再自行百度。

首先,创建测试文件vim /var/www/html/memcached.php,然后写入如下内容:

<?php
  $memcache = new Memcache;
  $memcache->connect('localhost', 11211) or die ("Could not connect");
  $key = md5('www.emule.com');
  $cache_result = array();
  //根据键,从缓存服务器中获取它的值
  $cache_result = $memcache->get($key);
  //如果存在该键对应的值,说明缓存中存在该内容
  if($cache_result){
      //那我们直接取出缓存的内容就可以了
      $demos_result=$cache_result;
      echo "I got the cache<br/>";
  } else {
      $demos_result = array("a","b","c");
      echo "This is the first time visited, no cache<br/>";
      //添加数据到缓存中
      $memcache->set($key, $demos_result, MEMCACHE_COMPRESSED, 1200);
  }
  foreach($demos_result as $row){
      echo $row."<br/>";
  }

?>第一次通过浏览器访问该文件,输出This is the first time visited, no cache以及输出结果。第二次通过浏览器访问该文件,输出I got the cache以及输出结果。

这样,Memcached基本算安装成功了。

你可能感兴趣的内容
MemCached缓存知识知多少? 收藏,1943 浏览
CentOS 6.5下Git服务器搭建 收藏,2500 浏览
0条评论

dexcoder

这家伙太懒了 <( ̄ ﹌  ̄)>
Owner