|
|
|
@ -1,15 +1,20 @@ |
|
|
|
package com.bnyer.img.service.impl; |
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.bnyer.common.redis.service.RedisService; |
|
|
|
import com.bnyer.img.constants.RedisKeyConstant; |
|
|
|
import com.bnyer.img.domain.TiktokCollection; |
|
|
|
import com.bnyer.img.enums.TiktokCollectionEnum; |
|
|
|
import com.bnyer.img.mapper.TiktokCollectionMapper; |
|
|
|
import com.bnyer.img.service.TiktokCollectionService; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.data.redis.core.Cursor; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import java.util.Date; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
@Service |
|
|
|
@Slf4j |
|
|
|
@ -18,17 +23,38 @@ public class TiktokCollectionServiceImpl implements TiktokCollectionService { |
|
|
|
@Autowired |
|
|
|
private TiktokCollectionMapper tiktokCollectionMapper; |
|
|
|
|
|
|
|
@Override |
|
|
|
public int insert(TiktokCollection tiktokCollection) { |
|
|
|
tiktokCollection.setCreateTime(new Date()); |
|
|
|
tiktokCollection.setUpdateTime(new Date()); |
|
|
|
return tiktokCollectionMapper.insert(tiktokCollection); |
|
|
|
@Autowired |
|
|
|
private RedisService redisService; |
|
|
|
|
|
|
|
private String getHashKey(Long userId, Long imgId){ |
|
|
|
return userId+":"+imgId; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public int update(TiktokCollection tiktokCollection) { |
|
|
|
tiktokCollection.setUpdateTime(new Date()); |
|
|
|
return tiktokCollectionMapper.updateById(tiktokCollection); |
|
|
|
public void insert() { |
|
|
|
log.info("==============redis同步收藏记录到db数据开始!==============="); |
|
|
|
//拿缓存
|
|
|
|
String redisKey = RedisKeyConstant.TIKTOK_USER_COLLECT_KEY; |
|
|
|
Cursor<Map.Entry<Object, Object>> collection = redisService.getHashScan(redisKey); |
|
|
|
while (collection.hasNext()){ |
|
|
|
Map.Entry<Object, Object> next = collection.next(); |
|
|
|
String key = (String) next.getKey(); |
|
|
|
//分离出 userId,imgId
|
|
|
|
String[] split = key.split(":"); |
|
|
|
String userId = split[0]; |
|
|
|
Long userLongId = Long.parseLong(userId); |
|
|
|
String imgId = split[1]; |
|
|
|
Long imgLongId = Long.parseLong(imgId); |
|
|
|
TiktokCollection tiktokCollection = new TiktokCollection(); |
|
|
|
tiktokCollection.setUserId(userLongId); |
|
|
|
tiktokCollection.setImgId(imgLongId); |
|
|
|
tiktokCollection.setCreateTime(new Date()); |
|
|
|
tiktokCollection.setUpdateTime(new Date()); |
|
|
|
tiktokCollectionMapper.insert(tiktokCollection); |
|
|
|
//保存后从 Redis 中删除
|
|
|
|
redisService.deleteHashKey(redisKey,key); |
|
|
|
} |
|
|
|
log.info("==============redis同步收藏记录到db数据完成!==============="); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@ -44,4 +70,29 @@ public class TiktokCollectionServiceImpl implements TiktokCollectionService { |
|
|
|
Long num = tiktokCollectionMapper.selectCount(wrapper); |
|
|
|
return num.intValue(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void collect(Long userId, Long imgId) { |
|
|
|
//查询缓存中是否存在该用户和图片组装成的结果
|
|
|
|
String hashKey = getHashKey(userId,imgId); |
|
|
|
String redisKey = RedisKeyConstant.TIKTOK_USER_COLLECT_KEY; |
|
|
|
String imgCollectNumKey = RedisKeyConstant.TIKTOK_IMG_COLLECT_NUM_KEY; |
|
|
|
if(!redisService.hasKey(redisKey)){ |
|
|
|
//无则插入redis,状态置为1,图片数量+1
|
|
|
|
redisService.setCacheMapValue(redisKey,hashKey, TiktokCollectionEnum.COLLECTE.getCode()); |
|
|
|
redisService.hashIncr(imgCollectNumKey,String.valueOf(imgId), 1); |
|
|
|
log.info("用户【{}】收藏了图片【{}】,收藏量增加1",userId,imgId); |
|
|
|
}else{ |
|
|
|
//有则删除,图片数量-1
|
|
|
|
if(redisService.hasHashKey(redisKey,hashKey)){ |
|
|
|
redisService.deleteHashKey(redisKey,hashKey); |
|
|
|
redisService.hashIncr(imgCollectNumKey,String.valueOf(imgId), -1); |
|
|
|
log.info("用户【{}】取消收藏了图片【{}】,收藏量减少1",userId,imgId); |
|
|
|
}else{ |
|
|
|
//不存在
|
|
|
|
log.error("用户【{}】取消收藏图片【{}】失败,hashKey【{}】不存在!",userId,imgId,hashKey); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|