微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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);

	}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