Browse Source

feature1.0.0:添加收藏量、点赞、下载xxl-job方法

master
chengkun 4 years ago
parent
commit
252cc09cb2
  1. 23
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/config/XxlJobConfig.java
  2. 5
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/constants/RedisKeyConstant.java
  3. 3
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/TiktokMiniController.java
  4. 7
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/mapper/TiktokLikeMapper.java
  5. 11
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/TiktokImgService.java
  6. 2
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/TiktokLikeService.java
  7. 10
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/TiktokCollectionServiceImpl.java
  8. 33
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/TiktokImgServiceImpl.java
  9. 32
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/TiktokLikeServiceImpl.java
  10. 28
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/task/CollectionSyncTask.java
  11. 28
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/task/DownloadSyncTask.java
  12. 27
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/task/LikeSyncTask.java
  13. 9
      bnyer-services/bnyer-img/src/main/resources/com/bnyer/img/mapper/TiktokLikeMapper.xml

23
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/config/XxlJobConfig.java

@ -3,7 +3,6 @@ package com.bnyer.img.config;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -20,11 +19,11 @@ public class XxlJobConfig {
@Value("${xxl-job.addresses}") @Value("${xxl-job.addresses}")
private String adminAddresses; private String adminAddresses;
// @Value("${xxl.job.executor.appname}") @Value("${xxl-job.appname}")
// private String appName; private String appName;
//
// @Value("${xxl.job.executor.ip}") @Value("${xxl-job.ip}")
// private String ip; private String ip;
@Value("${xxl-job.port}") @Value("${xxl-job.port}")
private int port; private int port;
@ -35,21 +34,21 @@ public class XxlJobConfig {
@Value("${xxl-job.logpath}") @Value("${xxl-job.logpath}")
private String logPath; private String logPath;
//@Value("${xxl.job.executor.logretentiondays}") @Value("${xxl-job.logretentiondays}")
//private int logRetentionDays; private int logRetentionDays;
@Bean(initMethod = "start", destroyMethod = "destroy") @Bean
public XxlJobSpringExecutor xxlJobExecutor() { public XxlJobSpringExecutor xxlJobExecutor() {
log.info(">>>>>>>>>>> xxl-job 配置初始化开始 <<<<<<<<<<<<."); log.info(">>>>>>>>>>> xxl-job 配置初始化开始 <<<<<<<<<<<<.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor(); XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses); xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
//xxlJobSpringExecutor.setAppName(appName); xxlJobSpringExecutor.setAppName(appName);
//xxlJobSpringExecutor.setIp(ip); xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port); xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken); xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath); xxlJobSpringExecutor.setLogPath(logPath);
//xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays); xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
log.info(">>>>>>>>>>> xxl-job 配置初始化结束 <<<<<<<<<<<<."); log.info(">>>>>>>>>>> xxl-job 配置初始化结束 <<<<<<<<<<<<.");
return xxlJobSpringExecutor; return xxlJobSpringExecutor;
} }

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

@ -27,6 +27,11 @@ public class RedisKeyConstant {
*/ */
public static final String TIKTOK_IMG_LIKE_NUM_KEY = "bnyer.tiktok.likeNum"; public static final String TIKTOK_IMG_LIKE_NUM_KEY = "bnyer.tiktok.likeNum";
/**
* 抖音小程序图片下载数量键
*/
public static final String TIKTOK_IMG_DOWNLOAD_NUM_KEY = "bnyer.tiktok.downloadNum";
/** /**
* 抖音小程序用户登录键 * 抖音小程序用户登录键
*/ */

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

@ -1,14 +1,13 @@
package com.bnyer.img.controller; package com.bnyer.img.controller;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.bnyer.common.core.annotation.TokenCheck;
import com.bnyer.common.core.web.controller.BaseController; import com.bnyer.common.core.web.controller.BaseController;
import com.bnyer.common.core.web.domain.AjaxResult; import com.bnyer.common.core.web.domain.AjaxResult;
import com.bnyer.img.domain.Feedback; import com.bnyer.img.domain.Feedback;
import com.bnyer.img.domain.TiktokImg; import com.bnyer.img.domain.TiktokImg;
import com.bnyer.img.dto.*; import com.bnyer.img.dto.*;
import com.bnyer.img.service.*; import com.bnyer.img.service.*;
import com.bnyer.img.service.impl.TiktokLikeService; import com.bnyer.img.service.TiktokLikeService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiParam;

7
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/mapper/TiktokLikeMapper.java

@ -4,10 +4,17 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bnyer.img.domain.TiktokLike; import com.bnyer.img.domain.TiktokLike;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/** /**
* @author chengkun * @author chengkun
* @date 2022/4/21 9:50 * @date 2022/4/21 9:50
*/ */
@Mapper @Mapper
public interface TiktokLikeMapper extends BaseMapper<TiktokLike> { public interface TiktokLikeMapper extends BaseMapper<TiktokLike> {
/**
* 批量插入点赞数据
* @param list 点赞数据列表
*/
void insertBatch(List<TiktokLike> list);
} }

11
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/TiktokImgService.java

@ -90,4 +90,15 @@ public interface TiktokImgService {
* @return - * @return -
*/ */
int changeHot(Long id,String status); int changeHot(Long id,String status);
/**
* 图片下载数量写入缓存
* @param imgId 图片id
*/
void writeDownloadNum(Long imgId);
/**
* 同步图片下载数量到db
*/
void insertDownloadNum();
} }

2
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/TiktokLikeService.java → bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/TiktokLikeService.java

@ -1,4 +1,4 @@
package com.bnyer.img.service.impl; package com.bnyer.img.service;
import java.util.List; import java.util.List;

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

@ -19,6 +19,7 @@ import org.springframework.data.redis.core.Cursor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -48,12 +49,17 @@ public class TiktokCollectionServiceImpl implements TiktokCollectionService {
String imgId = (String) next.getKey(); String imgId = (String) next.getKey();
Long imgLongId = Long.parseLong(imgId); Long imgLongId = Long.parseLong(imgId);
Integer collectNum = (Integer) next.getValue(); Integer collectNum = (Integer) next.getValue();
TiktokImg img = tiktokImgMapper.selectById(imgLongId);
if(img == null){
redisService.deleteHashKey(imgCollectNumKey, imgId);
continue;
}
TiktokImg tiktokImg = new TiktokImg(); TiktokImg tiktokImg = new TiktokImg();
tiktokImg.setId(imgLongId); tiktokImg.setId(imgLongId);
tiktokImg.setUpdateTime(new Date()); tiktokImg.setUpdateTime(new Date());
tiktokImg.setCollectionNum(collectNum); tiktokImg.setCollectionNum(collectNum+img.getCollectionNum());
tiktokImgMapper.updateById(tiktokImg); tiktokImgMapper.updateById(tiktokImg);
//保存后从 Redis 中删除 //删除缓存
redisService.deleteHashKey(imgCollectNumKey, imgId); redisService.deleteHashKey(imgCollectNumKey, imgId);
} }
log.info("==============redis同步图片收藏数量到db数据完成,耗时【{}】毫秒!===============",System.currentTimeMillis() - startTime); log.info("==============redis同步图片收藏数量到db数据完成,耗时【{}】毫秒!===============",System.currentTimeMillis() - startTime);

33
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/TiktokImgServiceImpl.java

@ -135,4 +135,37 @@ public class TiktokImgServiceImpl implements TiktokImgService {
img.setIsHot(status); img.setIsHot(status);
return tiktokImgMapper.update(img,wrapper); return tiktokImgMapper.update(img,wrapper);
} }
@Override
public void writeDownloadNum(Long imgId) {
String redisKey = RedisKeyConstant.TIKTOK_IMG_DOWNLOAD_NUM_KEY;
redisService.hashIncr(redisKey,String.valueOf(imgId), 1);
}
@Override
public void insertDownloadNum() {
log.info("==============redis同步图片下载数量到db数据开始!===============");
long startTime = System.currentTimeMillis();
String redisKey = RedisKeyConstant.TIKTOK_IMG_DOWNLOAD_NUM_KEY;
Cursor<Map.Entry<Object, Object>> download = redisService.getHashScan(redisKey);
while (download.hasNext()){
Map.Entry<Object, Object> next = download.next();
String imgId = (String) next.getKey();
Long imgLongId = Long.parseLong(imgId);
Integer downloadNum = (Integer) next.getValue();
TiktokImg img = tiktokImgMapper.selectById(imgLongId);
if(img == null){
redisService.deleteHashKey(redisKey, imgId);
continue;
}
TiktokImg tiktokImg = new TiktokImg();
tiktokImg.setId(imgLongId);
tiktokImg.setUpdateTime(new Date());
tiktokImg.setDownloadNum(downloadNum+img.getDownloadNum());
tiktokImgMapper.updateById(tiktokImg);
//删除缓存
redisService.deleteHashKey(redisKey, imgId);
}
log.info("==============redis同步图片下载数量到db数据完成,耗时【{}】毫秒!===============",System.currentTimeMillis() - startTime);
}
} }

