Browse Source

feature1.0.0:img服务新增redisService的递增/递减方法;添加收藏接口

master
chengkun 4 years ago
parent
commit
44742a4df1
  1. 45
      bnyer-common/bnyer-common-redis/src/main/java/com/bnyer/common/redis/service/RedisService.java
  2. 10
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/constants/RedisKeyConstant.java
  3. 11
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/TiktokMiniController.java
  4. 26
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/dto/CollectionDto.java
  5. 20
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/TiktokCollectionService.java
  6. 65
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/TiktokCollectionServiceImpl.java

45
bnyer-common/bnyer-common-redis/src/main/java/com/bnyer/common/redis/service/RedisService.java

@ -7,10 +7,7 @@ import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
/**
@ -273,4 +270,44 @@ public class RedisService
public Long decr(final String key,final long delta){
return redisTemplate.opsForValue().decrement(key, delta);
}
/**
* 获取hashScan结果
* @param key 键值
* @return -
*/
public Cursor<Map.Entry<Object, Object>> getHashScan(String key){
return redisTemplate.opsForHash().scan(key,ScanOptions.NONE);
}
/**
* 删除hashkey
* @param key 键值
* @param hashKey hash键值
*/
public void deleteHashKey(final String key,final String hashKey)
{
redisTemplate.opsForHash().delete(key, hashKey);
}
/**
* 判断是否存在hashkey
* @param key 键值
* @param hashKey hash键值
*/
public boolean hasHashKey(final,String key,final String hashKey)
{
return redisTemplate.opsForHash().hasKey(key,hashKey);
}
/**
* hash数据递增
* @param key
* @param hashKey hash键
* @param delta 步长
* @return -
*/
public Long hashIncr(final String key,final String hashKey,final long delta){
return redisTemplate.opsForHash().increment(key, hashKey,delta);
}
}

10
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/constants/RedisKeyConstant.java

@ -6,4 +6,14 @@ public class RedisKeyConstant {
* 小程序用户图片首页键
*/
public static final String TIKTOK_USER_IMG_KEY = "bnyer.tiktok.userImg";
/**
* 抖音小程序用户收藏图片键
*/
public static final String TIKTOK_USER_COLLECT_KEY = "bnyer.tiktok.collect";
/**
* 抖音小程序图片收藏数量键
*/
public static final String TIKTOK_IMG_COLLECT_NUM_KEY = "bnyer.tiktok.collectNum";
}

11
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/TiktokMiniController.java

@ -38,6 +38,9 @@ public class TiktokMiniController extends BaseController {
@Autowired
private TiktokUserService tiktokUserService;
@Autowired
private TiktokCollectionService tiktokCollectionService;
@ApiOperation(value="查询banner列表")
@GetMapping(value = "/listBanner")
public AjaxResult listBanner(){
@ -107,4 +110,12 @@ public class TiktokMiniController extends BaseController {
public AjaxResult tiktokUserImgs(){
return AjaxResult.success(tiktokUserService.queryUserImgList());
}
@ApiOperation(value="添加收藏/取消收藏")
@PostMapping(value = "/collect")
public AjaxResult insert(@Validated @RequestBody @ApiParam("收藏对象") CollectionDto dto){
log.debug("添加收藏/取消收藏参数为:{}", JSON.toJSONString(dto));
tiktokCollectionService.collect(dto.getUserId(),dto.getImgId());
return AjaxResult.success();
}
}

26
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/dto/CollectionDto.java

@ -0,0 +1,26 @@
package com.bnyer.img.dto;
import com.bnyer.common.core.utils.bean.BeanUtils;
import com.bnyer.img.domain.Banner;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
@Getter
@Setter
@ApiModel("收藏接收类")
public class CollectionDto implements Serializable {
@NotNull(message = "用户id不能为空!")
@ApiModelProperty(value="用户id")
private Long userId;
@NotNull(message = "图片id不能为空!")
@ApiModelProperty(value="图片id")
private Long imgId;
}

20
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/TiktokCollectionService.java

@ -7,18 +7,9 @@ import java.util.List;
public interface TiktokCollectionService {
/**
* 新增收藏
* @param tiktokCollection 收藏对象
* @return -
*/
int insert(TiktokCollection tiktokCollection);
/**
* 修改收藏
* @param tiktokCollection 收藏对象
* @return -
* 同步redis收藏到db中
*/
int update(TiktokCollection tiktokCollection);
void insert();
/**
* 批量删除收藏
@ -34,4 +25,11 @@ public interface TiktokCollectionService {
*/
Integer getCollectionCount(Long imgId);
/**
* 收藏/取消收藏
* @param userId 用户Id
* @param imgId 图片Id
*/
void collect(Long userId, Long imgId);
}

65
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/TiktokCollectionServiceImpl.java

@ -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) {
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());
return tiktokCollectionMapper.updateById(tiktokCollection);
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);
}
}
}
}

Loading…
Cancel
Save