当前位置:   article > 正文

六、企业级架构缓存篇之memcached

六、企业级架构缓存篇之memcached

一、memcached概述

1、网站架构优化流程:

LNMP架构中网站应用访问流程:

浏览器 (app) → web 服务器 → 后端服务 (php) → 数据库 (mysql)

访问流程越多,访问速度越慢,出现问题的几率也越大。

网站访问流程优化思路:

① 采用高性能的负载均衡设备;

② 页面静态化;

③ 引入缓存层,如使用Redis或Memcached,来缓存频繁访问的数据;

④ 优化数据库本身。

2、mencached介绍:

许多Web应用都将数据保存到 RDBMS (关系型数据库) 中,应用服务器从中读取数据并在浏览器中显示。 但随着数据量的增大、访问的集中,就会出现RDBMS的负担加重、数据库响应恶化、 网站显示延迟等重大影响。

Memcached 是高性能的分布式内存缓存服务器,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web等应用的速度, 提高可扩展性。

Web01/Web02 => memcached(内存) => MyCAT => MySQL01/MySQL02

3安装memcached软件:

克隆CentOS7生成一个memcached服务器,修改IP、UUID、主机名、hosts文件,关闭防火墙、selinux和NetworkManager。

(1) 架构图:

(2) 编译与安装:

  1. tar -zxf memcached-1.5.8.tar.gz
  2. cd memcached-1.5.8
  3. yum install libevent-devel -y
  4. yum -y install gcc gcc-c++ autoconf automake make
  5. ./configure --prefix=/usr/local/memcached
  6. make && make install

(3) 查看目录:

cd /usr/local/memcahced

bin:memcached 二进制程序文件

include:memcached 依赖库文件

bin/memcached -h:

-p  设置memcached监听的端口,默认为11211

-d  启动一个守护进程deamon(后台运行)

-m 分配给memcached使用的内存数量,单位是MB,默认为64M

-u  运行memcached的用户

-l   监听服务器IP地址,可以有多个地址

-c  最大运行的并发连接数,默认是1024

-P 设置保存memcached的pid文件

(4) 启动memcached软件:

bin/memcached -uroot -d :以root用户启动memcached,后台运行

4、使用Telnet连接memcached:

(1) telnet概述:

Telnet协议是TCP/IP协议族中的一部分,是Internet远程登录服务的标准协议之一。

Telnet协议允许用户通过网络连接到远程主机,并在该主机上执行命令、访问文件、查看系统状态等。

由于Telnet协议的通信内容并未加密,存在安全风险,因此在实际应用中,Telnet通常被SSH(Secure Shell)所取代,SSH提供了加密的通信通道,更安全地实现了远程访问。

(2) 安装telnet并连接memcached

yum install telnet -y

telnet memcachedd服务器IP memcahced服务端口

回车后看到ERROR说明已经连接上memcached,ERROR是memcached对空命令的报错。

二、memcached常用管理命令

memcached操作的是计算机的内存空间,通过内存来缓存数据,其数据结构是一个key:value的键值对。

1、set命令与get命令:

(1) set 命令:

set key flag exptime bytes

set:set命令,用于设置键值对(存储数据)。

key:键名,必须是唯一的,同名会被覆盖。

flag:数据的标识位,0代表没有特殊标识。

exptime:过期时间,0代表不过期,单位为s。

bytes:存储数据的字节数。

(2) get命令:

get key

get:get命令,用于获取键值对信息(获取数据)。

key:键名,根据提供的键名,搜索指定的数据。

如果 key 不存在,则返回空:

get key1 key2

get 命令可一次获取多个键的值:

2、add命令与replace命令:

(1) add命令:

add key flag exptime bytes

add 命令用于将 value 存储在指定的 key 中,如果 add 的 key 已经存在,则不会更新。(修改键)

set 命令则会直接覆盖旧的 key。

出现NOT_STORED代表系统中已经存在了一个键名为name的键值对。

(2) replace命令:

replace key flag exptime bytes

replace 命令用于替换已存在的 key 的 value。(修改值)

如果 key不存在,则替换失败,响应 NOT_STORED。

3、append命令与prepend命令:

(1) append命令:

append key flag exptime bytes

append 命令用于向已存在 key 的 value 尾部追加数据(尾部追加)。

定义一个key=hw,存储数据hello,然后追加一个world:

(2) prepend命令:

prepend key flag exptime bytes

prepend 命令用于向已存在 key 的 value 前面追加数据(头部追加)。

定义一个name=hema,然后追加一个it到hema的前面:

4、delete命令与flush_all命令:

(1) delete命令:

delete key

delete 命令指定用于删除已存在的key。

(2) ush_all清空命令:

flush_all [time]

time:用于在指定时间后执行清空操作

清空memcached中的所有缓存的数据,生产环境下尽量不要使用。

5、计算命令与stats命令:

(1) 计算命令:

incr key value   =>  在原有值的基础上进行自增操作

decr key value  =>  在原有值的基础上进行自减操作

(2) stats命令:

stats命令用来获取memcached的运行信息。

cmd_get => memcached运行以来get获取的次数

cmd_set => memcached运行以来set设置的次数

get_hits => 缓存命中数,对于memcached服务器非常重要的指标

① 缓存命中率:get_hits/cmd_get

如果命中率低,业务代码缓存有问题 ;命中率为0,说明缓存没有起作用 。

缓存穿透:数据访问经过缓存,没有获取到数据,再直接访问数据库。

缓存雪崩:数据查询全部没有命中,直接访问数据库,数据压力上升。

解决方案:

当数据库中的数据发生变化时,应该尽快将这些变化同步到 Memcached 中;

合理设置缓存的失效时间,并将它们分布开,确保缓存的过期事件不会集中在某个时间点,避免缓存雪崩。

6、memcached注意事项:

① memcached最大过期时间:

过期时间设置最大为30天 (30x24x60x60=2592000s),超过后,其值无法设置进缓存;

② memcached单个key最大长度:

memcached的key的最大长度是250个字符,是memcached服务端的限制;

③ memcached单个item最大能存储:1M。

三、php安装memcached扩展

1、为什么需要扩展:

PHP默认不支持memcached连接,需要通过memcached扩展程序连接php与memcached。

PHP → mecached.so → Memcached

2、安装PHP Memcached扩展:

(1) 准备软件:

