feat(printer): 添加用户购买项设置的Redis缓存控制

- 引入RedisTemplate依赖用于缓存控制
- 新增60秒的缓存键避免重复处理用户购买项
- 在setUserIsBuyItem方法中实现缓存检查逻辑- 添加TimeUnit依赖支持缓存过期时间设置
- 定义USER_PHOTO_LIST_TO_PRINTER缓存键前缀
This commit is contained in:
2025-09-26 12:39:17 +08:00
parent ab3208c9df
commit aba9fb0a15

View File

@@ -39,6 +39,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Strings; import org.apache.commons.lang3.Strings;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.math.BigDecimal; import java.math.BigDecimal;
@@ -49,6 +50,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Slf4j @Slf4j
@@ -377,8 +379,16 @@ public class PrinterServiceImpl implements PrinterService {
printerMapper.updateUserPhotoListToPrinter(memberId, scenicId, printerId); printerMapper.updateUserPhotoListToPrinter(memberId, scenicId, printerId);
} }
@Autowired
private RedisTemplate<String, String> redisTemplate;
private static final String USER_PHOTO_LIST_TO_PRINTER = "USER_PHOTO_LIST_TO_PRINTER:";
@Override @Override
public void setUserIsBuyItem(Long memberId, Long id, Long orderId) { public void setUserIsBuyItem(Long memberId, Long id, Long orderId) {
if (redisTemplate.opsForValue().get(USER_PHOTO_LIST_TO_PRINTER + memberId + ":" + orderId) != null) {
return;
}
redisTemplate.opsForValue().set(USER_PHOTO_LIST_TO_PRINTER + memberId + ":" + orderId, "1", 60, TimeUnit.SECONDS);
printerMapper.setUserIsBuyItem(memberId, id, orderId); printerMapper.setUserIsBuyItem(memberId, id, orderId);
// 创建打印任务 // 创建打印任务
List<MemberPrintResp> userPhotoListByOrderId = getUserPhotoListByOrderId(orderId); List<MemberPrintResp> userPhotoListByOrderId = getUserPhotoListByOrderId(orderId);