搜索
首页后端开发php教程php如何将session保存到memcached中?如何分布式保存php session

session_set_save_handler无关的memcached保存session的方法

在memcached服务器上

1)下载memcached

#wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz

2)由于memcached依赖libevent所以需要先安装libevent库,这里直接yum安装

#yum install *libevent*

3)安装memcached

#./configure --prefix=/usr/local/memcached
#make
#make install

4)启动memcached

#/usr/local/memcached/bin/memcached -d -m 4096 -p 11211 -u root
-d daemon ?-p port -u ?user -m memory

在web server服务器上

5)在web server上安装php的memcache模块

#/usr/local/php/bin/pecl install memcache

Enable memcache session handler support? [yes] : yes(这里选择yes)

6)在php.ini中加入如下内容:

extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/memcache.so

7)修改php.ini中的session.save_handler及session.save_path为如下内容:

session.save_handler = memcache
session.save_path = "tcp://memcached服务器ip:11211"

亦可在PHP程序中

ini_set('session.save_handler', 'memcache');
ini_set('session.save_path', 'tcp://memcached服务器ip:11211');

注意:这种使用memcached保存session的方式与session_set_save_handler无关

 安装完memcached之后

在php.ini中

将session.save_handler 修改为memcache,并修改save_path指向memcached的地址和端口即可

session.save_handler = memcache
session.save_path = tcp://127.0.0.1:11211

memcache的PECL这个扩展非常强大,可以支持failover以及分布存储。

使用方法很简单,只需要在session.save_path的参数列表中,使用逗号分隔各个memcached服务器,如:

session.save_path = "tcp://172.16.8.81:11211,tcp://172.16.8.82:11211,tcp://172.16.8.83:11211"

则保存的session会经过hash之后保存到各个memcached服务器中,而hash的算法memcache支持两种,crc32以及fnv:

memcache.hash_function= {crc32,fnv}

文档中很少有提到fnv算法的,据说其散列要比crc32更好,但是我通过以下小小的程序实验之后,发现仍旧是crc32的散列算法分布的更加平均。

