Redis(案例三:天热销视频榜单实战-List数据)
作者:互联网
需求
1.⼩滴课堂官⽹需要⼀个视频学习榜单,每天更新⼀次
2.需要⽀持⼈⼯运营替换榜单位置
企业中流程
1.定时任务计算昨天最多⼈学习的视频
2.晚上12点到1点更新到榜单上
3.预留⼀个接⼝,⽀持⼈⼯运营
类似场景
京东:热销⼿机榜单、电脑榜单等
百度:搜索热榜
疑惑:为啥不是实时计算?
真正⾼并发下项⽬,都是预先计算好结果,然后直接返回数据,且存储结构最简单
开发接⼝
import net.xdclass.xdclassredis.model.VideoDO;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("api/v1/rank")
public class RankController {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
private static final String DAILY_RANK_KEY = "video:rank:daily";
@RequestMapping("daily_rank")
public JsonData videoDailyRank(){
List<VideoDO> list = redisTemplate.opsForList().range(DAILY_RANK_KEY,0,-1);
return JsonData.buildSuccess(list);
}
}
测试数据
import net.xdclass.xdclassredis.model.UserDO;
import net.xdclass.xdclassredis.model.VideoDO;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import java.io.Serializable;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@SpringBootTest
class XdclassRedisApplicationTests {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Test
public void saveRank(){
String DAILY_RANK_KEY = "video:rank:daily";
VideoDO video1 = new VideoDO(3,"PaaS工业级微服务大课","xdclass.net",1099);
VideoDO video2 = new VideoDO(5,"AlibabaCloud全家桶实战","xdclass.net",59);
VideoDO video3 = new VideoDO(53,"SpringBoot2.X+Vue3综合实战","xdclass.net",49);
VideoDO video4 = new VideoDO(15,"玩转23种设计模式+最近实战","xdclass.net",49);
VideoDO video5 = new VideoDO(45,"Nginx网关+LVS+KeepAlive","xdclass.net",89);
redisTemplate.opsForList().leftPushAll(DAILY_RANK_KEY,video5,video4,video3,video2,video1);
}
}
人工运营操作榜单
import net.xdclass.xdclassredis.model.UserDO;
import net.xdclass.xdclassredis.model.VideoDO;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import java.io.Serializable;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@SpringBootTest
class XdclassRedisApplicationTests {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Test
public void replaceRank(){
String DAILY_RANK_KEY = "video:rank:daily";
VideoDO video = new VideoDO(5432,"小滴课堂面试专题第一季","xdclass.net",323);
redisTemplate.opsForList().set(DAILY_RANK_KEY,1,video);
}
}
标签:榜单,List,Redis,VideoDO,springframework,xdclass,import,org,net 来源: https://blog.csdn.net/qq_53609683/article/details/123642321