libmemcached-1.0.18.tar.gz :php memcached 扩展的依赖库

memcached-3.1.3.tgz :php memcached扩展

(2) 准备环境变量:

PHP扩展编译安装,需要web服务器支持phpize和php-cong

把php的bin目录配置到PATH中(web01 / web02):

  1. echo 'export PATH=$PATH:/usr/local/php/bin' >> /etc/profile
  2. source /etc/profile

(3) 安装php memcached扩展:

安装libmemcached

  1. tar xvf libmemcached-1.0.18.tar.gz
  2. cd libmemcached-1.0.18
  3. #指定安装目录编译configure脚本
  4. ./configure --prefix=/usr/local/libmemcached
  5. make && make install

安装memcached.so:

  1. tar -zxf memcached-3.1.3.tgz
  2. cd memcached-3.1.3
  3. #刚解压出来的memcached-3.1.3中没有configure脚本,需要运行phpize命令生成所需要的configure脚本
  4. /usr/local/php/bin/phpize
  5. ./configure --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl
  6. make && make install

脚本安装:

  1. #!/bin/bash
  2. echo "====================================="
  3. echo "1)安装libmemcached"
  4. echo "2)安装memcached.so"
  5. echo "====================================="
  6. read -p "Please Input The Number 1 or 2 =>" key
  7. libmemcached() {
  8. echo "Start Install libmemcached ..."
  9. sleep 1
  10. tar -zxf /root/libmemcached-1.0.18.tar.gz
  11. cd libmemcached-1.0.18
  12. ./configure --prefix=/usr/local/libmemcached
  13. make && make install
  14. }
  15. memcached() {
  16. echo "Start Install php memcached.so ..."
  17. sleep 1
  18. tar -zxf /root/memcached-3.1.3.tgz
  19. cd memcached-3.1.3
  20. /usr/local/php/bin/phpize
  21. ./configure --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl
  22. make && make install
  23. }
  24. case $key in
  25. 1)
  26. libmemcached
  27. ;;
  28. 2)
  29. memcached
  30. ;;
  31. esac

查看是否安装扩展:

/usr/local/php/lib/php/extensions/no-debug-zts-20170708:php扩展库的存储路径

(4) 在php.ini文件中开启memcached.so扩展:

vim /usr/local/php/etc/php.ini

extension=memcached.so #在第909行添加这行扩展

设置完成后,重启php-fpm服务:

service php-fpm restart

(5) 测试PHP Memcached扩展是否安装成功 :

在/home/www/public目录下创建一个demo.php文件:

  1. <?php
  2. phpinfo();
  3. ?>

3、使用PHP操纵Memcached服务器:

在web服务器上编写php代码,通过php的memcached扩展与memcached服务器交互。

Web01/Web02的 home/www/public 下创建一个 demo.php:

  1. <?php
  2. $mem=new Memcached(); //创建一个对象,用于与Memcached服务器进行通信
  3. $mem->addServer('10.1.1.20',11211); //指定Memcached服务监听的地址和端口
  4. $mem->set('name','itheima'); //使用set方法将一个键值对存储到Memcached中
  5. var_dump($mem->get('name')); //调试输出,输出 name这个键的内容
  6. ?>

先情况memcached内存中所有的key。

访问web服务器ip/demo.php,显示出在memcached服务器中存储的键值,说明PHP已经与memcached交互:

在memcached中可查看到name键值对。

4、memcached_tool:

(1) 作用:

用于查看memcached运行状态、有多少key(item)、内存使用了多少,以及查看缓存命中率等指标。

(2) 配置:

