Home php教程 php手册 php memcache和php memcached比较以及问题

php memcache和php memcached比较以及问题

May 23, 2016 am 08:33 AM
memcache

php memcache和php memcached是php的memcache分布式的高速缓存系统的两个客户端,php memcache是老客户端,php memcached是功能更加完善的新的代替php memcached的.

php memcache独立用php实现,是老客户端,从我们实践中已发现有多个问题,而且功能少,属性也可设置的少;

php memcached是基于原生的c的libmemcached的扩展,更加完善,建议替换为php memcached.

1.Php memcache的问题

1.1 分布式问题

php memcache默认会自动切换实例,所以有时取到老数据,并且value飘忽不定.

网友分享的问题:

这几天做某个产品的时候遇到一个小问题,现象比较诡异,产品用了两台分布式的memcached服务器,某一个计数器取回来的数偶尔会不对,最后定位在php memcache client的failover机制上面.

我们知道,在memcached分布式环境下,某一个key是通过hash计算,分配到某一个memcached上面的.

如果php.ini里面 memcache.allow_failover = 1的时候,在分布式环境下,某一台memcached出问题的话,会自动到其他的memcached尝试,就会出现上面的问题,所以要设置 allow_failover = 0 那么取不到时就直接返回失败而不会从其它mc上取,这样以避免网络异常或server端异常时,经常切换实例,会取到老数据.

1.2 高并发下稳定性问题

新浪微博提到的教训:

php memcache换成php memcached,在高并发下稳定下极大提高;

另外功能更多,出错码更精确。

Twitter的缓存经验

多层次Cache,减轻某些cache节点宕掉后的影响,读写都cache;将memcached api统一换为libmemcached(方便多语言访问memcached,让分布式等各种规则都一致).

1.3 1秒超时间隔没法修改问题

php memcache客户端有个1秒超时间隔没法修改问题:

bool Memcache::connect(string $host [,int $port [,int $timeout ]])

第三个参数本来可设置timeout,单位秒,但无法修改.

测试了以下三种修改timeout的方法都无效:

1.3.1. 用memcache api Memcache::setServerParams不能修改;

1.3.2. 改memcache 源代码vi php_memcache.h宏定义不能修改;

1.3.3. php.ini内这个配置:default_socket_timeout = 60对本timeout无效。

2.memcache和memcached对比

Php memcache这个老客户端在属性设置方面可设置的很少,出错码粒度很粗,出错后难以定位,而且功能欠缺一些:

There are primarily two clients used with PHP. One is the older, more widespread pecl/memcache and the other is the newer, less used, more feature rich pecl/memcached. 
	Both support the basics such as multiple servers, setting vaules, getting values, increment, decrement and getting stats. 
	 
	Here are some more advanced features and information. 
	项目              pecl/memcache       pecl/memcached 
	First Release Date      2004-06-08      2009-01-29 (beta) 
	Actively Developed      Yes             Yes 
	External Dependency     None            libmemcached 
	Automatic Key Fixup1    Yes             No 
	Append/Prepend          No              Yes 
	Automatic Serialzation2 Yes             Yes 
	Binary Protocol         No              Optional 
	CAS                     No              Yes 
	Compression             Yes             Yes 
	Communication Timeout   Connect Only    Various Options 
	Consistent Hashing      Yes             Yes 
	Delayed Get             No              Yes 
	Multi-Get               Yes             Yes 
	Session Support         Yes             Yes 
	Set/Get to a specific server    No          Yes 
	Stores Numerics         Converted to Strings    Yes 
	 
	//注释: 
	 
	1 pecl/memcache will convert an invalid key into a valid key for you. pecl/memcached will return false when trying to set/get a key that is not valid. 
	2 You do not have to serialize your objects or arrays before sending them to the set commands. Both clients will do
Copy after login

本文地址:

转载随意,但请附上文章地址:-)

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Memcache in PHP development? How to use Memcache in PHP development? Nov 07, 2023 pm 12:49 PM

