Time-consuming operations in web projects, use asynchronous requests to reduce request waiting time, and use Redis to ensure idempotency of operations

A recent project encountered an interface that required time-consuming operations, but our company's front-end and back-end interaction gateways set a timeout period. Therefore, I need to set up a plan to ensure a faster response to the user and to ensure that the task is executed. So I adopted, the following way of operation.

After the client sends the request, I will go to Redis to check whether there are still tasks being executed. If there is, the asynchronous thread will not be opened, and if not, the asynchronous thread will be opened to perform data business operations. Below is the pseudocode I wrote.

    @Override
    public Result AsyncService() {
       //1、检查redis中是否有对应的操作
       Boolean check =  checkReis();
        if(check){
            //2、用线程池开启异步线程
            threadPoolTaskExecutor.execute(() -> {
                //3、执行具体的业务操作
                doService();
            }
            return Result.ok();
        }

      return Result.fail(); 
    }

 

おすすめ

転載: blog.csdn.net/weixin_55229531/article/details/131628362