上传memcache_tools.php文件到Web服务器中的/home/www/public目录下

  1. <?php
  2. /*
  3. +----------------------------------------------------------------------+
  4. | PHP Version 5 |
  5. +----------------------------------------------------------------------+
  6. | Copyright (c) 1997-2004 The PHP Group |
  7. +----------------------------------------------------------------------+
  8. | This source file is subject to version 3.0 of the PHP license, |
  9. | that is bundled with this package in the file LICENSE, and is |
  10. | available through the world-wide-web at the following url: |
  11. | http://www.php.net/license/3_0.txt. |
  12. | If you did not receive a copy of the PHP license and are unable to |
  13. | obtain it through the world-wide-web, please send a note to |
  14. | license@php.net so we can mail you a copy immediately. |
  15. +----------------------------------------------------------------------+
  16. | Author: Harun Yayli <harunyayli at gmail.com> |
  17. +----------------------------------------------------------------------+
  18. */
  19. $VERSION='$Id: memcache.php,v 1.1.2.3 2008/08/28 18:07:54 mikl Exp $';
  20. define('ADMIN_USERNAME','root'); // Admin Username
  21. define('ADMIN_PASSWORD','123456'); // Admin Password
  22. define('DATE_FORMAT','Y/m/d H:i:s');
  23. define('GRAPH_SIZE',200);
  24. define('MAX_ITEM_DUMP',50);
  25. $MEMCACHE_SERVERS[] = '192.168.65.128:11211'; // add more as an array
  26. // END OF DEFAULT CONFIG AREA /
  27. / Password protect
  28. if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) ||
  29. $_SERVER['PHP_AUTH_USER'] != ADMIN_USERNAME ||$_SERVER['PHP_AUTH_PW'] != ADMIN_PASSWORD) {
  30. Header("WWW-Authenticate: Basic realm=\"Memcache Login\"");
  31. Header("HTTP/1.0 401 Unauthorized");
  32. echo <<<EOB
  33. <html><body>
  34. <h1>Rejected!</h1>
  35. <big>Wrong Username or Password!</big>
  36. </body></html>
  37. EOB;
  38. exit;
  39. }
  40. ///MEMCACHE FUNCTIONS /
  41. function sendMemcacheCommands($command){
  42. global $MEMCACHE_SERVERS;
  43. $result = array();
  44. foreach($MEMCACHE_SERVERS as $server){
  45. $strs = explode(':',$server);
  46. $host = $strs[0];
  47. $port = $strs[1];
  48. $result[$server] = sendMemcacheCommand($host,$port,$command);
  49. }
  50. return $result;
  51. }
  52. function sendMemcacheCommand($server,$port,$command){
  53. $s = @fsockopen($server,$port);
  54. if (!$s){
  55. die("Cant connect to:".$server.':'.$port);
  56. }
  57. fwrite($s, $command."\r\n");
  58. $buf='';
  59. while ((!feof($s))) {
  60. $buf .= fgets($s, 256);
  61. if (strpos($buf,"END\r\n")!==false){ // stat says end
  62. break;
  63. }
  64. if (strpos($buf,"DELETED\r\n")!==false || strpos($buf,"NOT_FOUND\r\n")!==false){ // delete says these
  65. break;
  66. }
  67. if (strpos($buf,"OK\r\n")!==false){ // flush_all says ok
  68. break;
  69. }
  70. }
  71. fclose($s);
  72. return parseMemcacheResults($buf);
  73. }
  74. function parseMemcacheResults($str){
  75. $res = array();
  76. $lines = explode("\r\n",$str);
  77. $cnt = count($lines);
  78. for($i=0; $i< $cnt; $i++){
  79. $line = $lines[$i];
  80. $l = explode(' ',$line,3);
  81. if (count($l)==3){
  82. $res[$l[0]][$l[1]]=$l[2];
  83. if ($l[0]=='VALUE'){ // next line is the value
  84. $res[$l[0]][$l[1]] = array();
  85. list ($flag,$size)=explode(' ',$l[2]);
  86. $res[$l[0]][$l[1]]['stat']=array('flag'=>$flag,'size'=>$size);
  87. $res[$l[0]][$l[1]]['value']=$lines[++$i];
  88. }
  89. }elseif($line=='DELETED' || $line=='NOT_FOUND' || $line=='OK'){
  90. return $line;
  91. }
  92. }
  93. return $res;
  94. }
  95. function dumpCacheSlab($server,$slabId,$limit){
  96. list($host,$port) = explode(':',$server);
  97. $resp = sendMemcacheCommand($host,$port,'stats cachedump '.$slabId.' '.$limit);
  98. return $resp;
  99. }
  100. function flushServer($server){
  101. list($host,$port) = explode(':',$server);
  102. $resp = sendMemcacheCommand($host,$port,'flush_all');
  103. return $resp;
  104. }
  105. function getCacheItems(){
  106. $items = sendMemcacheCommands('stats items');
  107. $serverItems = array();
  108. $totalItems = array();
  109. foreach ($items as $server=>$itemlist){
  110. $serverItems[$server] = array();
  111. $totalItems[$server]=0;
  112. if (!isset($itemlist['STAT'])){
  113. continue;
  114. }
  115. $iteminfo = $itemlist['STAT'];
  116. foreach($iteminfo as $keyinfo=>$value){
  117. if (preg_match('/items\:(\d+?)\:(.+?)$/',$keyinfo,$matches)){
  118. $serverItems[$server][$matches[1]][$matches[2]] = $value;
  119. if ($matches[2]=='number'){
  120. $totalItems[$server] +=$value;
  121. }
  122. }
  123. }
  124. }
  125. return array('items'=>$serverItems,'counts'=>$totalItems);
  126. }
  127. function getMemcacheStats($total=true){
  128. $resp = sendMemcacheCommands('stats');
  129. if ($total){
  130. $res = array();
  131. foreach($resp as $server=>$r){
  132. foreach($r['STAT'] as $key=>$row){
  133. if (!isset($res[$key])){
  134. $res[$key]=null;
  135. }
  136. switch ($key){
  137. case 'pid':
  138. $res['pid'][$server]=$row;
  139. break;
  140. case 'uptime':
  141. $res['uptime'][$server]=$row;
  142. break;
  143. case 'time':
  144. $res['time'][$server]=$row;
  145. break;
  146. case 'version':
  147. $res['version'][$server]=$row;
  148. break;
  149. case 'pointer_size':
  150. $res['pointer_size'][$server]=$row;
  151. break;
  152. case 'rusage_user':
  153. $res['rusage_user'][$server]=$row;
  154. break;
  155. case 'rusage_system':
  156. $res['rusage_system'][$server]=$row;
  157. break;
  158. case 'curr_items':
  159. $res['curr_items']+=$row;
  160. break;
  161. case 'total_items':
  162. $res['total_items']+=$row;
  163. break;
  164. case 'bytes':
  165. $res['bytes']+=$row;
  166. break;
  167. case 'curr_connections':
  168. $res['curr_connections']+=$row;
  169. break;
  170. case 'total_connections':
  171. $res['total_connections']+=$row;
  172. break;
  173. case 'connection_structures':
  174. $res['connection_structures']+=$row;
  175. break;
  176. case 'cmd_get':
  177. $res['cmd_get']+=$row;
  178. break;
  179. case 'cmd_set':
  180. $res['cmd_set']+=$row;
  181. break;
  182. case 'get_hits':
  183. $res['get_hits']+=$row;
  184. break;
  185. case 'get_misses':
  186. $res['get_misses']+=$row;
  187. break;
  188. case 'evictions':
  189. $res['evictions']+=$row;
  190. break;
  191. case 'bytes_read':
  192. $res['bytes_read']+=$row;
  193. break;
  194. case 'bytes_written':
  195. $res['bytes_written']+=$row;
  196. break;
  197. case 'limit_maxbytes':
  198. $res['limit_maxbytes']+=$row;
  199. break;
  200. case 'threads':
  201. $res['rusage_system'][$server]=$row;
  202. break;
  203. }
  204. }
  205. }
  206. return $res;
  207. }
  208. return $resp;
  209. }
  210. //
  211. //
  212. // don't cache this page
  213. //
  214. header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
  215. header("Cache-Control: post-check=0, pre-check=0", false);
  216. header("Pragma: no-cache"); // HTTP/1.0
  217. function duration($ts) {
  218. global $time;
  219. $years = (int)((($time - $ts)/(7*86400))/52.177457);
  220. $rem = (int)(($time-$ts)-($years * 52.177457 * 7 * 86400));
  221. $weeks = (int)(($rem)/(7*86400));
  222. $days = (int)(($rem)/86400) - $weeks*7;
  223. $hours = (int)(($rem)/3600) - $days*24 - $weeks*7*24;
  224. $mins = (int)(($rem)/60) - $hours*60 - $days*24*60 - $weeks*7*24*60;
  225. $str = '';
  226. if($years==1) $str .= "$years year, ";
  227. if($years>1) $str .= "$years years, ";
  228. if($weeks==1) $str .= "$weeks week, ";
  229. if($weeks>1) $str .= "$weeks weeks, ";
  230. if($days==1) $str .= "$days day,";
  231. if($days>1) $str .= "$days days,";
  232. if($hours == 1) $str .= " $hours hour and";
  233. if($hours>1) $str .= " $hours hours and";
  234. if($mins == 1) $str .= " 1 minute";
  235. else $str .= " $mins minutes";
  236. return $str;
  237. }
  238. // create graphics
  239. //
  240. function graphics_avail() {
  241. return extension_loaded('gd');
  242. }
  243. function bsize($s) {
  244. foreach (array('','K','M','G') as $i => $k) {
  245. if ($s < 1024) break;
  246. $s/=1024;
  247. }
  248. return sprintf("%5.1f %sBytes",$s,$k);
  249. }
  250. // create menu entry
  251. function menu_entry($ob,$title) {
  252. global $PHP_SELF;
  253. if ($ob==$_GET['op']){
  254. return "<li><a class=\"child_active\" href=\"$PHP_SELF&op=$ob\">$title</a></li>";
  255. }
  256. return "<li><a class=\"active\" href=\"$PHP_SELF&op=$ob\">$title</a></li>";
  257. }
  258. function getHeader(){
  259. $header = <<<EOB
  260. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  261. <html>
  262. <head><title>MEMCACHE INFO</title>
  263. <style type="text/css"><!--
  264. body { background:white; font-size:100.01%; margin:0; padding:0; }
  265. body,p,td,th,input,submit { font-size:0.8em;font-family:arial,helvetica,sans-serif; }
  266. * html body {font-size:0.8em}
  267. * html p {font-size:0.8em}
  268. * html td {font-size:0.8em}
  269. * html th {font-size:0.8em}
  270. * html input {font-size:0.8em}
  271. * html submit {font-size:0.8em}
  272. td { vertical-align:top }
  273. a { color:black; font-weight:none; text-decoration:none; }
  274. a:hover { text-decoration:underline; }
  275. div.content { padding:1em 1em 1em 1em; position:absolute; width:97%; z-index:100; }
  276. h1.memcache { background:rgb(153,153,204); margin:0; padding:0.5em 1em 0.5em 1em; }
  277. * html h1.memcache { margin-bottom:-7px; }
  278. h1.memcache a:hover { text-decoration:none; color:rgb(90,90,90); }
  279. h1.memcache span.logo {
  280. background:rgb(119,123,180);
  281. color:black;
  282. border-right: solid black 1px;
  283. border-bottom: solid black 1px;
  284. font-style:italic;
  285. font-size:1em;
  286. padding-left:1.2em;
  287. padding-right:1.2em;
  288. text-align:right;
  289. display:block;
  290. width:130px;
  291. }
  292. h1.memcache span.logo span.name { color:white; font-size:0.7em; padding:0 0.8em 0 2em; }
  293. h1.memcache span.nameinfo { color:white; display:inline; font-size:0.4em; margin-left: 3em; }
  294. h1.memcache div.copy { color:black; font-size:0.4em; position:absolute; right:1em; }
  295. hr.memcache {
  296. background:white;
  297. border-bottom:solid rgb(102,102,153) 1px;
  298. border-style:none;
  299. border-top:solid rgb(102,102,153) 10px;
  300. height:12px;
  301. margin:0;
  302. margin-top:1px;
  303. padding:0;
  304. }
  305. ol,menu { margin:1em 0 0 0; padding:0.2em; margin-left:1em;}
  306. ol.menu li { display:inline; margin-right:0.7em; list-style:none; font-size:85%}
  307. ol.menu a {
  308. background:rgb(153,153,204);
  309. border:solid rgb(102,102,153) 2px;
  310. color:white;
  311. font-weight:bold;
  312. margin-right:0em;
  313. padding:0.1em 0.5em 0.1em 0.5em;
  314. text-decoration:none;
  315. margin-left: 5px;
  316. }
  317. ol.menu a.child_active {
  318. background:rgb(153,153,204);
  319. border:solid rgb(102,102,153) 2px;
  320. color:white;
  321. font-weight:bold;
  322. margin-right:0em;
  323. padding:0.1em 0.5em 0.1em 0.5em;
  324. text-decoration:none;
  325. border-left: solid black 5px;
  326. margin-left: 0px;
  327. }
  328. ol.menu span.active {
  329. background:rgb(153,153,204);
  330. border:solid rgb(102,102,153) 2px;
  331. color:black;
  332. font-weight:bold;
  333. margin-right:0em;
  334. padding:0.1em 0.5em 0.1em 0.5em;
  335. text-decoration:none;
  336. border-left: solid black 5px;
  337. }
  338. ol.menu span.inactive {
  339. background:rgb(193,193,244);
  340. border:solid rgb(182,182,233) 2px;
  341. color:white;
  342. font-weight:bold;
  343. margin-right:0em;
  344. padding:0.1em 0.5em 0.1em 0.5em;
  345. text-decoration:none;
  346. margin-left: 5px;
  347. }
  348. ol.menu a:hover {
  349. background:rgb(193,193,244);
  350. text-decoration:none;
  351. }
  352. div.info {
  353. background:rgb(204,204,204);
  354. border:solid rgb(204,204,204) 1px;
  355. margin-bottom:1em;
  356. }
  357. div.info h2 {
  358. background:rgb(204,204,204);
  359. color:black;
  360. font-size:1em;
  361. margin:0;
  362. padding:0.1em 1em 0.1em 1em;
  363. }
  364. div.info table {
  365. border:solid rgb(204,204,204) 1px;
  366. border-spacing:0;
  367. width:100%;
  368. }
  369. div.info table th {
  370. background:rgb(204,204,204);
  371. color:white;
  372. margin:0;
  373. padding:0.1em 1em 0.1em 1em;
  374. }
  375. div.info table th a.sortable { color:black; }
  376. div.info table tr.tr-0 { background:rgb(238,238,238); }
  377. div.info table tr.tr-1 { background:rgb(221,221,221); }
  378. div.info table td { padding:0.3em 1em 0.3em 1em; }
  379. div.info table td.td-0 { border-right:solid rgb(102,102,153) 1px; white-space:nowrap; }
  380. div.info table td.td-n { border-right:solid rgb(102,102,153) 1px; }
  381. div.info table td h3 {
  382. color:black;
  383. font-size:1.1em;
  384. margin-left:-0.3em;
  385. }
  386. .td-0 a , .td-n a, .tr-0 a , tr-1 a {
  387. text-decoration:underline;
  388. }
  389. div.graph { margin-bottom:1em }
  390. div.graph h2 { background:rgb(204,204,204);; color:black; font-size:1em; margin:0; padding:0.1em 1em 0.1em 1em; }
  391. div.graph table { border:solid rgb(204,204,204) 1px; color:black; font-weight:normal; width:100%; }
  392. div.graph table td.td-0 { background:rgb(238,238,238); }
  393. div.graph table td.td-1 { background:rgb(221,221,221); }
  394. div.graph table td { padding:0.2em 1em 0.4em 1em; }
  395. div.div1,div.div2 { margin-bottom:1em; width:35em; }
  396. div.div3 { position:absolute; left:40em; top:1em; width:580px; }
  397. //div.div3 { position:absolute; left:37em; top:1em; right:1em; }
  398. div.sorting { margin:1.5em 0em 1.5em 2em }
  399. .center { text-align:center }
  400. .aright { position:absolute;right:1em }
  401. .right { text-align:right }
  402. .ok { color:rgb(0,200,0); font-weight:bold}
  403. .failed { color:rgb(200,0,0); font-weight:bold}
  404. span.box {
  405. border: black solid 1px;
  406. border-right:solid black 2px;
  407. border-bottom:solid black 2px;
  408. padding:0 0.5em 0 0.5em;
  409. margin-right:1em;
  410. }
  411. span.green { background:#60F060; padding:0 0.5em 0 0.5em}
  412. span.red { background:#D06030; padding:0 0.5em 0 0.5em }
  413. div.authneeded {
  414. background:rgb(238,238,238);
  415. border:solid rgb(204,204,204) 1px;
  416. color:rgb(200,0,0);
  417. font-size:1.2em;
  418. font-weight:bold;
  419. padding:2em;
  420. text-align:center;
  421. }
  422. input {
  423. background:rgb(153,153,204);
  424. border:solid rgb(102,102,153) 2px;
  425. color:white;
  426. font-weight:bold;
  427. margin-right:1em;
  428. padding:0.1em 0.5em 0.1em 0.5em;
  429. }
  430. //-->
  431. </style>
  432. </head>
  433. <body>
  434. <div class="head">
  435. <h1 class="memcache">
  436. <span class="logo"><a href="http://pecl.php.net/package/memcache">memcache</a></span>
  437. <span class="nameinfo">memcache.php by <a href="http://livebookmark.net">Harun Yayli</a></span>
  438. </h1>
  439. <hr class="memcache">
  440. </div>
  441. <div class=content>
  442. EOB;
  443. return $header;
  444. }
  445. function getFooter(){
  446. global $VERSION;
  447. $footer = '</div><!-- Based on apc.php '.$VERSION.'--></body>
  448. </html>
  449. ';
  450. return $footer;
  451. }
  452. function getMenu(){
  453. global $PHP_SELF;
  454. echo "<ol class=menu>";
  455. if ($_GET['op']!=4){
  456. echo <<<EOB
  457. <li><a href="$PHP_SELF&op={$_GET['op']}">Refresh Data</a></li>
  458. EOB;
  459. }
  460. else {
  461. echo <<<EOB
  462. <li><a href="$PHP_SELF&op=2}">Back</a></li>
  463. EOB;
  464. }
  465. echo
  466. menu_entry(1,'View Host Stats'),
  467. menu_entry(2,'Variables');
  468. echo <<<EOB
  469. </ol>
  470. <br/>
  471. EOB;
  472. }
  473. // TODO, AUTH
  474. $_GET['op'] = !isset($_GET['op'])? '1':$_GET['op'];
  475. $PHP_SELF= isset($_SERVER['PHP_SELF']) ? htmlentities(strip_tags($_SERVER['PHP_SELF'],'')) : '';
  476. $PHP_SELF=$PHP_SELF.'?';
  477. $time = time();
  478. // sanitize _GET
  479. foreach($_GET as $key=>$g){
  480. $_GET[$key]=htmlentities($g);
  481. }
  482. // singleout
  483. // when singleout is set, it only gives details for that server.
  484. if (isset($_GET['singleout']) && $_GET['singleout']>=0 && $_GET['singleout'] <count($MEMCACHE_SERVERS)){
  485. $MEMCACHE_SERVERS = array($MEMCACHE_SERVERS[$_GET['singleout']]);
  486. }
  487. // display images
  488. if (isset($_GET['IMG'])){
  489. $memcacheStats = getMemcacheStats();
  490. $memcacheStatsSingle = getMemcacheStats(false);
  491. if (!graphics_avail()) {
  492. exit(0);
  493. }
  494. function fill_box($im, $x, $y, $w, $h, $color1, $color2,$text='',$placeindex='') {
  495. global $col_black;
  496. $x1=$x+$w-1;
  497. $y1=$y+$h-1;
  498. imagerectangle($im, $x, $y1, $x1+1, $y+1, $col_black);
  499. if($y1>$y) imagefilledrectangle($im, $x, $y, $x1, $y1, $color2);
  500. else imagefilledrectangle($im, $x, $y1, $x1, $y, $color2);
  501. imagerectangle($im, $x, $y1, $x1, $y, $color1);
  502. if ($text) {
  503. if ($placeindex>0) {
  504. if ($placeindex<16)
  505. {
  506. $px=5;
  507. $py=$placeindex*12+6;
  508. imagefilledrectangle($im, $px+90, $py+3, $px+90-4, $py-3, $color2);
  509. imageline($im,$x,$y+$h/2,$px+90,$py,$color2);
  510. imagestring($im,2,$px,$py-6,$text,$color1);
  511. } else {
  512. if ($placeindex<31) {
  513. $px=$x+40*2;
  514. $py=($placeindex-15)*12+6;
  515. } else {
  516. $px=$x+40*2+100*intval(($placeindex-15)/15);
  517. $py=($placeindex%15)*12+6;
  518. }
  519. imagefilledrectangle($im, $px, $py+3, $px-4, $py-3, $color2);
  520. imageline($im,$x+$w,$y+$h/2,$px,$py,$color2);
  521. imagestring($im,2,$px+2,$py-6,$text,$color1);
  522. }
  523. } else {
  524. imagestring($im,4,$x+5,$y1-16,$text,$color1);
  525. }
  526. }
  527. }
  528. function fill_arc($im, $centerX, $centerY, $diameter, $start, $end, $color1,$color2,$text='',$placeindex=0) {
  529. $r=$diameter/2;
  530. $w=deg2rad((360+$start+($end-$start)/2)%360);
  531. if (function_exists("imagefilledarc")) {
  532. // exists only if GD 2.0.1 is avaliable
  533. imagefilledarc($im, $centerX+1, $centerY+1, $diameter, $diameter, $start, $end, $color1, IMG_ARC_PIE);
  534. imagefilledarc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color2, IMG_ARC_PIE);
  535. imagefilledarc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color1, IMG_ARC_NOFILL|IMG_ARC_EDGED);
  536. } else {
  537. imagearc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color2);
  538. imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($start)) * $r, $centerY + sin(deg2rad($start)) * $r, $color2);
  539. imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($start+1)) * $r, $centerY + sin(deg2rad($start)) * $r, $color2);
  540. imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($end-1)) * $r, $centerY + sin(deg2rad($end)) * $r, $color2);
  541. imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($end)) * $r, $centerY + sin(deg2rad($end)) * $r, $color2);
  542. imagefill($im,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2, $color2);
  543. }
  544. if ($text) {
  545. if ($placeindex>0) {
  546. imageline($im,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2,$diameter, $placeindex*12,$color1);
  547. imagestring($im,4,$diameter, $placeindex*12,$text,$color1);
  548. } else {
  549. imagestring($im,4,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2,$text,$color1);
  550. }
  551. }
  552. }
  553. $size = GRAPH_SIZE; // image size
  554. $image = imagecreate($size+50, $size+10);
  555. $col_white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
  556. $col_red = imagecolorallocate($image, 0xD0, 0x60, 0x30);
  557. $col_green = imagecolorallocate($image, 0x60, 0xF0, 0x60);
  558. $col_black = imagecolorallocate($image, 0, 0, 0);
  559. imagecolortransparent($image,$col_white);
  560. switch ($_GET['IMG']){
  561. case 1: // pie chart
  562. $tsize=$memcacheStats['limit_maxbytes'];
  563. $avail=$tsize-$memcacheStats['bytes'];
  564. $x=$y=$size/2;
  565. $angle_from = 0;
  566. $fuzz = 0.000001;
  567. foreach($memcacheStatsSingle as $serv=>$mcs) {
  568. $free = $mcs['STAT']['limit_maxbytes']-$mcs['STAT']['bytes'];
  569. $used = $mcs['STAT']['bytes'];
  570. if ($free>0){
  571. // draw free
  572. $angle_to = ($free*360)/$tsize;
  573. $perc =sprintf("%.2f%%", ($free *100) / $tsize) ;
  574. fill_arc($image,$x,$y,$size,$angle_from,$angle_from + $angle_to ,$col_black,$col_green,$perc);
  575. $angle_from = $angle_from + $angle_to ;
  576. }
  577. if ($used>0){
  578. // draw used
  579. $angle_to = ($used*360)/$tsize;
  580. $perc =sprintf("%.2f%%", ($used *100) / $tsize) ;
  581. fill_arc($image,$x,$y,$size,$angle_from,$angle_from + $angle_to ,$col_black,$col_red, '('.$perc.')' );
  582. $angle_from = $angle_from+ $angle_to ;
  583. }
  584. }
  585. break;
  586. case 2: // hit miss
  587. $hits = ($memcacheStats['get_hits']==0) ? 1:$memcacheStats['get_hits'];
  588. $misses = ($memcacheStats['get_misses']==0) ? 1:$memcacheStats['get_misses'];
  589. $total = $hits + $misses ;
  590. fill_box($image, 30,$size,50,-$hits*($size-21)/$total,$col_black,$col_green,sprintf("%.1f%%",$hits*100/$total));
  591. fill_box($image,130,$size,50,-max(4,($total-$hits)*($size-21)/$total),$col_black,$col_red,sprintf("%.1f%%",$misses*100/$total));
  592. break;
  593. }
  594. header("Content-type: image/png");
  595. imagepng($image);
  596. exit;
  597. }
  598. echo getHeader();
  599. echo getMenu();
  600. switch ($_GET['op']) {
  601. case 1: // host stats
  602. $phpversion = phpversion();
  603. $memcacheStats = getMemcacheStats();
  604. $memcacheStatsSingle = getMemcacheStats(false);
  605. $mem_size = $memcacheStats['limit_maxbytes'];
  606. $mem_used = $memcacheStats['bytes'];
  607. $mem_avail= $mem_size-$mem_used;
  608. $startTime = time()-array_sum($memcacheStats['uptime']);
  609. $curr_items = $memcacheStats['curr_items'];
  610. $total_items = $memcacheStats['total_items'];
  611. $hits = ($memcacheStats['get_hits']==0) ? 1:$memcacheStats['get_hits'];
  612. $misses = ($memcacheStats['get_misses']==0) ? 1:$memcacheStats['get_misses'];
  613. $sets = $memcacheStats['cmd_set'];
  614. $req_rate = sprintf("%.2f",($hits+$misses)/($time-$startTime));
  615. $hit_rate = sprintf("%.2f",($hits)/($time-$startTime));
  616. $miss_rate = sprintf("%.2f",($misses)/($time-$startTime));
  617. $set_rate = sprintf("%.2f",($sets)/($time-$startTime));
  618. echo <<< EOB
  619. <div class="info div1"><h2>General Cache Information</h2>
  620. <table cellspacing=0><tbody>
  621. <tr class=tr-1><td class=td-0>PHP Version</td><td>$phpversion</td></tr>
  622. EOB;
  623. echo "<tr class=tr-0><td class=td-0>Memcached Host". ((count($MEMCACHE_SERVERS)>1) ? 's':'')."</td><td>";
  624. $i=0;
  625. if (!isset($_GET['singleout']) && count($MEMCACHE_SERVERS)>1){
  626. foreach($MEMCACHE_SERVERS as $server){
  627. echo ($i+1).'. <a href="'.$PHP_SELF.'&singleout='.$i++.'">'.$server.'</a><br/>';
  628. }
  629. }
  630. else{
  631. echo '1.'.$MEMCACHE_SERVERS[0];
  632. }
  633. if (isset($_GET['singleout'])){
  634. echo '<a href="'.$PHP_SELF.'">(all servers)</a><br/>';
  635. }
  636. echo "</td></tr>\n";
  637. echo "<tr class=tr-1><td class=td-0>Total Memcache Cache</td><td>".bsize($memcacheStats['limit_maxbytes'])."</td></tr>\n";
  638. echo <<<EOB
  639. </tbody></table>
  640. </div>
  641. <div class="info div1"><h2>Memcache Server Information</h2>
  642. EOB;
  643. foreach($MEMCACHE_SERVERS as $server){
  644. echo '<table cellspacing=0><tbody>';
  645. echo '<tr class=tr-1><td class=td-1>'.$server.'</td><td><a href="'.$PHP_SELF.'&server='.array_search($server,$MEMCACHE_SERVERS).'&op=6">[<b>Flush this server</b>]</a></td></tr>';
  646. echo '<tr class=tr-0><td class=td-0>Start Time</td><td>',date(DATE_FORMAT,$memcacheStatsSingle[$server]['STAT']['time']-$memcacheStatsSingle[$server]['STAT']['uptime']),'</td></tr>';
  647. echo '<tr class=tr-1><td class=td-0>Uptime</td><td>',duration($memcacheStatsSingle[$server]['STAT']['time']-$memcacheStatsSingle[$server]['STAT']['uptime']),'</td></tr>';
  648. echo '<tr class=tr-0><td class=td-0>Memcached Server Version</td><td>'.$memcacheStatsSingle[$server]['STAT']['version'].'</td></tr>';
  649. echo '<tr class=tr-1><td class=td-0>Used Cache Size</td><td>',bsize($memcacheStatsSingle[$server]['STAT']['bytes']),'</td></tr>';
  650. echo '<tr class=tr-0><td class=td-0>Total Cache Size</td><td>',bsize($memcacheStatsSingle[$server]['STAT']['limit_maxbytes']),'</td></tr>';
  651. echo '</tbody></table>';
  652. }
  653. echo <<<EOB
  654. </div>
  655. <div class="graph div3"><h2>Host Status Diagrams</h2>
  656. <table cellspacing=0><tbody>
  657. EOB;
  658. $size='width='.(GRAPH_SIZE+50).' height='.(GRAPH_SIZE+10);
  659. echo <<<EOB
  660. <tr>
  661. <td class=td-0>Cache Usage</td>
  662. <td class=td-1>Hits &amp; Misses</td>
  663. </tr>
  664. EOB;
  665. echo
  666. graphics_avail() ?
  667. '<tr>'.
  668. "<td class=td-0><img alt=\"\" $size src=\"$PHP_SELF&IMG=1&".(isset($_GET['singleout'])? 'singleout='.$_GET['singleout'].'&':'')."$time\"></td>".
  669. "<td class=td-1><img alt=\"\" $size src=\"$PHP_SELF&IMG=2&".(isset($_GET['singleout'])? 'singleout='.$_GET['singleout'].'&':'')."$time\"></td></tr>\n"
  670. : "",
  671. '<tr>',
  672. '<td class=td-0><span class="green box">&nbsp;</span>Free: ',bsize($mem_avail).sprintf(" (%.1f%%)",$mem_avail*100/$mem_size),"</td>\n",
  673. '<td class=td-1><span class="green box">&nbsp;</span>Hits: ',$hits.sprintf(" (%.1f%%)",$hits*100/($hits+$misses)),"</td>\n",
  674. '</tr>',
  675. '<tr>',
  676. '<td class=td-0><span class="red box">&nbsp;</span>Used: ',bsize($mem_used ).sprintf(" (%.1f%%)",$mem_used *100/$mem_size),"</td>\n",
  677. '<td class=td-1><span class="red box">&nbsp;</span>Misses: ',$misses.sprintf(" (%.1f%%)",$misses*100/($hits+$misses)),"</td>\n";
  678. echo <<< EOB
  679. </tr>
  680. </tbody></table>
  681. <br/>
  682. <div class="info"><h2>Cache Information</h2>
  683. <table cellspacing=0><tbody>
  684. <tr class=tr-0><td class=td-0>Current Items(total)</td><td>$curr_items ($total_items)</td></tr>
  685. <tr class=tr-1><td class=td-0>Hits</td><td>{$hits}</td></tr>
  686. <tr class=tr-0><td class=td-0>Misses</td><td>{$misses}</td></tr>
  687. <tr class=tr-1><td class=td-0>Request Rate (hits, misses)</td><td>$req_rate cache requests/second</td></tr>
  688. <tr class=tr-0><td class=td-0>Hit Rate</td><td>$hit_rate cache requests/second</td></tr>
  689. <tr class=tr-1><td class=td-0>Miss Rate</td><td>$miss_rate cache requests/second</td></tr>
  690. <tr class=tr-0><td class=td-0>Set Rate</td><td>$set_rate cache requests/second</td></tr>
  691. </tbody></table>
  692. </div>
  693. EOB;
  694. break;
  695. case 2: // variables
  696. $m=0;
  697. $cacheItems= getCacheItems();
  698. $items = $cacheItems['items'];
  699. $totals = $cacheItems['counts'];
  700. $maxDump = MAX_ITEM_DUMP;
  701. foreach($items as $server => $entries) {
  702. echo <<< EOB
  703. <div class="info"><table cellspacing=0><tbody>
  704. <tr><th colspan="2">$server</th></tr>
  705. <tr><th>Slab Id</th><th>Info</th></tr>
  706. EOB;
  707. foreach($entries as $slabId => $slab) {
  708. $dumpUrl = $PHP_SELF.'&op=2&server='.(array_search($server,$MEMCACHE_SERVERS)).'&dumpslab='.$slabId;
  709. echo
  710. "<tr class=tr-$m>",
  711. "<td class=td-0><center>",'<a href="',$dumpUrl,'">',$slabId,'</a>',"</center></td>",
  712. "<td class=td-last><b>Item count:</b> ",$slab['number'],'<br/><b>Age:</b>',duration($time-$slab['age']),'<br/> <b>Evicted:</b>',((isset($slab['evicted']) && $slab['evicted']==1)? 'Yes':'No');
  713. if ((isset($_GET['dumpslab']) && $_GET['dumpslab']==$slabId) && (isset($_GET['server']) && $_GET['server']==array_search($server,$MEMCACHE_SERVERS))){
  714. echo "<br/><b>Items: item</b><br/>";
  715. $items = dumpCacheSlab($server,$slabId,$slab['number']);
  716. // maybe someone likes to do a pagination here :)
  717. $i=1;
  718. foreach($items['ITEM'] as $itemKey=>$itemInfo){
  719. $itemInfo = trim($itemInfo,'[ ]');
  720. echo '<a href="',$PHP_SELF,'&op=4&server=',(array_search($server,$MEMCACHE_SERVERS)),'&key=',base64_encode($itemKey).'">',$itemKey,'</a>';
  721. if ($i++ % 10 == 0) {
  722. echo '<br/>';
  723. }
  724. elseif ($i!=$slab['number']+1){
  725. echo ',';
  726. }
  727. }
  728. }
  729. echo "</td></tr>";
  730. $m=1-$m;
  731. }
  732. echo <<<EOB
  733. </tbody></table>
  734. </div><hr/>
  735. EOB;
  736. }
  737. break;
  738. break;
  739. case 4: //item dump
  740. if (!isset($_GET['key']) || !isset($_GET['server'])){
  741. echo "No key set!";
  742. break;
  743. }
  744. // I'm not doing anything to check the validity of the key string.
  745. // probably an exploit can be written to delete all the files in key=base64_encode("\n\r delete all").
  746. // somebody has to do a fix to this.
  747. $theKey = htmlentities(base64_decode($_GET['key']));
  748. $theserver = $MEMCACHE_SERVERS[(int)$_GET['server']];
  749. list($h,$p) = explode(':',$theserver);
  750. $r = sendMemcacheCommand($h,$p,'get '.$theKey);
  751. echo <<<EOB
  752. <div class="info"><table cellspacing=0><tbody>
  753. <tr><th>Server<th>Key</th><th>Value</th><th>Delete</th></tr>
  754. EOB;
  755. echo "<tr><td class=td-0>",$theserver,"</td><td class=td-0>",$theKey,
  756. " <br/>flag:",$r['VALUE'][$theKey]['stat']['flag'],
  757. " <br/>Size:",bsize($r['VALUE'][$theKey]['stat']['size']),
  758. "</td><td>",chunk_split($r['VALUE'][$theKey]['value'],40),"</td>",
  759. '<td><a href="',$PHP_SELF,'&op=5&server=',(int)$_GET['server'],'&key=',base64_encode($theKey),"\">Delete</a></td>","</tr>";
  760. echo <<<EOB
  761. </tbody></table>
  762. </div><hr/>
  763. EOB;
  764. break;
  765. case 5: // item delete
  766. if (!isset($_GET['key']) || !isset($_GET['server'])){
  767. echo "No key set!";
  768. break;
  769. }
  770. $theKey = htmlentities(base64_decode($_GET['key']));
  771. $theserver = $MEMCACHE_SERVERS[(int)$_GET['server']];
  772. list($h,$p) = explode(':',$theserver);
  773. $r = sendMemcacheCommand($h,$p,'delete '.$theKey);
  774. echo 'Deleting '.$theKey.':'.$r;
  775. break;
  776. case 6: // flush server
  777. $theserver = $MEMCACHE_SERVERS[(int)$_GET['server']];
  778. $r = flushServer($theserver);
  779. echo 'Flush '.$theserver.":".$r;
  780. break;
  781. }
  782. echo getFooter();
  783. ?>

