热点数据
经常会被查询,但是不经常被修改或者删除的数据
⾸⻚-详情⻚
链路逻辑
检查缓存是否有
缓存不存在则查询数据库
查询结果放到缓存,设置过期时间
下次访问则命中缓存
代码实现
pojo —热点视频
@Data
@NoArgsConstructor
@AllArgsConstructor
public class VideoDO {
private int id;
private String title;
private String img;
private int price;
pojo —热点卡片
@Data
import java.util.List;
public class VideoCardDO {
private String title;
private int id;
private int weight;
List<VideoDO> list;
dao —模拟数据
import net.xdclass.xdclassredis.model.VideoCardDO;
import net.xdclass.xdclassredis.model.VideoDO;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Repository
public class VideoCardDao {
public List<VideoCardDO> list(){
try {
TimeUnit.MILLISECONDS.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
List<VideoCardDO> cardDOList = new ArrayList<>();
VideoCardDO videoCardDO = new VideoCardDO();
videoCardDO.setId(1);
videoCardDO.setTitle("热门视频");
VideoDO videoDO1 = new VideoDO(1,"springcloud微服务视频","xdclass.net",43);
VideoDO videoDO2 = new VideoDO(2,"Paas工业级项目实战","xdclass.net",32);
VideoDO videoDO3 = new VideoDO(3,"面试专题视频","xdclass.net",43);
VideoDO videoDO4 = new VideoDO(4,"spring源码实战","xdclass.net",4);
List<VideoDO> videoDOS = new ArrayList<>();
videoDOS.add(videoDO1);
videoDOS.add(videoDO2);
videoDOS.add(videoDO3);
videoDOS.add(videoDO4);
videoCardDO.setList(videoDOS);
VideoCardDO videoCardDO2 = new VideoCardDO();
videoCardDO2.setId(2);
videoCardDO2.setTitle("项目实战视频");
VideoDO videoDO5 = new VideoDO(1,"springcloud微服务视频项目","xdclass.net",43);
VideoDO videoDO6 = new VideoDO(2,"Paas工业级项目实战项目","xdclass.net",32);
VideoDO videoDO7 = new VideoDO(3,"面试专题视频项目","xdclass.net",43);
VideoDO videoDO8 = new VideoDO(4,"设计模式视频","xdclass.net",4);
List<VideoDO> videoDOS2 = new ArrayList<>();
videoDOS2.add(videoDO5);
videoDOS2.add(videoDO6);
videoDOS2.add(videoDO7);
videoDOS2.add(videoDO8);
videoCardDO2.setList(videoDOS2);
cardDOList.add(videoCardDO);
cardDOList.add(videoCardDO2);
return cardDOList;
}
}
service
import net.xdclass.xdclassredis.model.VideoCardDO;
import java.util.List;
public interface VideoCardService {
List<VideoCardDO> list();
}
serviceImpl
import net.xdclass.xdclassredis.dao.VideoCardDao;
import net.xdclass.xdclassredis.model.VideoCardDO;
import net.xdclass.xdclassredis.service.VideoCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class VideoCardServiceImpl implements VideoCardService {
@Autowired
private VideoCardDao videoCardDao;
@Override
public List<VideoCardDO> list() {
return videoCardDao.list();
}
}
controller
import net.xdclass.xdclassredis.model.VideoCardDO;
import net.xdclass.xdclassredis.service.VideoCardService;
import net.xdclass.xdclassredis.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.concurrent.TimeUnit;
@RequestMapping("/api/v1/card")
@RestController
public class VideoCardController {
@Autowired
private VideoCardService videoCardService;
@Autowired
private RedisTemplate redisTemplate;
/**
* 缓存key
*/
private static final String VIDEO_CARD_CACHE_KEY = "video:card:key";
/**
* 有缓存
* @return
*/
@GetMapping("list_cache")
public JsonData listCardCache(){
Object cacheObj = redisTemplate.opsForValue().get(VIDEO_CARD_CACHE_KEY);
if(cacheObj != null){
List<VideoCardDO> list = (List<VideoCardDO>) cacheObj;
return JsonData.buildSuccess(list);
} else {
List<VideoCardDO> list = videoCardService.list();
redisTemplate.opsForValue().set(VIDEO_CARD_CACHE_KEY,list,10,TimeUnit.MINUTES);
return JsonData.buildSuccess(list);
}
}
/**
* 无缓存
* @return
*/
@GetMapping("list_nocache")
public JsonData listCardNoCache(){
List<VideoCardDO> list = videoCardService.list();
return JsonData.buildSuccess(list);
}
}