redis(二)实战-高并发投票网站

redis(二)实战-高并发投票网站

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

public class ArticleService {

    JedisPool jedisPool = new JedisPool("192.168.71.128", 6379);
    Jedis jedis = jedisPool.getResource();

    public String postArticle(String authorId, String title, String content, String link){
        //设置文章id为自增长
        String articleId = String.valueOf(jedis.incr("article:"));
        //将点赞用户添加到集合中
        String voted = "voted:" + articleId;
        jedis.sadd(voted, authorId);
        jedis.expire(voted, Constants.ONE_WEEK_IN_SECONDS);
        //将文章详情添加到hash中
        long time = System.currentTimeMillis() / 1000;
        String article = "article:" + articleId;
        Map<String, String> articleData = new HashMap<>();
        articleData.put("authorId", authorId);
        articleData.put("content", content);
        articleData.put("link", link);
        articleData.put("title", title);
        articleData.put("now", String.valueOf(time));
        //投票数为1
        articleData.put("votes", "1");
        jedis.hmset(article, articleData);
        //获取时间戳
        //将文章id和时间戳存入zset
        jedis.zadd("score:info", Constants.VOTE_SCORE + time, article);
        jedis.zadd("time", time, article);
        return articleId;
    }

    /**
     * 文章投票功能
     * 需要改变 投票用户列表、文章分值列表
     * voted:articleId userId
     * score:info 400 article:1
     * @param userId
     * @param article
     */
    public void articleVote(String userId, String article){
        //先获取articleId
        String articleId = article.substring(article.indexOf(":") + 1);
        //判断投票是否已经截止
        //获取创建的时间
        String time = jedis.hget(article, "time");
        /*
        long cutoff = (System.currentTimeMillis() / 1000) - Constants.ONE_WEEK_IN_SECONDS;
        Double createTime = jedis.zscore("time:", article);
        if (createTime < cutoff){
            return ;
        }
        */
        if (System.currentTimeMillis() / 1000 - Long.parseLong(time) <= Constants.ONE_WEEK_IN_SECONDS){
            return;
        }
        //将投票用户信息存入投票集合
        Long result = jedis.sadd("voted:" + articleId, userId);
        //添加成功
        if (result == 1){
            //改变文章分值
            jedis.zincrby("score:info", Constants.VOTE_SCORE, article);
            //投票数增加1
            jedis.hincrBy(article, "votes", 1);
        }
    }

    public List<Map<String, String>> getArticles(int page, String key) {
        //page为传入的第几页 start从0开始
        int start = (page - 1) * Constants.ARTICLES_PER_PAGE;
        //结束的下标 默认为start + 24(共25条记录)
        int end = start + Constants.ARTICLES_PER_PAGE - 1;
        //倒序查询出投票数最高的文章,zset有序集合,分值递减
        Set<String> ids = jedis.zrevrange(key, start, end);
        List<Map<String, String>> articles = new ArrayList<>();
        for (String id : ids){
            Map<String, String> article = jedis.hgetAll(id);
            article.put("id", id);
            articles.add(article);
        }
        return articles;
    }
}





评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×