在memcache_tools.php文件中设置memcached服务器地址与相关信息:

  1. ADMIN_USERNAME root #22行,登录memcache_tool的默认用户名
  2. ADMIN_PASSWORD 123456 #23行,登录memcache_tool的默认密码
  3. $MEMCACHE_SERVERS[] ='Memcached服务器地址:11211' #配置memcached服务器地址和端口

登录后可查看memcached服务器的相关信息:

memcached中存储的键值对:

5、Memcached缓存失效机制:

(1) Lazy Expiration(惰性过期)

在Memcached中,惰性过期是一种节省CPU资源的策略。Memcached不会在后台主动检查每个键是否过期,而是当客户端尝试获取一个键时,Memcached会检查该键的过期时间戳,如果该键已经过期,那么它不会被返回给客户端,并从缓存中删除。

(2) Least Recently Used(LRU 替换策略)

LRU策略是一种常用的缓存替换策略,当Memcached的内存空间不足以存储新数据时,它根据数据项最后一次被访问的时间来做出决策。即最近最少使用的数据项将被删除,并将其空间分配给新的记录。

四、session入库

1、为什么需要session入库:

由于负载均衡需要对用户的请求进行分发操作,每个服务器有自己的Session文件存储,如果用户的请求被分发到了之前没有处理过该用户的服务器,那么该服务器就无法找到对应的Session数据,从而导致Session失效。

