怎么在代码层处理app客户端同个人同时并发多个请求?
怎么在代码层处理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.利用队列,将同一个用户的请求放入同一个队列,依次进行处理

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

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.

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 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 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 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.

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...

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

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