redis高级功能-数据排序

使用场景

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

功能点分析:

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

接口说明

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

部分代码实现

pom.xml


        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            redis.clients
            jedis
        
        
            com.alibaba
            fastjson
        
        
            org.projectlombok
            lombok
        
        
            org.mybatis
            mybatis
        
        
            com.github.pagehelper
            pagehelper
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-devtools
            true
        
    

投票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 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 fset = pool.getResource().zrevrangeWithScores(VOTE_KEY, 0, -1);
        List 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 getAll() {
        Jedis jedis = jedisPool.getResource();
        jedis.zrange(VOTESET_KEY,0,-1);
        return null;
    }

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

    public List topTen() {
        return null;
    }

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

vote.html




    Title
    


你可能感兴趣的:(redis高级功能-数据排序)