Home >Backend Development >PHP Tutorial >采用memcache在web集群中实现session的同步会话_php技巧

采用memcache在web集群中实现session的同步会话_php技巧

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-17 08:39:271025browse

使用memcache来同步session是还是不错的,当然也可以通过redis来保存session,可以php开启并将Session存储到Redis缓存,下面是设置利用memcache在web集群中同步会话session的实现过程:

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

<&#63;php 
session_start(); 
$_SESSION['username'] = "abcabc"; 
echo session_id(); 
&#63;>

b)、显示session内容文件

<&#63;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; 
&#63;>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn