Table of Contents
回复内容:
Home Backend Development PHP Tutorial 怎么在代码层处理app客户端同个人同时并发多个请求?

怎么在代码层处理app客户端同个人同时并发多个请求?

Jun 06, 2016 pm 08:20 PM
mysql nginx php python redis

怎么在代码层处理app客户端同个人同时并发多个请求?

有个分享统计的接口,有时候客户端同个人会并发多个请求过来,服务端做了集群,最先通过redis 做一个cache(uid+articleid)判断是否有,结果直接绕过.

有没有什么办法直接在代码逻辑里面判断到,然后直接返回,而不是要到mysql那里通过唯一索引什么的来解决.

谢谢~~~

回复内容:

怎么在代码层处理app客户端同个人同时并发多个请求?

有个分享统计的接口,有时候客户端同个人会并发多个请求过来,服务端做了集群,最先通过redis 做一个cache(uid+articleid)判断是否有,结果直接绕过.

有没有什么办法直接在代码逻辑里面判断到,然后直接返回,而不是要到mysql那里通过唯一索引什么的来解决.

谢谢~~~

记录这个人访问时间间隔,如果频率超出限定值,直接返回错误代码~~

同个人的请求也可以通过redis控制请求频率啊。redis中key:uid+operation 通过increment(原子操作) 拿锁限制。

你的需求是并发安全方面的,就是如果同时多个请求,每个请求都涉及到查询,判断,更新这样的三个步骤,由于多个线程同时进行前两部操作,而导致判断被绕过。
解决办法:
1.redis的watch,将查询到更新这三个步骤绑定到一起,其实直接使用redis的事务应该就可以了,无需使用watch,具体请实际测试
2.利用redis设计一个分布式锁,可参考官方给出的方案:http://www.oschina.net/translate/redis-distlock

3.利用队列,将同一个用户的请求放入同一个队列,依次进行处理

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)

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Python vs. C  : Learning Curves and Ease of Use Python vs. C : Learning Curves and Ease of Use Apr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Does Python projects need to be layered? Does Python projects need to be layered? Apr 19, 2025 pm 10:06 PM

Discussion on Hierarchical Structure in Python Projects In the process of learning Python, many beginners will come into contact with some open source projects, especially projects using the Django framework...

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? Apr 19, 2025 pm 07:15 PM

Discussing the hierarchical architecture problem in back-end development. In back-end development, common hierarchical architectures include controller, service and dao...

See all articles