Home php教程 php手册 PHP模块 Memcached功能多于Memcache

PHP模块 Memcached功能多于Memcache

Jun 21, 2016 am 08:53 AM
counter memcache memcached token

比如说PECL里有两个Memcached的模块,Memcache和Memcached,目前大部分PHP环境里使用的是名字里不带d的Memcache版本,这个版本释出的比较早,是一个原生版本,与之对应的带d的Memcached版本则是建立在libmemcached的基础上,所以说Memcached版本的功能更全一些。

安装Memcached版本的PHP模块

wget http://download.tangent.org/libmemcached-0.35.tar.gz
tar zxf libmemcached-0.35.tar.gz
cd libmemcached-0.35
./configure
make
make install

wget http://pecl.php.net/get/memcached-1.0.0.tgz
tar zxf memcached-1.0.0.tgz
cd memcached-1.0.0
phpize
./configure
make
make install

打开php.ini加上:

extension = "memcached.so"

这样安装就结束了,你可以通过下列命令来确认:

php -m grep mem

演示Memcached版本的新功能

先虚构一个问题,假设counter初始值是一个整数,不使用increment方法,通过get/set完成每次加一。

在Memcache版本里,我们只能按照大致如下的方式来进行:

$m = new Memcache();
$m->addServer('localhost', 11211);
$v = $m->get('counter');
$m->set('counter', $v + 1);

由于get/set这两个动作无法作为一个原子来操作,所以当多个进程同时处理时,会出现丢失的可能,更让人恼火的是,你根本就不知道什么时候出现丢失。

再看看Memcached版本里,我们是如何做的:

$md = new Memcached();
$md->addServer('localhost', 11211);
$v = $md->get('counter', null, $token)
$md->cas($token, 'counter', $v + 1);

cas是Memcached版本里提供的功能,说白了就是一个乐观锁的功能,如果你把$token的值var_dump出来,就会发现$token其实就是一个版本号,如果通过get得到的$token版本号在cas的时候不对应,就说明已经有别的操作更新了,此时cas操作会失败,至于如何继续操作,就看你自己了。

注:如果你想手动重现一下冲突的情况,可在get和cas之间sleep若干秒,并拷贝两份脚本,先后执行。

顺便说一句,推荐的Memcached版本模块的哈希设置如下:

$md->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
$md->setOption(Memcached::OPT_HASH, Memcached::HASH_CRC);

总结

Memcached版本还有很多Memcache没有的功能,比如通过getByKey, setByKey等自动支持多个服务器,就不赘述了,该用哪个扩展已经不言自明了。

补充:http://code.google.com/p/memcached/wiki/PHPClientComparison



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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

What to do if the login token is invalid What to do if the login token is invalid Sep 14, 2023 am 11:33 AM

Solutions to invalid login token include checking whether the Token has expired, checking whether the Token is correct, checking whether the Token has been tampered with, checking whether the Token matches the user, clearing the cache or cookies, checking the network connection and server status, logging in again or requesting a new Token. Contact technical support or developers, etc. Detailed introduction: 1. Check whether the Token has expired. The login Token usually has a validity period set. Once the validity period exceeds, it will be considered invalid, etc.

How to solve the problem of invalid login token How to solve the problem of invalid login token Sep 14, 2023 am 10:57 AM

The problem of invalid login token can be solved by checking the network connection, checking the token validity period, clearing cache and cookies, checking login status, contacting the application developer and strengthening account security. Detailed introduction: 1. Check the network connection, reconnect to the network or change the network environment; 2. Check the token validity period, obtain a new token, or contact the developer of the application; 3. Clear cache and cookies, clear browser cache and Cookie, and then log in to the application again; 4. Check the login status.

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 solve C++ syntax error: 'expected primary-expression before ':' token'? How to solve C++ syntax error: 'expected primary-expression before ':' token'? Aug 26, 2023 pm 04:06 PM

How to solve C++ syntax error: 'expectedprimary-expressionbefore':'token'? Syntax errors are a common problem in C++ programming. One of the common errors is the "expectedprimary-expressionbefore':'token" error message. This error usually occurs when using conditional expressions and the ternary operator. This article will introduce the cause of this error

What does token mean? What does token mean? Feb 29, 2024 am 10:19 AM

Token is a kind of virtual currency. It is a digital currency used to represent user permissions, record transaction information, and pay virtual currency. Token can be used to conduct transactions on a specific network, it can be used to buy or sell specific virtual currencies, and it can also be used to pay for specific services.

How to optimize PHP application CPU usage using Memcached caching technology? How to optimize PHP application CPU usage using Memcached caching technology? Jun 21, 2023 pm 05:07 PM

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

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

See all articles