C a c h e

This commit is contained in:
2025-09-20 17:49:03 +08:00
parent d55c7a7769
commit b7d3e20c46
16 changed files with 219 additions and 75 deletions

View File

@@ -0,0 +1,113 @@
package com.ycwl.basic.repository;
import com.fasterxml.jackson.core.type.TypeReference;
import com.ycwl.basic.mapper.SourceMapper;
import com.ycwl.basic.mapper.VideoMapper;
import com.ycwl.basic.model.pc.source.entity.MemberSourceEntity;
import com.ycwl.basic.model.pc.video.entity.MemberVideoEntity;
import com.ycwl.basic.utils.JacksonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class MemberRelationRepository {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Autowired
private VideoMapper videoMapper;
@Autowired
private SourceMapper sourceMapper;
public static final String MEMBER_RELATION_BY_FACE_CACHE_KEY = "member_relation:face:%d:v";
public static final String MEMBER_RELATION_BY_FACE_TEMPLATE_CACHE_KEY = "member_relation:face:%d:v:template:%d";
public static final String MEMBER_SOURCE_BY_FACE_TYPE_CACHE_KEY = "member_relation:face:%d:s:type:%d";
public List<MemberVideoEntity> listRelationByFace(Long faceId) {
String cacheKey = String.format(MEMBER_RELATION_BY_FACE_CACHE_KEY, faceId);
if (redisTemplate.hasKey(cacheKey)) {
String cacheValue = redisTemplate.opsForValue().get(cacheKey);
return JacksonUtil.fromJson(cacheValue, new TypeReference<List<MemberVideoEntity>>() {});
}
List<MemberVideoEntity> result = videoMapper.listRelationByFace(faceId);
if (result != null) {
redisTemplate.opsForValue().set(cacheKey, JacksonUtil.toJson(result), 1, TimeUnit.HOURS);
}
return result;
}
public List<MemberVideoEntity> listRelationByFaceAndTemplate(Long faceId, Long templateId) {
String cacheKey = String.format(MEMBER_RELATION_BY_FACE_TEMPLATE_CACHE_KEY, faceId, templateId);
if (redisTemplate.hasKey(cacheKey)) {
String cacheValue = redisTemplate.opsForValue().get(cacheKey);
return JacksonUtil.fromJson(cacheValue, new TypeReference<List<MemberVideoEntity>>() {});
}
List<MemberVideoEntity> result = videoMapper.listRelationByFaceAndTemplate(faceId, templateId);
if (result != null) {
redisTemplate.opsForValue().set(cacheKey, JacksonUtil.toJson(result), 1, TimeUnit.HOURS);
}
return result;
}
public List<MemberSourceEntity> listSourceByFaceRelation(Long faceId, Integer type) {
String cacheKey = String.format(MEMBER_SOURCE_BY_FACE_TYPE_CACHE_KEY, faceId, type);
if (redisTemplate.hasKey(cacheKey)) {
String cacheValue = redisTemplate.opsForValue().get(cacheKey);
return JacksonUtil.fromJson(cacheValue, new TypeReference<List<MemberSourceEntity>>() {});
}
List<MemberSourceEntity> result = sourceMapper.listByFaceRelation(faceId, type);
if (result != null) {
redisTemplate.opsForValue().set(cacheKey, JacksonUtil.toJson(result), 1, TimeUnit.HOURS);
}
return result;
}
public void clearCacheByFace(Long faceId) {
String pattern = "member_relation:face:" + faceId + ":*";
Set<String> keys = redisTemplate.keys(pattern);
if (keys != null && !keys.isEmpty()) {
redisTemplate.delete(keys);
}
}
public void clearVCacheByFace(Long faceId) {
String pattern = "member_relation:face:" + faceId + ":v:*";
Set<String> keys = redisTemplate.keys(pattern);
if (keys != null && !keys.isEmpty()) {
redisTemplate.delete(keys);
}
}
public void clearSCacheByFace(Long faceId) {
String pattern = "member_relation:face:" + faceId + ":s:*";
Set<String> keys = redisTemplate.keys(pattern);
if (keys != null && !keys.isEmpty()) {
redisTemplate.delete(keys);
}
}
public void clearAllCache() {
String pattern = "member_relation:*";
Set<String> keys = redisTemplate.keys(pattern);
if (keys != null && !keys.isEmpty()) {
redisTemplate.delete(keys);
}
}
}

View File

@@ -33,6 +33,8 @@ public class SourceRepository {
private TemplateRepository templateRepository;
@Autowired
private DeviceRepository deviceRepository;
@Autowired
private MemberRelationRepository memberRelationRepository;
public void addSource(SourceEntity source) {
sourceMapper.add(source);
@@ -46,6 +48,7 @@ public class SourceRepository {
memberSource.setOrderId(orderId);
memberSource.setIsBuy(1);
sourceMapper.updateRelation(memberSource);
memberRelationRepository.clearSCacheByFace(faceId);
}
public void setUserNotBuyItem(Long memberId, int type, Long faceId) {
@@ -56,6 +59,7 @@ public class SourceRepository {
memberSource.setOrderId(null);
memberSource.setIsBuy(0);
sourceMapper.updateRelation(memberSource);
memberRelationRepository.clearSCacheByFace(faceId);
}
public boolean getUserIsBuy(Long userId, int type, Long faceId) {

View File

@@ -2,8 +2,6 @@ package com.ycwl.basic.repository;
import com.ycwl.basic.biz.PriceBiz;
import com.ycwl.basic.model.mobile.order.IsBuyBatchRespVO;
import com.ycwl.basic.model.mobile.order.IsBuyRespVO;
import com.ycwl.basic.pricing.dto.DiscountDetectionContext;
import com.ycwl.basic.pricing.dto.VoucherInfo;
import com.ycwl.basic.pricing.enums.VoucherDiscountType;
import com.ycwl.basic.pricing.service.IVoucherService;
@@ -36,6 +34,8 @@ public class VideoRepository {
private PriceBiz priceBiz;
@Autowired
private IVoucherService iVoucherService;
@Autowired
private MemberRelationRepository memberRelationRepository;
public VideoEntity getVideo(Long videoId) {
if (redisTemplate.hasKey(String.format(VIDEO_CACHE_KEY, videoId))) {
@@ -84,6 +84,12 @@ public class VideoRepository {
memberVideo.setIsBuy(1);
memberVideo.setOrderId(orderId);
videoMapper.updateRelation(memberVideo);
// 清理视频关系缓存
MemberVideoEntity existingVideo = videoMapper.queryUserVideo(memberId, videoId);
if (existingVideo != null && existingVideo.getFaceId() != null) {
memberRelationRepository.clearVCacheByFace(existingVideo.getFaceId());
}
}
public void setUserNotBuyItem(Long memberId, Long videoId) {
@@ -93,6 +99,12 @@ public class VideoRepository {
memberVideo.setIsBuy(0);
memberVideo.setOrderId(null);
videoMapper.updateRelation(memberVideo);
// 清理视频关系缓存
MemberVideoEntity existingVideo = videoMapper.queryUserVideo(memberId, videoId);
if (existingVideo != null && existingVideo.getFaceId() != null) {
memberRelationRepository.clearVCacheByFace(existingVideo.getFaceId());
}
}
public boolean getUserIsBuy(Long userId, Long videoId) {
@@ -133,7 +145,4 @@ public class VideoRepository {
return true;
}
public List<MemberVideoEntity> getVideoByFaceAndTemplateId(Long memberId, Long faceId, String templateId) {
return videoMapper.userFaceTemplateVideo(memberId, faceId, Long.valueOf(templateId));
}
}