


Analysis on how to use long connection of PHP extension module memcached, extension module memcached
There is an article widely circulated on the Internet about the difference between memcache and memcached, the two extension modules of PHP. It specifically emphasizes that a big difference between memcached and memcached is that the memcached module does not support long connections. So for many years I thought that memcached did not support long connections. In fact, this is not the case. The memcached extension module has supported long connections since a very early version. We can see from the source code of the extension module:
/* {{{ Memcached::__construct([string persistent_id[, callback on_new[, string connection_str]]]))
Creates a Memcached object, optionally using persistent memcache connection */
static PHP_METHOD(Memcached, __construct)
{
From the PHP manual, we can see that the constructor provided by the memcached extension module provides an optional parameter persistent_id
, which is introduced in the manual:
By default, Memcached instances will be destroyed after the request ends. However, you can share instances between requests by specifying a unique ID for each instance by persistent_id
when creating it. All instances created with the same persistent_id
value share the same connection.
The meaning of this parameter is that if you pass a named id to the constructor, then a long connection will be established. Usually we use PHP-FPM mode, so that the PHP-FPM process will resume the memcached service A long connection channel. We can also understand that persistent_id is a connection pool name, and all php-fpm processes are members of this connection pool.
But what we need to pay attention to is that PHP is an interpreted language. When PHP establishes a long connection through the memached module for the first time, remember that subsequent PHP executions should not build long connections named with the same persistent_id through the constructor of memcached. Long connections with different persistent_id names can be established. If the same name is executed repeatedly by PHP, it will definitely cause the php-fpm process to be abnormal and cause the communication with memcached to become slower and slower. At the same time, depending on the version of libmemcached, it will also cause PHP to generate coredump. .
So how do we prevent a single php-fpm from repeatedly establishing a long connection after establishing a long connection named with persistent_id? In fact, it is explained in the PHP manual with comments, and the content is as follows:
When using persistent connections, it is important to not re-add servers.
This is what you do not want to do:
$mc = new Memcached('mc'); $mc->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true); $mc->addServers(array( array('mc1.example.com',11211), array('mc2.example.com',11211), ));
Every time the page is loaded those servers will be appended to the list resulting in many simultaneous open connections to the same server. The addServer/addServers functions to not check for existing references to the specified servers.
A better approach is something like:
$mc = new Memcached('mc'); $mc->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true); if (!count($mc->getServerList())) { $mc->addServers(array( array('mc1.example.com',11211), array('mc2.example.com',11211), )); }<br />
Use the getServerList() method to check whether a long connection resource with the same name already exists in the php-fpm process container currently used for execution. If it exists, do not repeatedly use the addServers() method to add a new connection configuration.

Memcached是一种常用的缓存技术,它可以使Web应用程序的性能得到很大的提升。在PHP中,常用的Session处理方式是将Session文件存放在服务器的硬盘上。但是,这种方式并不是最优的,因为服务器的硬盘会成为性能瓶颈之一。而使用Memcached缓存技术可以对PHP中的Session处理进行优化,提高Web应用程序的性能。PHP中的Session处

PHP8.0中的缓存库:Memcached随着互联网的快速发展,现代应用程序需要高效可靠的缓存技术来提高性能和处理大量数据。由于PHP的流行和开源特性,PHP缓存库已经成为了Web开发社区的一个必备工具。Memcached是一种广泛使用的开源高速内存缓存系统,它能处理数百万个同时连接的缓存请求,可以用于许多不同类型的应用程序,例如社交网络、在线

随着互联网技术的不断发展,音视频资源已经成为了互联网上非常重要的一种内容形式,而PHP作为网络开发中使用最广泛的语言之一,也在不断地应用于视频和音频播放领域。然而,随着音视频网站的用户日益增加,许多网站已经发现了一个问题:在高并发的情况下,PHP对于音视频的处理速度明显变缓,会导致无法及时播放或者播放卡顿等问题。为了解决这个问题,Memcached缓存技术应

随着互联网的快速发展,大规模MySQL数据库备份和恢复成为各大企业和网站必备的技能之一。而随着Memcached的广泛应用,如何备份和恢复Memcached也成为了一个重要的问题。PHP作为Web开发的主力语言之一,在处理备份和恢复MySQL和Memcached上拥有独特的优势和技巧。本文将详细介绍PHP处理MySQL和Memcached备份与恢复的实现方法

随着网络应用的不断增加和数据量的不断膨胀,数据的读写效率成为影响应用性能的重要因素之一。而缓存技术的应用则可以很好地解决这个问题。在PHP应用中,Memcached是最常用的缓存服务器。Memcached是一个高性能的分布式内存对象缓存系统,可以将常用的数据存储在内存中,提高数据检索的效率。本文将介绍如何使用PHP和Memcached进行缓存管理,以及如何优

随着现代互联网应用的快速发展,用户体验对于一个应用的成功至关重要。如何保证应用的高性能和高可用性,成为了开发人员需要解决的重要问题之一。PHP作为一种广泛应用的编程语言之一,它的性能监控和优化也是非常重要的。Memcached是一个高性能、分布式的内存对象缓存系统,可以帮助应用提高性能和扩展性。本文将介绍如何使用PHP和Memcached实现性能监控的方法。

随着互联网的发展,缓存技术在Web开发中扮演着越来越重要的角色。Redis和Memcached作为两种流行的缓存服务器,被广泛应用于各种Web应用开发中。然而,对于不熟悉Linux系统的开发人员来说,安装和配置这些缓存服务器可能会带来一些麻烦。但是,在宝塔面板的帮助下,这一过程将变得相当简单。一、什么是宝塔面板?宝塔面板是一款Linux服务器管理面板,它可以

Memcached是一种分布式内存缓存技术,被广泛应用于Web应用程序中。它可以帮助Web应用程序提高性能,减少数据库负载,加速数据访问,从而提高用户体验。在PHP中,使用Memcached技术也变得越来越流行。本文将介绍在PHP中如何使用Memcached技术。一、什么是Memcached技术Memcached是一种基于内存的缓存技术,它可以将数据缓存在内


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools
