kongdeqiang
1 天以前 973f7a2b420dddb06553489d72d4aa6fdea3c1c0
src/main/java/com/boying/controller/phone/YCPayController.java
@@ -715,8 +715,48 @@
                outParkService.saveOrUpdate(outPark);
                //缓存在redis里
                String jsonValue = JSON.toJSONString(outPark);
                redisTemplate.opsForValue().set("outPark-"+outPark.getBarrierId(), jsonValue);
                redisTemplate.opsForValue().set("park_change_in_"+outPark.getParkId(),"true",1, TimeUnit.DAYS);
                String redisKey = "outPark-"+outPark.getBarrierId();
                String parkChangeKey = "park_change_in_"+outPark.getParkId();
                int retryCount = 0;
                int maxRetry = 3;
                boolean cacheSuccess = false;
                while(retryCount < maxRetry && !cacheSuccess){
                    try {
                        redisTemplate.opsForValue().set(redisKey, jsonValue);
                        redisTemplate.opsForValue().set(parkChangeKey,"true",1, TimeUnit.DAYS);
                        cacheSuccess = true;
                        String logs = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(new Date())+
                                " 车牌号为:"+outPark.getCarNo()+
                                ",Redis缓存设置成功,道闸ID:"+outPark.getBarrierId()+"\n";
                        writeTxt2(logs);
                    } catch (Exception e) {
                        retryCount++;
                        String errorLogs = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(new Date())+
                                " 车牌号为:"+outPark.getCarNo()+
                                ",Redis缓存设置失败,第"+retryCount+"次重试,错误信息:"+e.getMessage()+"\n";
                        writeTxt2(errorLogs);
                        e.printStackTrace();
                        if(retryCount < maxRetry){
                            try {
                                Thread.sleep(100); // 等待100ms后重试
                            } catch (InterruptedException ie) {
                                Thread.currentThread().interrupt();
                            }
                        }
                    }
                }
                if(!cacheSuccess){
                    String errorLogs = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(new Date())+
                            " 车牌号为:"+outPark.getCarNo()+
                            ",Redis缓存设置失败,已重试"+maxRetry+"次,请检查Redis服务状态!\n";
                    writeTxt2(errorLogs);
                    // 虽然Redis缓存失败,但数据库已更新,心跳接口可以通过数据库查询获取数据
                }
//                redisTemplate.opsForValue().set("outPark-"+outPark.getBarrierId(), jsonValue);
//                redisTemplate.opsForValue().set("park_change_in_"+outPark.getParkId(),"true",1, TimeUnit.DAYS);
            }
        }
    }