memcached服務器

  • yum安裝服務器

yum -y install memcached nc telnet
rpm -qa |egrep "memcached|nc|telnet"
vim /etc/sysconfig/memcached
PORT="11211"        #默認端口號,實際工做中要改掉
USER="memcached"
MAXCONN="1024"    #最大鏈接數
CACHESIZE="64"    #緩存大小 MB
OPTIONS="-l 172.16.1.21"
systemctl restart memcached.service
systemctl enable memcached.service
  • memcache客戶端

tar xf memcache-2.2.5.tgz
cd memcache-2.2.5
/application/php/bin/phpize
./configure --enable-memcache --with-php-config=/application/php/bin/php-config --with-zlib-dir
make && make install
sed -i '$a extension=memcache.so' /application/php/lib/php.ini
pkill php    #killall php-fpm
/application/php/sbin/php-fpm -t
/application/php/sbin/php-fpm
/application/php/bin/php -m|grep memcache
  • php代碼測試

 cat >/application/nginx/html/www/mc.php<<'EOF'
<?php
$memcache = new Memcache;
$memcache->connect('172.16.1.25', 11211) or die ("Could not connect");
$memcache->set('key_oldboy0', 'hello,oldgirl0');
$memcache->set('key_oldboy1', 'hello,oldgirl1');
$memcache->set('key_oldboy2', 'hello,oldgirl2');
$get_value1 = $memcache->get('key_oldboy0');
$get_value2 = $memcache->get('key_oldboy1');
$get_value3 = $memcache->get('key_oldboy2');
echo "$get_value1 $get_value2 $get_value3";
?>
EOF
[root@cache01 ~]# printf "get key_oldboy0\r\n"|nc 172.16.1.21 11211
VALUE key_oldboy 0 14
hello,oldgirl0
END

 

  • web管理memcached

官網:http://www.junopen.com/memadmin/ 
tar xf memadmin-1.0.12.tar.gz -C /application/nginx/html/www/
更改權限:
find /application/nginx/html/www/memadmin -type d |xargs chmod 755
find /application/nginx/html/www/memadmin -type f |xargs chmod 644

默認帳號密碼:adminphp

  • Memcached緩存案例1-緩存wordpress數據庫

wordpress緩存數據緩存到memcached中:https://cn.wordpress.org/plugins/memcached/
wordpress緩存數據緩存到redis中:https://cn.wordpress.org/plugins/redis-cache/ 
wordpress會自動檢查wp-content下面是否有object-cache.php
使用memcached緩存wordpress博文數據
修改:array('127.0.0.1','');爲memcached服務器ip地址

  • Memcached Session共享

方法1:經過程序實現(開發),web01只須要往memcahce寫session,web02從memcahce讀session,看成普通數據讀寫(更具備通用性)
方法2:經過php的配置文件,php默認將session存儲在文件中,修改成存儲在memcached中
sed -i 's&session.save_handler = files&session.save_handler = memcache&;$a session.save_path = "tcp://172.16.1.21:11211"' /application/php/lib/php.ini
使用這個功能,須要使用php的session函數

相關文章
相關標籤/搜索