时间:2021-07-01 10:21:17 帮助过:8人阅读
1、模拟web集群
我启动了二个memcached进程,分别模拟二台服务器
/usr/local/bin/memcached -d -m 20 -u zhangy -p 12000 -P ./memcached.pid
/usr/local/bin/memcached -d -m 20 -u zhangy -p 13000 -P ./mem.pid
2、修改php的配置
vi /usr/local/php/lib/php.ini
session.save_handler = "memcache"
memcache.hash_strategy = "consistent"
session.save_path = "tcp://127.0.0.1:13000?weight=10,tcp://127.0.0.1:12000"
说明:第一行,session的储存方式是memcache;第二行,memcache的hash算法是consistent;第三行,session储存的地位;
3、重启apache
查看phpinfo
session
Session Support | enabled |
Registered save handlers | files user sqlite memcache |
Registered serializer handlers | php php_binary |
紧接着下面是:
session.save_path | tcp://127.0.0.1:13000,tcp://127.0.0.1:12000 | tcp://127.0.0.1:13000,tcp://127.0.0.1:12000 |
4、做个简单测试如下:
a)、准备文件session.php
<?php session_start(); $_SESSION['username'] = "abcabc"; echo session_id(); ?>
b)、显示session内容文件
<?php $mem = new Memcache; $mem->addServer("127.0.0.1",12000)or die ("Could not add server 12000"); $mem->addServer("127.0.0.1",13000)or die ("Could not add server 13000"); $val = $mem->get('qp0mrob2ovcqle3u4lbr4obsa5'); //echo session_id(); 得到的session id echo $val; ?>
http://www.bkjia.com/PHPjc/824607.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/824607.htmlTechArticle使用memcache来同步session是还是不错的,当然也可以通过redis来保存session,可以php开启并将Session存储到Redis缓存,下面是设置利用memcache在w...