In web development, we often need to use caching technology to improve website performance and response speed. Memcache is a popular caching technology that can cache any data type and supports high concurrency and high availability. This article will introduce how to use Memcache in PHP development and provide specific code examples. 1. Install Memcache To use Memcache, we first need to install the Memcache extension on the server. In CentOS operating system, you can use the following command

How to use Memcache for efficient data reading and writing operations in PHP development? How to use Memcache for efficient data reading and writing operations in PHP development? Nov 07, 2023 pm 03:48 PM

In PHP development, using the Memcache caching system can greatly improve the efficiency of data reading and writing. Memcache is a memory-based caching system that can cache data in memory to avoid frequent reading and writing of the database. This article will introduce how to use Memcache in PHP for efficient data reading and writing operations, and provide specific code examples. 1. Install and configure Memcache First, you need to install the Memcache extension on the server. able to pass

How to use Memcache for efficient data writing and querying in PHP development? How to use Memcache for efficient data writing and querying in PHP development? Nov 07, 2023 pm 01:36 PM

How to use Memcache for efficient data writing and querying in PHP development? With the continuous development of Internet applications, the requirements for system performance are getting higher and higher. In PHP development, in order to improve system performance and response speed, we often use various caching technologies. One of the commonly used caching technologies is Memcache. Memcache is a high-performance distributed memory object caching system that can be used to cache database query results, page fragments, session data, etc. By storing data in memory

Utilizing Memcache caching technology to improve the concurrent processing capabilities of PHP applications Utilizing Memcache caching technology to improve the concurrent processing capabilities of PHP applications May 18, 2023 am 08:12 AM

With the rapid development of the Internet, more and more applications need to face a large number of concurrent requests. How to improve the concurrent processing capabilities of applications has become a problem that developers need to solve. Among them, using Memcache caching technology for concurrency optimization has become a relatively popular solution. Memcache is an efficient caching technology suitable for large-scale web applications, databases and distributed systems. Its characteristic is to store data in memory to achieve high-speed read and write operations. During the data access process of web applications,

How to use Memcache for distributed caching in PHP development? How to use Memcache for distributed caching in PHP development? Nov 07, 2023 pm 03:04 PM

As web applications become increasingly complex, performance has become a critical issue. In many applications, database queries are one of the most time-consuming operations. In order to avoid frequently reading data from the database, a caching system can be used to store frequently read data in memory for quick access. In PHP development, using Memcached for distributed caching is an extremely common practice. In this article we will introduce how to use Memcached for distributed caching. What is Memca

Practice and thinking on optimizing data interaction with Memcache caching technology in PHP Practice and thinking on optimizing data interaction with Memcache caching technology in PHP May 17, 2023 pm 09:51 PM

The practice and thinking of Memcache caching technology to optimize data interaction in PHP In modern Web applications, data interaction is a very important issue. It is not efficient enough and will limit the scalability and performance of Web applications. In order to speed up data interaction, our usual approach is to optimize the design of the database, improve the performance of the hardware and increase the server capacity. However, these methods all have a common limitation: they increase the cost of the system. In recent years, Memcache technology has made progress in solving this problem.

Application and practice of Memcache caching technology in PHP projects Application and practice of Memcache caching technology in PHP projects May 17, 2023 pm 02:10 PM

Memcache is an open source, distributed caching technology. It greatly improves the speed of data access by storing data in memory, thus improving the performance and responsiveness of the website. In PHP projects, Memcache caching technology is also widely used and has achieved good results. This article will deeply explore the application and practice of Memcache caching technology in PHP projects. 1. Principles and advantages of Memcache Memcache is a memory caching technology that can store data

How to use Memcache to optimize data storage operations in your PHP application? How to use Memcache to optimize data storage operations in your PHP application? Nov 08, 2023 pm 09:06 PM

How to use Memcache to optimize data storage operations in your PHP application? In web application development, data storage is a crucial link. In PHP applications, Memcache, as a memory cache system, can effectively improve the efficiency of data storage and reading operations. This article will introduce how to use Memcache to optimize data storage operations in PHP applications, and attach specific code examples. Step 1: Install the Memcache extension First, you need to install Me in your PHP environment

See all articles