<?php
ini_set("memcache.hash_function", "crc32");
$memcache = new Memcache;
$memcache1 = new Memcache;
$memcache2 = new Memcache;
$memcache->addServer(&#39;localhost&#39;, 11211);
$memcache->addServer(&#39;localhost&#39;, 11212);
$memcache->flush();
$memcache1->connect(&#39;localhost&#39;, 11211);
$memcache2->connect(&#39;localhost&#39;, 11212);
$fp1 = fopen("mem1.txt", "w");
$fp2 = fopen("mem2.txt", "w");
for ($i = 0; $i < 1000; $i++)
{
$memcache->set($i, $i, 0, 1000);
fwrite($fp1, $memcache1->get($i) . " ");
fwrite($fp2, $memcache2->get($i) . " ");
}
fclose($fp1);
fclose($fp2);

接着我就session的保存进行了测试

我开了3个memcached进程进行测试

<?php
ini_set("memcache.hash_function", "fnv");
ini_set("error_reporting", "E_CORE_ERROR");
$memcache1 = new Memcache;
$memcache1->connect(&#39;localhost&#39;, 11211);
$memcache1->flush();
$memcache2 = new Memcache;
$memcache2->connect(&#39;localhost&#39;, 11212);
$memcache2->flush();
$memcache3 = new Memcache;
$memcache3->connect(&#39;localhost&#39;, 11213);
$memcache3->flush();
$fp1 = fopen(&#39;mem1.txt&#39;, &#39;w&#39;);
$fp2 = fopen(&#39;mem2.txt&#39;, &#39;w&#39;);
$fp3 = fopen(&#39;mem3.txt&#39;, &#39;w&#39;);
for ($i = 0; $i < 1000; $i++)
{
session_start();
$ssid = session_id();
echo $ssid;
session_register("id");
$_SESSION["id"] = $ssid;
session_write_close();
fwrite($fp1, $memcache1->get($ssid) . &#39; &#39;);
fwrite($fp2, $memcache2->get($ssid) . &#39; &#39;);
fwrite($fp3, $memcache3->get($ssid) . &#39; &#39;);
//session_destroy();
}
fclose($fp1);
fclose($fp2);
fclose($fp3);

比较奇怪的是 memcached2一般都会不被选中,

而1,3的内容是一致的。可能是为了failover,

而当我把1,3关闭后,2中将会出现内容,说明memcached2是正常工作的。

而不论我的散列算法使用crc32还是fnv,这种现象都存在,

最后我发现:这个测试程序存在问题。

因为在session_write_close之后,整个程序的session都是唯一的了。

也就是虽然循环了这么多次,里面包含了session_destroy调用,但是返回的session id都是同样的。

这就导致了两个文件中的内容一致而另一个文件中没有内容,

基于此点,

我只能分次调用脚本,脚本修改如下:

<?php
ini_set("memcache.hash_strategy", "consistent");
ini_set("memcache.hash_function", "crc32");
ini_set("error_reporting", "E_CORE_ERROR");
ini_set("memcache.allow_failover", "0");
$memcache1 = new Memcache;
$memcache1->connect(&#39;localhost&#39;, 10001);
$memcache1->flush();
$memcache2 = new Memcache;
$memcache2->connect(&#39;localhost&#39;, 10002);
$memcache2->flush();
$memcache3 = new Memcache;
$memcache3->connect(&#39;localhost&#39;, 10003);
$memcache3->flush();
$fp1 = fopen("mem1.txt", "a+");
$fp2 = fopen("mem2.txt", "a+");
$fp3 = fopen("mem3.txt", "a+");
session_start();
$ssid = session_id();
echo $ssid . "\n";
session_register("id");
$_SESSION["id"] = $ssid;
//session_destroy();
session_write_close();
fwrite($fp1, $memcache1->get($ssid) . " ");
fwrite($fp2, $memcache2->get($ssid) . " ");
fwrite($fp3, $memcache3->get($ssid) . " ");
session_destroy();
fclose($fp1);
fclose($fp2);
fclose($fp3);

然后再shell中重复运行多次,返回的ID不同了。

再打开mem*.txt文件查看,

发现3个文件中,每个session会保存在其中两个文件,然后分布不同。

这证明了使用memcache来保存session,一个是做到了failover,第二会按照session id来做hash分布保存。


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP依赖注入容器:快速启动PHP依赖注入容器:快速启动May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依赖注入与服务定位器PHP中的依赖注入与服务定位器May 13, 2025 am 12:10 AM

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

PHP性能优化策略。PHP性能优化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP电子邮件验证:确保正确发送电子邮件PHP电子邮件验证:确保正确发送电子邮件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

如何使PHP应用程序更快如何使PHP应用程序更快May 12, 2025 am 12:12 AM

tomakephpapplicationsfaster,关注台词:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

PHP性能优化清单:立即提高速度PHP性能优化清单:立即提高速度May 12, 2025 am 12:07 AM

到ImprovephPapplicationspeed,关注台词:1)启用opcodeCachingwithapCutoredUcescriptexecutiontime.2)实现databasequerycachingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandredececonnection.4 limitsclection.4.4

PHP依赖注入:提高代码可检验性PHP依赖注入:提高代码可检验性May 12, 2025 am 12:03 AM

依赖注入(DI)通过显式传递依赖关系,显着提升了PHP代码的可测试性。 1)DI解耦类与具体实现,使测试和维护更灵活。 2)三种类型中,构造函数注入明确表达依赖,保持状态一致。 3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

PHP性能优化:数据库查询优化PHP性能优化:数据库查询优化May 12, 2025 am 12:02 AM

databasequeryOptimizationinphpinvolVolVOLVESEVERSEVERSTRATEMIESOENHANCEPERANCE.1)SELECTONLYNLYNESSERSAYCOLUMNSTORMONTOUMTOUNSOUDSATATATATATATATATATATRANSFER.3)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。