detail价格计算折扣

This commit is contained in:
2024-10-17 09:12:58 +08:00
parent eff2d6d630
commit 519bedfc7c
3 changed files with 28 additions and 0 deletions

View File

@@ -70,6 +70,31 @@ public class Utils {
}
}
public static<T> T runFunAndTransaction(Supplier<T> supplier, StringRedisTemplate redisTemplate, String lockKey) {
try{
// 创建线程id, 用作判断
String clientId = UUID.randomUUID().toString();
// 设置分布式锁
boolean lock = Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(lockKey, clientId, 30, TimeUnit.MILLISECONDS));
int count = 0;
while (!lock) {
if (count++ > 100) {
throw new BadRequestException("系统繁忙, 稍后再试");
}
Thread.sleep(20);
lock = Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(lockKey, clientId, 30, TimeUnit.MILLISECONDS));
}
return supplier.get();
} catch (RuntimeException e){
log.info("执行出错:{}", e.getMessage());
throw e;
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally{
redisTemplate.delete(lockKey);
}
}
public static <T, R> R runFunAndRetry(
Supplier<R> function,
Function<R, Boolean> check, Consumer<R> errFun) {