Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial php memcached的高并发处理队列实现问题

php memcached的高并发处理队列实现问题

Jun 23, 2016 pm 01:39 PM

       刚看了memcached处理抢购问题的队列的一些代码,我理解到的思路有两种

      1.使用 $mem->set(LOCK_key,1) 来加锁,入队结束后 $mem->delete(LOCK_key) 解锁

      2.使用memcached 的 increment(key,1) 来获得队列位置

     我的问题是: 
    1.第一种方法可行吗?
     2.$memcached->increment(key,1)  这个函数能处理并发访问吗?也就是多个进程同时调用 increment函数,会不会发生并发覆盖问题?

    我今年大四了,在不断找工作中,由于之前学习的不够系统的PHP,被 YY 和 4399 都复试的时候刷了。
    心很凉,找工作不易,且行且珍惜,我要好好加油,谢谢各位大牛拉我一把


回复讨论(解决方案)

会发生冲突
你自己测试一下就知道

只用memcached你这样会冲突的,对于高并发冲突,可使用memcacheQ来解决。
memcacheq 是专为门为解决高并发问题所开发的中间件,以队列的方式存取数据。
参考: http://blog.csdn.net/fdipzone/article/details/17933673

1.第一种方法是可以的,只不过这个锁效率太低了,多个客户端访问都在等待解锁,速度超级慢(根据我的测试,800个客户端,每个客户端访问10次,填满一个200的队列,需要用时32秒)。
这种队列的实现方法github上面已经有大神贴代码了
https://github.com/meetrajesh/php-memqueue/blob/master/memqueue.php
只不过第95行  while ($this->memc->add($this->lock_key, '1', 0, 2))  while里面要加上!   才能正确加锁。

2.memcached的所有操作都是原子性的(get/set/increment, 参见 http://blog.csdn.net/jarfield/article/details/4336035#what's the big benefit for all this),也就是说无论多少个操作,你丢到memcached服务器里,都是顺序执行的,所以方法2是可行的。我同样用800个客户端,每个客户端访问10次,最后查看increment的值,正好是8000,所以不会覆盖。

无独有偶,我也是今年大四连续被4399,YY等公司刷掉了。
痛定思痛,从工作中慢慢重新学习吧

忘了说了,队列服务,建议用 redis 吧,他自带队列的各种操作,性能比memcached的在服务端加锁要好很多。

http://redis.io/

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)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months 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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles