addServer('127.0.0.1',11211); $mem->addServer('127.0.0.1',11212); $session_id = session_id(); echo ""; print_r(@$mem->get($session_id)); /*name|s:6:"tangqi";name0|s:7:"tangqi0";name1|s:7:"tangqi1";name2|s:7:"tangqi2";name3|s:7:"tangqi3";name4|s:7:"tangqi4";name5|s:7:"tangqi5";name6|s:7:"tangqi6";name7|s:7:"tangqi7";name8|s:7:"tangqi8";name9|s:7:"tangqi9";*/ 2.//配置好php.ini ,让session存到Redis里面,同时编写PHP程序来测试是否ok。 ini_set('session.save_handler','redis'); ini_set('session.save_path','tcp://127.0.0.1:6379'); session_start(); $_SESSEION['name'] = 'tangqi'; $redis = new Redis(); $redis->connect('127.0.0.1',6379); $session_id = session_id(); echo $redis->get("name"); 3.//配置Redis主从复制 //修改redis.conf redis-server --port 6378 --slaveof 127.0.0.1 6379 4.//参考课件当中的访问频率控制功能,实现一个自己PHP访问频率控制类。并发现目前负责的相关项目,哪些地方可以用上频率限制的功能。 function ratelimit($option) { if(!is_array($option)){ return false; } $mem = new Mencache(); $men->connect('127.0.0.1',11211); $ip = $_SERVER['REMOTE_ADDR']; $ip = sprintf("%u", ip2long($ip)); $now = time(); $data = $mem->get("rateLimit:$ip"); foreach($option as $minutes=>$limitNumber){ if($minutes && $limitNumber && isset($data[$minutes])) { $data[$minutes] = array_slice($data[$minutes], -$limitNumber); $timestamps = $data[$minutes]; if(count($timestamps) >= $limitNumber && ($now - $timestamps[0] <= $minutes * 60) ){ return $minutes; }else{ array_push($data[$minutes], $now); } }else{ $data[$minutes] = array($now); } } $mem->set("rateLimit:$ip", $data, 0, 3600); $mem->close(); return 0; } $result = rateLimit(array( '1' => 3, '10' => 5, '120' => 10, '1440' => 20, )); if ($result) { echo "$result, ERROR"; } else { echo 'OK'; }