解决方案:

使用负载均衡算法:如Nginx的ip_hash算法,确保来自同一用户的请求总是被分发到同一台服务器上。

把Session入库:将所有服务器的Session数据集中存储在一个共享的位置,例如数据库或分布式缓存系统。这样,无论用户的请求被分发到哪个服务器,都可以从共享的位置获取到Session数据。

2、DSShop商城系统中的Session入库:

(1) 修改lB负载均衡器配置文件:

先注释掉在第五节配置过的ip_hash,使负载均衡算法为轮询。

(2) 需要配置session入库的文件:

在需要进行登录操作的界面配置session入库。

home文件夹:用户的前端界面,用户登录;

admin文件夹:管理员的后端界面,管理员登录。

(3) 修改文件:

① 前台:修改/home/下的config.php

vim /home/www/application/home/config.php

添加驱动方式和memcached服务器地址:

查看前端界面的session缓存:

前端界面登录信息的session缓存:

② 后台:修改/admin/下的config.php

vim /home/www/application/admin/config.php

修改方式与前台类似:

● 后台除了修改/admin/config.php文件之外,还需要修改总的配置文件,才能使session共享。

vim /home/www/application/config.php

登录后台:

后台登录信息的session缓存:

五、memcached单机多实例:

在一台机器上运行多个memcached实例,每个memcached设置单独的端口号。

/usr/local/memcached/bin /memcached -uroot -d -p 11211

/usr/local/memcached/bin /memcached -uroot -d -p 11212

/usr/local/memcached/bin /memcached -uroot -d -p 11213

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/399351
推荐阅读
相关标签
  

闽ICP备14008679号