memcache php 长连接

本文探讨了Memcache使用Memcache::addServer()导致的长连接问题,指出错误在于未正确理解默认持久连接特性。文章详细解释了Memcache::connect()与Memcache::pconnect()的区别,强调了在PHP中避免使用持久连接的重要性,以防止资源消耗过大的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于框架提供的memcachewrapper内部对memcache使用方法有误,导致长连接过多,(线上四台web机器每台有4000链接连在了两台memcache服务器上)

具体bug说明参见 [url]https://bugs.php.net/bug.php?id=60460[/url]
[quote]
Memcache::addServer() does a *presistent* connection by default, if you do not
specify it to be different.

memcache::close() function doesn't close persistent connections, which are closed
only during web-server shutdown/restart.

so this is not a bug at all, but a coding error.
[/quote]
亦即问题是错误的使用Memcache::addServer所致。调用addserver时,如果设置persistent为true,则此链接将不会关闭,直至web server重启;即使显示调用close也没用。而php为短执行进程,执行完毕退出后新执行脚本又发起addServer的新链接,从而导致链接数不停增加。

使用Memcache::connect则不会出现此问题,其实也就是persistent=false的形式。

对应的还可以参考
[quote]
Memcache::pconnect()is similar to Memcache::connect() with the difference, that the connection it establishes is persistent. This connection is not closed after the end of script execution and by Memcache::close() function.
[/quote]
也是长连接
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值