发表于: 2019-12-20 21:50:06

1 645


一、今天完成的事

1.修改出现的bug

重新整理了查询点赞收藏列表的接口


   /**
    * 所有视频列表
    * @param video
    * @return
    */
   @Select("<script>select distinct video.id,video.title, video.grade,video.subject,video.video_image,video.number_of_likes, \n" +
           "video.number_of_collections,video.classification,video.video_url,video.content,video.introduction,video.update_at,\n" +
           "teacher.teacher_id,teacher.teacher_name,teacher.teacher_image,video_collection.collection_sid,video_like.like_sid from video \n" +
           "left join teacher_video on teacher_video.vid =video.id   \n" +
           "left join teacher on teacher.teacher_id = teacher_video.tid\n" +
           "left join video_collection on video_collection.vid=id and video_collection.collection_sid=#{collection_sid}\n" +
           "left join video_like on video_like.vid=id and video_like.like_sid=#{like_sid}\n" +
           "<where>  1=1  " +
           "<if test= \"grade != null\">and video.grade=#{grade}</if>\n"+
           "<if test= \"subject != null\">and video.subject=#{subject}</if>\n"+
           "<if test= \"classification != null\">and video.classification=#{classification}</if>\n" +
           "</where> order by update_at desc"+
           "</script>\n")
   List<Video> selectVideoList(Video video);

2.调整了点赞收藏接口

/**
    * 收藏
    *
    * @param vid
    * @param key   认证码 1收藏,0取消
    * @param token
    * @return
    */
   @PutMapping(value = "/u/video/updateCollection")
   private boolean updateCollection(int key, Long vid, String token) throws Exception {
       Video video = wxVideoService.queryVideoById(vid);
       //获取id
       WxVideoController wxVideoController = new WxVideoController();
       String openId = wxVideoController.getIdFromOpenId(token);
       Long sid = wxVideoService.queryStudentId(openId);
       int collections = video.getNumber_of_collections();
       if (key == 1) {
           collections += 1;
           wxVideoService.setVideoCollection(video.getId(), sid);
       } else {
           collections -= 1;
           wxVideoService.delVideoCollection(video.getId(), sid);
       }
       return wxVideoService.setCollection(vid, collections);

   }

   /**
    * 点赞
    *
    * @param vid 视频id
    * @param key 认证码 1 点赞,0取消
    * @return boolen
    */
   @PutMapping(value = "/u/video")
   private boolean setLike(int key, Long vid, String token) throws Exception {
       Video video = wxVideoService.queryVideoById(vid);
       //获取sid
       WxVideoController wxVideoController = new WxVideoController();
       String openId = wxVideoController.getIdFromOpenId(token);
       Long sid = wxVideoService.queryStudentId(openId);
       int like = video.getNumber_of_likes();
       if (key == 1) {
           like += 1;
           wxVideoService.addVideoLike(video.getId(), sid);
       } else {
           like -= 1;
           wxVideoService.delVideoLike(video.getId(), sid);
       }
       return wxVideoService.setLike(like, vid);
   }

3.加入缓存

前台视频

package com.jnshu.video.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jnshu.model.Video;
import com.jnshu.utils.DesUtil;
import com.jnshu.video.service.WxVideoService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
* @author Admin
* @PackageName com.jnshu.academyctrlwebclient.controller
* @ClassName ctrl
* @Description
* @create 2019-11-29 14:05
*/
@RestController
@RequestMapping("wx/video/a")
public class WxVideoController {

   private static final Logger log = LogManager.getLogger(WxVideoController.class);

   @Autowired
   WxVideoService wxVideoService;

   /**
    * 获取列表
    *
    * @param start
    * @return
    * @throws Exception
    */
   @GetMapping(value = "/u/videoList")
   private PageInfo<Video> getVideoList(@RequestParam(value = "start", defaultValue = "1") int start,
                                        @RequestParam(value = "size", defaultValue = "10") int size,
                                        Video video, String token) throws Exception {
       PageHelper.startPage(start, size);
       List<Video> videoList = wxVideoService.selectVideoList(video, token);
       return new PageInfo<>(videoList);
   }

   /**
    * 视频详情
    *
    * @param
    * @return
    */
   @GetMapping(value = "/u/video")
   private JSONObject getVideo(Long id, String token) throws Exception {
       JSONObject jsonObject = new JSONObject();
       //获取sid
       WxVideoController wxVideoController = new WxVideoController();
       String openId = wxVideoController.getIdFromOpenId(token);
       Long sid = wxVideoService.queryStudentId(openId);
       jsonObject.put("code", 200);
       if (wxVideoService.queryVideoLike(id, sid) != null) {
           jsonObject.put("like", true);
       } else {
           jsonObject.put("like", false);
       }
       if (wxVideoService.queryVideoCollection(id, sid) != null) {
           jsonObject.put("collection", true);
       } else {
           jsonObject.put("collection", false);
       }
       jsonObject.put("data", wxVideoService.queryVideoById(id));
       return jsonObject;
   }


   /**
    * 收藏
    *
    * @param vid
    * @param key   认证码 1收藏,0取消
    * @param token
    * @return
    */
   @PutMapping(value = "/u/video/updateCollection")
   private boolean updateCollection(int key, Long vid, String token) throws Exception {
       Video video = wxVideoService.queryVideoById(vid);
       //获取id
       WxVideoController wxVideoController = new WxVideoController();
       String openId = wxVideoController.getIdFromOpenId(token);
       Long sid = wxVideoService.queryStudentId(openId);
       int collections = video.getNumber_of_collections();
       if (key == 1) {
           collections += 1;
           wxVideoService.setVideoCollection(video.getId(), sid);
       } else {
           collections -= 1;
           wxVideoService.delVideoCollection(video.getId(), sid);
       }
       return wxVideoService.setCollection(vid, collections);

   }