32
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/TiktokLikeServiceImpl.java

@ -8,12 +8,14 @@ import com.bnyer.img.domain.TiktokLike;
import com.bnyer.img.enums.TiktokLikeEnum; import com.bnyer.img.enums.TiktokLikeEnum;
import com.bnyer.img.mapper.TiktokImgMapper; import com.bnyer.img.mapper.TiktokImgMapper;
import com.bnyer.img.mapper.TiktokLikeMapper; import com.bnyer.img.mapper.TiktokLikeMapper;
import com.bnyer.img.service.TiktokLikeService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor; import org.springframework.data.redis.core.Cursor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -43,6 +45,8 @@ public class TiktokLikeServiceImpl implements TiktokLikeService {
//拿缓存 //拿缓存
String redisKey = RedisKeyConstant.TIKTOK_USER_LIKE_KEY; String redisKey = RedisKeyConstant.TIKTOK_USER_LIKE_KEY;
Cursor<Map.Entry<Object, Object>> like = redisService.getHashScan(redisKey); Cursor<Map.Entry<Object, Object>> like = redisService.getHashScan(redisKey);
List<TiktokLike> list = new ArrayList<>();
List<String> keyList = new ArrayList<>();
while (like.hasNext()){ while (like.hasNext()){
Map.Entry<Object, Object> next = like.next(); Map.Entry<Object, Object> next = like.next();
String key = (String) next.getKey(); String key = (String) next.getKey();
@ -52,15 +56,30 @@ public class TiktokLikeServiceImpl implements TiktokLikeService {
Long userLongId = Long.parseLong(userId); Long userLongId = Long.parseLong(userId);
String imgId = split[1]; String imgId = split[1];
Long imgLongId = Long.parseLong(imgId); Long imgLongId = Long.parseLong(imgId);
//判断图片是否存在
TiktokImg img = tiktokImgMapper.selectById(imgLongId);
if(img == null){
//不存在则图片Id加入删除列表,遍历完删除
keyList.add(key);
continue;
}
TiktokLike tiktokLike = new TiktokLike(); TiktokLike tiktokLike = new TiktokLike();
tiktokLike.setUserId(userLongId); tiktokLike.setUserId(userLongId);
tiktokLike.setImgId(imgLongId); tiktokLike.setImgId(imgLongId);
tiktokLike.setIsShow("1");
tiktokLike.setSort(0);
tiktokLike.setCreateTime(new Date()); tiktokLike.setCreateTime(new Date());
tiktokLike.setUpdateTime(new Date()); tiktokLike.setUpdateTime(new Date());
tiktokLikeMapper.insert(tiktokLike); list.add(tiktokLike);
//保存后从 Redis 中删除 keyList.add(key);
}
//数据插入数据库并删除缓存
tiktokLikeMapper.insertBatch(list);
if(keyList.size() > 0){
for (String key : keyList) {
redisService.deleteHashKey(redisKey, key); redisService.deleteHashKey(redisKey, key);
} }
}
log.info("==============redis同步点赞记录到db数据完成,耗时【{}】毫秒!===============",System.currentTimeMillis() - startTime); log.info("==============redis同步点赞记录到db数据完成,耗时【{}】毫秒!===============",System.currentTimeMillis() - startTime);
} }
@ -76,12 +95,17 @@ public class TiktokLikeServiceImpl implements TiktokLikeService {
String imgId = (String) next.getKey(); String imgId = (String) next.getKey();
Long imgLongId = Long.parseLong(imgId); Long imgLongId = Long.parseLong(imgId);
Integer greatNum = (Integer) next.getValue(); Integer greatNum = (Integer) next.getValue();
TiktokImg img = tiktokImgMapper.selectById(imgLongId);
if(img == null){
redisService.deleteHashKey(imgLikeNumKey, imgId);
continue;
}
TiktokImg tiktokImg = new TiktokImg(); TiktokImg tiktokImg = new TiktokImg();
tiktokImg.setId(imgLongId); tiktokImg.setId(imgLongId);
tiktokImg.setUpdateTime(new Date()); tiktokImg.setUpdateTime(new Date());
tiktokImg.setGreatNum(greatNum); tiktokImg.setGreatNum(greatNum+img.getGreatNum());
tiktokImgMapper.updateById(tiktokImg); tiktokImgMapper.updateById(tiktokImg);
//保存后从 Redis 中删除 //删除缓存
redisService.deleteHashKey(imgLikeNumKey, imgId); redisService.deleteHashKey(imgLikeNumKey, imgId);
} }
log.info("==============redis同步图片点赞数量到db数据完成,耗时【{}】毫秒!===============",System.currentTimeMillis() - startTime); log.info("==============redis同步图片点赞数量到db数据完成,耗时【{}】毫秒!===============",System.currentTimeMillis() - startTime);

