funUtil修改

This commit is contained in:
张松
2025-03-03 11:20:19 +08:00
parent 0409cda25a
commit 2817139481
3 changed files with 15 additions and 6 deletions

View File

@@ -49,6 +49,8 @@ public class CallTableServiceImpl extends ServiceImpl<CallTableMapper, CallTable
@Resource
private ShopUserService shopUserService;
@Resource
private FunUtil funUtil;
@Resource
private StringRedisTemplate stringRedisTemplate;
@Resource
private ShopInfoService shopInfoService;
@@ -220,7 +222,7 @@ public class CallTableServiceImpl extends ServiceImpl<CallTableMapper, CallTable
}
private String getCallNumber(Long shopId, CallTable callTable) {
return FunUtil.runFunAndCheckKey(() -> {
return funUtil.runFunAndCheckKey(() -> {
String callNumKey = RedisCst.getTableCallNumKey(shopId, callTable.getId());
String value = stringRedisTemplate.opsForValue().get(callNumKey);
AtomicReference<String> newVal = new AtomicReference<>("");
@@ -244,7 +246,7 @@ public class CallTableServiceImpl extends ServiceImpl<CallTableMapper, CallTable
stringRedisTemplate.opsForValue().set(callNumKey, value);
return callTable.getPrefix() + value;
}
}, stringRedisTemplate, RedisCst.getLockKey("UPDATE_TABLE", shopId, callTable.getId()));
}, RedisCst.getLockKey("UPDATE_TABLE", shopId, callTable.getId()));
}

View File

@@ -28,7 +28,7 @@ import java.util.Calendar;
@Service
public class ShopTableOrderStatisticServiceImpl extends ServiceImpl<ShopTableOrderStatisticMapper, ShopTableOrderStatistic> implements ShopTableOrderStatisticService{
@Resource
private StringRedisTemplate redisTemplate;
private FunUtil funUtil;
@Override
public Page<ShopTableOrderStatistic> summary(Long shopId, String startTime, String endTime) {
@@ -39,7 +39,7 @@ public class ShopTableOrderStatisticServiceImpl extends ServiceImpl<ShopTableOrd
@Override
public boolean addInfo(long shopId, long tableId, long count, BigDecimal amount) {
return FunUtil.runFunAndCheckKey(() -> {
return funUtil.runFunAndCheckKey(() -> {
ShopTableOrderStatistic statistic = getOne(new QueryWrapper().eq(ShopTableOrderStatistic::getShopId, shopId).eq(ShopTableOrderStatistic::getTableId, tableId)
.eq(ShopTableOrderStatistic::getCreateDay, DateUtil.date().toDateStr()));
if (statistic == null) {
@@ -52,6 +52,6 @@ public class ShopTableOrderStatisticServiceImpl extends ServiceImpl<ShopTableOrd
save(statistic);
}
return mapper.incrInfo(shopId, tableId, count, amount, DateUtil.date().toDateStr());
}, redisTemplate, RedisCst.getLockKey("add_table_order_statistic", shopId, tableId));
}, RedisCst.getLockKey("add_table_order_statistic", shopId, tableId));
}
}

View File

@@ -1,8 +1,12 @@
package com.czg.service.account.util;
import com.czg.exception.ApiNotPrintException;
import com.czg.service.RedisService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@@ -14,10 +18,13 @@ import java.util.function.Supplier;
* @author Administrator
*/
@Slf4j
@Component
public class FunUtil {
@Resource
private RedisTemplate<String, Object> redisTemplate;
public static int retryCount = 5;
public static<T> T runFunAndCheckKey(Supplier<T> supplier, StringRedisTemplate redisTemplate, String lockKey) {
public <T> T runFunAndCheckKey(Supplier<T> supplier, String lockKey) {
try{
// 设置分布式锁
boolean lock = Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(lockKey, "1", 30, TimeUnit.MILLISECONDS));