   /**
    * 点赞
    *
    * @param vid 视频id
    * @param key 认证码 1 点赞,0取消
    * @return boolen
    */
   @PutMapping(value = "/u/video")
   private boolean setLike(int key, Long vid, String token) throws Exception {
       Video video = wxVideoService.queryVideoById(vid);
       //获取sid
       WxVideoController wxVideoController = new WxVideoController();
       String openId = wxVideoController.getIdFromOpenId(token);
       Long sid = wxVideoService.queryStudentId(openId);
       int like = video.getNumber_of_likes();
       if (key == 1) {
           like += 1;
           wxVideoService.addVideoLike(video.getId(), sid);
       } else {
           like -= 1;
           wxVideoService.delVideoLike(video.getId(), sid);
       }
       return wxVideoService.setLike(like, vid);
   }

   /**
    * 内部处理token获取openId
    *
    * @param token
    * @return
    */
   public String getIdFromOpenId(String token) throws Exception {
       DesUtil desUtil = new DesUtil();
       String desToken = desUtil.decrypt(token);
       String[] splitToken = desToken.split("/");
       return splitToken[0];
   }
}

前台签到

package com.jnshu.checkin.service.serviceImpl;

import com.jnshu.checkin.mapper.CheckinMapper;
import com.jnshu.checkin.service.CheckinService;
import com.jnshu.model.Student;

import com.jnshu.model.StudentCheckin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Admin
* @PackageName com.jnshu.checkin.service
* @ClassName academy-ctrl-wx-provider
* @Description
* @create 2019-12-10 11:20
*/
@Service
public class CheckinServiceImpl implements CheckinService {

   @Autowired
   CheckinMapper checkinMapper;

   @Autowired
   RedisTemplate redisTemplate;

   @Override
   public Student getStudent( Long id) {
       //查询连续签到信息
       Student student = new Student();
       if(redisTemplate.opsForValue().get("studentByCheckin") != null){
           student = (Student) redisTemplate.opsForValue().get("studentByCheckin");
       }else {
           StudentCheckin studentCheckin = checkinMapper.getDate(id);
           long verifyDate = (System.currentTimeMillis())-86400000L;
           if(studentCheckin==null||studentCheckin.getDate()<(verifyDate)){
               student.setCurrent_continued_check(0);
               boolean status =  checkinMapper.clearCheckin(student.getCurrent_continued_check(),id);
               System.out.println(status);
           }
           student = checkinMapper.getStudent(id);
       }
       return student;
   }

   @Override
   public Student getStudentID(String openId) {
       Student student = null;
       if(redisTemplate.opsForValue().get("studentIdByCheckin") != null){
           student = (Student) redisTemplate.opsForValue().get("studentIdByCheckin");
       }else {
           student =  checkinMapper.getStudentID(openId);
           redisTemplate.opsForValue().set("studentIdByCheckin",student);
       }
       return student;
   }

   @Override
   public List<StudentCheckin> getCheckinDate(Long id) {
       List<StudentCheckin> studentCheckinList = null;
       if(redisTemplate.opsForList().size("studentCheckinList") != 0){
           System.out.println("从缓存中取");
           studentCheckinList = redisTemplate.opsForList().range("studentCheckinList",0,-1);
       }else {
           System.out.println("从数据库中查询");
           studentCheckinList = checkinMapper.getCheckinDate(id);
           redisTemplate.opsForList().rightPushAll("studentCheckinList",studentCheckinList);
       }
       return studentCheckinList;
   }

   @Override
   public int totalCheck(Long id) {
       return checkinMapper.totalCheck(id);
   }

   @Override
   public boolean currentCheck(Long id)  {
       return checkinMapper.currentCheck(id);
   }

   @Override
   public boolean clearCheckin(int current_continued_check,Long id) {
       return checkinMapper.clearCheckin(current_continued_check,id);
   }

   @Override
   public StudentCheckin getDate(Long id) {
       return checkinMapper.getDate(id);
   }

   @Override
   public boolean addCheckin(Long id, Long date) {
       return checkinMapper.addCheckin(id, date);
   }

   @Override
   public boolean updateCheckin(Student student) {

       System.out.println(checkinMapper.totalCheck(student.getId()));
       //增加累计签到
       student.setTotal_check(checkinMapper.totalCheck(student.getId())+1);
       //增加当前连续签到
       student.setCurrent_continued_check(student.getCurrent_continued_check()+1);
       //修改最高连续签到次数
       if(student.getHighest_continued_check()<student.getCurrent_continued_check()){
           student.setHighest_continued_check(student.getCurrent_continued_check());
       }
       //增加逆袭豆数量
       Long beansAmount = student.getBeans_amount();
       if(student.getCurrent_continued_check()<5){
           student.setBeans_amount(beansAmount+(long) student.getCurrent_continued_check());
       }else{
           student.setBeans_amount(beansAmount+5L);
       }
       return checkinMapper.updateCheckin(student);
   }

   @Override
   public boolean cleanTable() {
       return checkinMapper.cleanTable();
   }

}

未加缓存之前本地压测

加了缓存后本地压测

效果很明显

二、遇到的问题

怎么实现redis  自动清除key

三、收获

四、明天的计划

解决问题




返回列表 返回列表
评论

    分享到