28
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/task/CollectionSyncTask.java

@ -0,0 +1,28 @@
package com.bnyer.img.task;
import com.bnyer.img.service.TiktokCollectionService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author chengkun
* @date 2022/5/12 18:13
*/
@Component
@Slf4j
public class CollectionSyncTask {
@Autowired
private TiktokCollectionService tiktokCollectionService;
@XxlJob("collectionSyncTask")
public ReturnT<String> syncCollection(String param) throws Exception {
tiktokCollectionService.insertNum();
XxlJobLogger.log("{} 我执行了同步收藏记录任务", System.currentTimeMillis());
return ReturnT.SUCCESS;
}
}

28
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/task/DownloadSyncTask.java

@ -0,0 +1,28 @@
package com.bnyer.img.task;
import com.bnyer.img.service.TiktokImgService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author chengkun
* @date 2022/5/12 18:13
*/
@Component
@Slf4j
public class DownloadSyncTask {
@Autowired
private TiktokImgService tiktokImgService;
@XxlJob("collectionSyncTask")
public ReturnT<String> syncDownload(String param) throws Exception {
tiktokImgService.insertDownloadNum();
XxlJobLogger.log("{} 我执行了同步图片下载数量任务", System.currentTimeMillis());
return ReturnT.SUCCESS;
}
}

