You've already forked FrameTour-BE
feat(face): 增加人脸识别计数功能
- 在 FaceConstant 中添加 FACE_RECOGNITION_COUNT_PFX 常量 - 在 FaceServiceImpl 中实现记录人脸识别次数的方法 - 使用 Redis 进行计数,并设置过期时间
This commit is contained in:
@@ -4,4 +4,5 @@ public class FaceConstant {
|
||||
public static final String FACE_DB_NAME_PFX="face:db:";
|
||||
public static final String USER_FACE_DB_NAME="userFace";
|
||||
public static final String FACE_USER_URL_PFX="face:user:url:";
|
||||
public static final String FACE_RECOGNITION_COUNT_PFX="face:recognition:count:";
|
||||
}
|
||||
|
@@ -53,6 +53,7 @@ import com.ycwl.basic.task.VideoPieceGetter;
|
||||
import com.ycwl.basic.utils.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@@ -65,8 +66,10 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.ycwl.basic.constant.FaceConstant.FACE_RECOGNITION_COUNT_PFX;
|
||||
import static com.ycwl.basic.constant.FaceConstant.USER_FACE_DB_NAME;
|
||||
import static com.ycwl.basic.constant.StorageConstant.USER_FACE;
|
||||
|
||||
@@ -110,6 +113,8 @@ public class FaceServiceImpl implements FaceService {
|
||||
private DeviceRepository deviceRepository;
|
||||
@Autowired
|
||||
private OrderMapper orderMapper;
|
||||
@Autowired
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
@Override
|
||||
public ApiResponse<PageInfo<FaceRespVO>> pageQuery(FaceReqQuery faceReqQuery) {
|
||||
@@ -251,6 +256,9 @@ public class FaceServiceImpl implements FaceService {
|
||||
|
||||
log.debug("开始人脸匹配:faceId={}, isNew={}", faceId, isNew);
|
||||
|
||||
// 记录识别次数到Redis,设置2天过期时间
|
||||
recordFaceRecognitionCount(faceId);
|
||||
|
||||
try {
|
||||
// 1. 数据准备:获取人脸信息、景区配置、适配器等
|
||||
FaceEntity face = faceRepository.getFace(faceId);
|
||||
@@ -800,4 +808,31 @@ public class FaceServiceImpl implements FaceService {
|
||||
throw new BaseException("生成二维码失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录人脸识别次数到Redis
|
||||
*
|
||||
* @param faceId 人脸ID
|
||||
*/
|
||||
private void recordFaceRecognitionCount(Long faceId) {
|
||||
if (faceId == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
String redisKey = FACE_RECOGNITION_COUNT_PFX + faceId;
|
||||
|
||||
// 使用Redis原子操作INCR增加计数
|
||||
Long count = redisTemplate.opsForValue().increment(redisKey);
|
||||
|
||||
// 设置2天过期时间(48小时)
|
||||
redisTemplate.expire(redisKey, 2, TimeUnit.DAYS);
|
||||
|
||||
log.debug("人脸识别计数更新:faceId={}, count={}", faceId, count);
|
||||
|
||||
} catch (Exception e) {
|
||||
// 计数失败不应影响主要业务逻辑,只记录错误日志
|
||||
log.error("记录人脸识别次数失败:faceId={}", faceId, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user