redis高级功能-数据排序

使用场景

排行榜应用,支持多字段排行

功能点分析:

  • 首页
  • [ ] 添加候选人
  • [ ] 分页列表
  • [ ] 排行榜(前十)
  • 详情页
  • [ ] 内容
  • [ ] 投票

接口说明

  • [ ] 添加候选人
  • [ ] 投票
  • [ ] 排名
  • [ ] 显示所有候选人

部分代码实现

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional><!-- optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用devtools,需要重新引入 -->
        </dependency>
    </dependencies>

投票controller

@Controller
public class VoteController {
    private final String VOTE_KEY="votedata";

    private static JedisPool pool = new JedisPool(new JedisPoolConfig(), "192.168.222.188",6379);

    @RequestMapping("/addUser")
    @ResponseBody
    public String addUser(@RequestParam("id") Integer id ,@RequestParam("userName")String userName){
        User user = new User(id,userName);
        pool.getResource().zadd(VOTE_KEY, 0, JSON.toJSONString(user));
        return "success";
    }

    @RequestMapping("/vote")
    @ResponseBody
    public String vote(@RequestParam("id") Integer id ,@RequestParam("userName")String userName){
        User user = new User(id,userName);
        pool.getResource().zincrby(VOTE_KEY,1,JSON.toJSONString(user));
        return "success";
    }

    @RequestMapping("/top")
    @ResponseBody
    public String top(@RequestParam("num")int num){
        Set<String> fset = pool.getResource().zrevrange(VOTE_KEY,0,num-1);
        StringBuffer sb = new StringBuffer();
        for (String set :fset){
            sb.append(set);
        }
        return sb.toString();
    }

    @RequestMapping("/showAll")
    public String showAll(Model model){
        Set<Tuple> fset = pool.getResource().zrevrangeWithScores(VOTE_KEY, 0, -1);
        List<JSONObject> users = new ArrayList();
        for (Tuple user : fset){
            JSONObject userObj = JSON.parseObject(user.getElement());
            userObj.put("score",user.getScore());
            users.add(userObj);
        }
        model.addAttribute("users",users);
        return "vote/vote";
    }
}

voteServiceImpl.java

public class VoteServiceImpl implements VoteService {

    private final String VOTESET_KEY="vote_users";
    private JedisPool jedisPool ;

    public VoteServiceImpl(JedisPool jedisPool){
        this.jedisPool=jedisPool;
    }

    public boolean addUser(User user) {
        Jedis jedis = jedisPool.getResource();
        jedis.zadd(VOTESET_KEY,0, JSON.toJSONString(user));
        return true;
    }

    public List<User> getAll() {
        Jedis jedis = jedisPool.getResource();
        jedis.zrange(VOTESET_KEY,0,-1);
        return null;
    }

    public boolean Vote(User user) {
        return false;
    }

    public List<User> topTen() {
        return null;
    }

    public User getUser(Integer userId) {
        return null;
    }
}

vote.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Title</title>
    <script type="text/javascript" src="/js/jquery-3.3.1.min.js"></script>
</head>
<body>
<table>
    <tr th:each="user : ${users}">
        <td th:text="${user.userName}" />
        <td th:text="${user.score}"/>
        <td>
            <button th:id="${user.id}" th:name="${user.userName}" >vote</button>
        </td>
    </tr>
</table>
    <script type="text/javascript">
        $(document).ready(function(){
            $("button").click(function(){
                var id = this.id;
                var userName = this.name;
                var url = "/vote?id="+id+"&userName="+userName;
                var htmlobj = $.ajax({url:url});
                if(htmlobj.responseText=="success"){
                    $.reload();
                }
            });
        });
    </script>
</body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • 用到的组件 1、通过CocoaPods安装 2、第三方类库安装 3、第三方服务 友盟社会化分享组件 友盟用户反馈 ...
    SunnyLeong阅读 14,711评论 1 180
  • 文/屋下乌 这只是我晚上的随笔,只求不要被人打断,让我写下去才好! 以前我在微博上看到一名作家说如何才能写出一篇...
    如若有一天你发现了我阅读 439评论 0 2
  • 今天的晨读就是为我量身定做的! 因为目前我的生活和学习需要: 001 整理家居(包括打扫卫生和扔扔扔) 002 复...
    陶心暖暖阅读 171评论 0 1
  • 公司处于不同的发展阶段,就会有不同的管理模式,但不管处于哪种管理模式,各管理层之间的信任和配合是不能缺少的。
    潍坊泰华DDM店刘云阅读 68评论 0 0