PHP扩展模块memcached长连接使用方法分析,扩展模块memcached
PHP扩展模块memcached长连接使用方法分析,扩展模块memcached
网上广泛流传着一篇文章,讲述php的两个扩展模块memcache和memcached的区别,其中特意强调了memcached与memcached一个很大的区别是memcached模块不支持长连接。以至于后来很多年我都认为memcached是不支持长连接的,其实不然,memcached扩展模块从很早的版本开始就已经支持长连接了。从扩展模块的源码注视中我们就能看到:
/* {{{ 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)
{
从php的手册身上我们可以看到memcached的扩展模块提供的构造函数提供一个参数persistent_id
可选项,手册中这样介绍:
默认情况下,Memcached实例在请求结束后会被销毁。但可以在创建时通过persistent_id
为每个实例指定唯一的ID, 在请求间共享实例。所有通过相同的persistent_id
值创建的实例共享同一个连接。
这个参数的含义就是说如果你传递了一个命名id给到构造方法,那么就会建立长连接,通常我们使用的都是PHP-FPM模式,这样PHP-FPM进程就会和memcached服务简历一条长连接通道。我们也可以理解为persistent_id就是一个连接池名字,所有php-fpm进程都是这个连接池中的一员。
但我们需要注意的是php是解释性语言,当php第一次通过memached模块建立起长连接后,切记后续的php执行就不要再通过memcached的构造函数构建相同persistent_id命名的长连接,可以建立不同persistent_id名字的长连接,如果是相同的名字被php重复执行,一定会导致php-fpm的进程异常导致与memcached的通信越来越慢,同时根据libmemcached的版本不同还会导致php产生coredump。
那么我们如何避免单个php-fpm在建立完以persistent_id命名的长连接后不再重复建立长连接呢?其实在PHP带有评注的手册上是有讲解的,内容如下:
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 />
通过使用getServerList()方法来检查当前执行使用的php-fpm进程容器中是否已经存在相同名字的长连接资源,如果存在就不要重复使用addServers() 方法来新增长连接配置。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



Memcached is a commonly used caching technology that can greatly improve the performance of web applications. In PHP, the commonly used Session processing method is to store the Session file on the server's hard disk. However, this method is not optimal because the server's hard disk will become one of the performance bottlenecks. The use of Memcached caching technology can optimize Session processing in PHP and improve the performance of Web applications. Session in PHP

Swoole and Workerman's optimization method for long connections and persistent connections between PHP and MySQL requires specific code examples. With the development of web applications and the increase in user scale, database queries have become one of the focuses of application performance optimization. In PHP development, commonly used database connection methods include long connections and short connections. A long connection refers to maintaining the connection state after establishing a database connection and reusing the same connection multiple times; while a short connection means closing the connection after each query is completed. In PHP, the traditional My

Caching library in PHP8.0: Memcached With the rapid development of the Internet, modern applications require efficient and reliable caching technology to improve performance and handle large amounts of data. Due to PHP's popularity and open source nature, the PHP caching library has become an essential tool in the web development community. Memcached is a widely used open source high-speed memory caching system that can handle millions of simultaneous connected cache requests and can be used in many different types of applications, such as social networks, online

With the development of the Internet, PHP applications have become more and more common in the field of Internet applications. However, high concurrent access by PHP applications can lead to high CPU usage on the server, thus affecting the performance of the application. In order to optimize the performance of PHP applications, Memcached caching technology has become a good choice. This article will introduce how to use Memcached caching technology to optimize the CPU usage of PHP applications. Introduction to Memcached caching technology Memcached is a

With the rapid development of the Internet, large-scale MySQL database backup and recovery has become one of the essential skills for major enterprises and websites. With the widespread application of Memcached, how to back up and restore Memcached has also become an important issue. As one of the main languages for web development, PHP has unique advantages and skills in handling backup and recovery of MySQL and Memcached. This article will introduce in detail the implementation method of PHP processing MySQL and Memcached backup and recovery.

With the rapid development of modern Internet applications, user experience is crucial to the success of an application. How to ensure high performance and high availability of applications has become one of the important issues that developers need to solve. As one of the widely used programming languages, PHP's performance monitoring and optimization are also very important. Memcached is a high-performance, distributed memory object caching system that can help applications improve performance and scalability. This article will introduce how to use PHP and Memcached to implement performance monitoring.

With the continuous increase of network applications and the continuous expansion of data volume, data reading and writing efficiency has become one of the important factors affecting application performance. The application of caching technology can solve this problem well. In PHP applications, Memcached is the most commonly used cache server. Memcached is a high-performance distributed memory object caching system that can store commonly used data in memory and improve the efficiency of data retrieval. This article will introduce how to use PHP and Memcached for cache management, and how to optimize

With the continuous development of Internet technology, audio and video resources have become a very important form of content on the Internet, and PHP, as one of the most widely used languages in network development, is also constantly used in the field of video and audio playback. However, with the increasing number of users of audio and video websites, many websites have discovered a problem: under high concurrency conditions, PHP's processing speed of audio and video slows down significantly, resulting in problems such as inability to play in time or stuck playback. To solve this problem, Memcached caching technology should