27
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/task/LikeSyncTask.java

@ -1,10 +1,11 @@
package com.bnyer.img.task; package com.bnyer.img.task;
import com.bnyer.img.service.TiktokLikeService;
import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.handler.annotation.XxlJob; import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
@ -13,12 +14,22 @@ import org.springframework.stereotype.Component;
*/ */
@Component @Component
@Slf4j @Slf4j
@JobHandler(value = "likeSyncTask") public class LikeSyncTask{
public class LikeSyncTask extends IJobHandler {
@Override @Autowired
public ReturnT<String> execute(String param) throws Exception { private TiktokLikeService tiktokLikeService;
System.out.println("123125123123123");
return null; @XxlJob("likeSyncTask")
public ReturnT<String> syncLike(String param) throws Exception {
tiktokLikeService.insertRecord();
XxlJobLogger.log("{} 我执行了同步点赞记录任务", System.currentTimeMillis());
return ReturnT.SUCCESS;
}
@XxlJob("likeNumSyncTask")
public ReturnT<String> syncLikeNum(String param) throws Exception {
tiktokLikeService.insertNum();
XxlJobLogger.log("{} 我执行了同步点赞数量任务", System.currentTimeMillis());
return ReturnT.SUCCESS;
} }
} }

9
bnyer-services/bnyer-img/src/main/resources/com/bnyer/img/mapper/TiktokLikeMapper.xml

@ -16,4 +16,13 @@
<!--@mbg.generated--> <!--@mbg.generated-->
id, user_id, img_id, is_show, create_time, update_time, sort id, user_id, img_id, is_show, create_time, update_time, sort
</sql> </sql>
<insert id="insertBatch">
insert into img_tiktok_like
(user_id, img_id, is_show,create_time,update_time,sort)
VALUES
<foreach collection ="list" item="item" separator =",">
(#{item.userId}, #{item.imgId}, #{item.isShow}, #{item.createTime}, #{item.updateTime}, #{item.sort})
</foreach>
</insert>
</mapper> </mapper>
Loading…
Cancel
Save