更改为:
session.save_handler=memcache
session.save_path=”tcp://127.0.0.1:11211?persistent=1&weight=1&timeout=1&retry_interval=15″
如果memcahced使用的是公网服务器的话最好使用其公网地址
保存退出并重启服务
[root@testhtdocs]#/etc/init.d/php-fpmrestart
Gracefullyshuttingdownphp-fpm.done
Startingphp-fpmdone
创建测试页setsession.php
[root@testhtdocs]#catsetsession.php
<?php
session_start();
if(!isset($_SESSION[‘test’])){
$_SESSION[‘test’]=time();
}
print$_SESSION[‘test’];
print”<br><br>”;
print”SessionID:”.session_id();
?>
新建php页面showsess.php,获取当前用户的会话ID:
[root@testhtdocs]#catshowsess.php
<?php
session_start();
$memcache_obj=newMemcache;
$memcache_obj->connect(‘127.0.0.1’,11211);
$mysess=session_id();
var_dump($memcache_obj->get($mysess));
$memcache_obj->close();
?>
而后找一其他主机,使用负载均衡轮询到不同的主机上,可以发现无论刷新至哪个页面其用户的session是一样的
访问setsession.php
1399775256#为获取时间SessionID:9a0itlgjlurghq83ibvmol5pc7#为session的id号
获取session
可以看到其时间是与上面setsession是一样的
总结
这样一来比php将session保存在本地效率要快很多,,如果以后要使用多台memcached的话至于开发程序能否实现负载均衡,到底是使用取余的算法还是使用一致性哈希的算法完全要看开发人员的决定了
感谢各位。
本文出自 “心情依旧” 博客,请务必保留此出处
要永不言弃坚持到底百折不挠宁死不屈,但我们好多人没想过,