美文网首页我爱编程
redis高级功能-数据排序

redis高级功能-数据排序

作者: tuoxie119 | 来源:发表于2018-04-07 22:52 被阅读0次

    使用场景

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

    功能点分析:

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

    接口说明

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

    部分代码实现

    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>
    

    相关文章

      网友评论

        本文标题:redis高级功能-数据排序

        本文链接:https://www.haomeiwen.com/subject/wnuuhftx.html