Redis實戰SpringBoot版本之投票服務

redis實戰是本好東東,值得詳細研讀。本文主要是對第一章的投票服務的代碼從jredis改成SpringBoot的redis template版本。java

功能說明

  • 用redis來存儲文章
  • 對文章投票
  • 每一個用戶只能投一次
  • 文章按投票和時間排行
  • 文章打標籤

數據結構選擇

  • 文章採用hash存儲
  • 用redis的incr來實現文章的自增id
  • 用set來記錄每篇文章已投票的用戶
  • 用zset存儲文章與得分,建立時間
  • 用set來存儲標籤裏有哪些文章

常量聲明

/**
     * incr,獲取文章的id
     */
    public static final String KEY_ARTICLE_ID_SEQ = "articleIdSeq";

    /**
     * 數據結構:SET
     * key -- voted:articleId
     * value -- user
     * 記錄每篇文章的投票用戶
     */
    public static final String KEY_VOTE_ARTICLE_PREFIX = "votedSet:";

    /**
     * 數據結構:map
     * key -- articleMap:id
     * value --  article map
     */
    public static final String KEY_ARTICLE_PREFIX = "articleMap:";

    /**
     * 數據結構:zset
     * key -- scoreZSet
     * value -- 得分 articleMap:id
     */
    public static final String KEY_SCORE = "scoreZSet";

    /**
     * 數據結構:zset
     * key -- timeZSet
     * value -- 得分 articleMap:id
     */
    public static final String KEY_TIME = "timeZSet";

    /**
     * 文章標籤前綴
     * 數據結構 set
     * key -- tagSet:tag
     * value -- articleMap:id
     */
    public static final String KEY_TAG_PREFIX = "tagSet:";

    /**
     * 文章用戶投票記錄的過時時間
     */
    public static final int ARTICLE_USER_VOTE_EXPIRE_DAY = 7;

    /**
     * 文章用戶投票記錄的過時時間
     * 秒爲單位
     */
    public static final int ONE_WEEK_IN_SECONDS = 7 * 86400;

    /**
     * 投票得分
     */
    public static final int VOTE_SCORE = 432;

    /**
     * 每頁多少條記錄
     */
    public static final int ARTICLES_PER_PAGE = 25;

主要功能

添加文章

/**
     * 添加文章
     * @param user
     * @param title
     * @param link
     * @return 文章的自增id
     */
    public Long postArticle(String user,String title,String link){
        ValueOperations<String, Long> intOps = redisTemplate.opsForValue();
        Long id = intOps.increment(KEY_ARTICLE_ID_SEQ,1L);

        //本人發佈的文章,自動添加到已投票set中
        voteInternal(id,user);

        Map<String,Object> articleData = new HashMap<String,Object>();
        articleData.put("title", title);
        articleData.put("link", link);
        articleData.put("user", user);
        long nowInSecs = System.currentTimeMillis() / 1000;
        articleData.put("now", String.valueOf(nowInSecs));
        articleData.put("votes", "1"); //主要投票數的計數器

        //保存文章
        String articleKey = formArticleKey(id);
        redisTemplate.opsForHash().putAll(articleKey, articleData);

        //記錄得分和時間
        redisTemplate.boundZSetOps(KEY_SCORE).add(articleKey,nowInSecs + VOTE_SCORE);
        redisTemplate.boundZSetOps(KEY_TIME).add(articleKey,nowInSecs);

        return id;
    }

給文章投票

/**
     * 用戶給文章投票
     * 嚴格一點須要加事務
     * @param user
     * @param id
     */
    public void voteArticle(String user,Long id){
        long now = System.currentTimeMillis() / 1000;
        String articleKey = formArticleKey(id);
        Double score = redisTemplate.boundZSetOps(KEY_SCORE).score(articleKey);
        //文章發佈超過一週,不能投票
        if(score.longValue() + ONE_WEEK_IN_SECONDS < now){
            return;
        }

        //判斷用戶是否能夠投票
        //若已經投票則返回,未投票則投票,並增長投票記錄
        Long tryVote = voteInternal(id,user);
        if(tryVote == 0){
            return;
        }

        //給文章增長投票
        redisTemplate.opsForHash().increment(articleKey,"votes",1L);
        //增長文章的得分
        redisTemplate.boundZSetOps(KEY_SCORE).incrementScore(articleKey,VOTE_SCORE);
    }

根據id獲取文章

/**
     * 根據id獲取文章內容
     * @param id
     * @return
     */
    public ArticleBean getArticleById(Long id){
        Map<String,Object> data = redisTemplate.opsForHash().entries(formArticleKey(id));
        ArticleBean bean = new ArticleBean();
        bean.setId(id);
        try {
            BeanUtils.populate(bean,data);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return bean;
    }

給文章打標籤

/**
     * 給文章打標籤
     * @param id
     * @param tags
     */
    public void tagArticle(Long id,String[] tags){
        String articleKey = formArticleKey(id);
        for(String tag:tags){
            String tagKey = formTagKey(tag);
            redisTemplate.opsForSet().add(tagKey,articleKey);
        }
    }

獲取排行具體實現

/**
     * 根據排序及頁數獲取文章
     * @param page
     * @param zrangeKey
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    private List<ArticleBean> getArticleRank(int page,String zrangeKey){
        int start = (page - 1) * ARTICLES_PER_PAGE;
        int end = start + ARTICLES_PER_PAGE - 1;

        BoundZSetOperations<String,String> ops = redisTemplate.boundZSetOps(zrangeKey);
        //根據得分從大到小
        Set<String> ids = ops.reverseRange(start, end);
        List<ArticleBean> rs = ids.stream().map(new Function<String, ArticleBean>() {
            @Override
            public ArticleBean apply(String id) {
                HashOperations<String, String, String> hashOps = redisTemplate.opsForHash();
                Map<String, String> map = hashOps.entries(id);
                ArticleBean bean = new ArticleBean();
                bean.setId(Long.valueOf(id.substring(id.indexOf(':') + 1)));
                try {
                    BeanUtils.populate(bean, map);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                return bean;
            }
        }).collect(Collectors.toList());
        return rs;
    }

獲取各類排行

/**
     * 根據標籤獲取文章排名
     * @param page
     * @param zrangeKey
     * @param tag
     * @return
     */
    public List<ArticleBean> getArticleRankByTag(int page,String zrangeKey,String tag){
        String scoreTagKey = formScoreTagKey(zrangeKey, tag);
        Boolean exist = redisTemplate.hasKey(scoreTagKey);
        if(!exist){
            Long rs = redisTemplate.opsForZSet().intersectAndStore(formTagKey(tag),zrangeKey,scoreTagKey);
            if(rs == 1){
                redisTemplate.expire(scoreTagKey,1,TimeUnit.MINUTES);
            }
        }
        return getArticleRank(page,scoreTagKey);
    }

    /**
     * 根據得分排序
     * @param page
     * @return
     */
    public List<ArticleBean> getArticleRankByScore(int page){
        return getArticleRank(page,KEY_SCORE);
    }

    /**
     * 根據時間排序
     * @param page
     * @return
     */
    public List<ArticleBean> getArticleRankByTime(int page){
        return getArticleRank(page,KEY_TIME);
    }

junit測試

/**
 * Created by codecraft on 2016-02-11.
 */
public class VoteServiceTest extends RedisdemoApplicationTests{

    @Autowired
    VoteService voteService;

    @Autowired
    RedisTemplate redisTemplate;

    @Test
    public void incrMap(){
        //template.setHashValueSerializer(new StringRedisSerializer(StandardCharsets.UTF_8));
        //這裏得用string寫進去
        redisTemplate.<String,Object>opsForHash().put("user-abcde", "count", "1");
        redisTemplate.opsForHash().increment("user-abcde", "count", 5);

        //error java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
        redisTemplate.<String,Object>opsForHash().put("user-abcde", "now", System.currentTimeMillis());
        System.out.println(redisTemplate.opsForHash().get("user-abcde", "now"));
    }

    @Test
    public void delete(){
        for(int i=0;i<100;i++){
            voteService.delete(i+1L);
        }
        redisTemplate.delete("articleIdSeq");
    }

    @Test
    public void post() throws InterruptedException {
        for(int i=0;i<100;i++){
            voteService.postArticle("user" + i, "title" + i, "link" + i);
            Thread.sleep(1000);
        }
        for(int i=0;i<100;i++){
            ArticleBean bean  = voteService.getArticleById(i + 1L);
            System.out.println(ToStringBuilder.reflectionToString(bean));
        }
    }

    @Test
    public void vote(){
        Random random = new Random();
        IntStream.range(1,31).forEach(x -> {
            Long articleId = random.nextInt(100) + 1L;
            String user = "user" + (random.nextInt(100) + 1);
            voteService.voteArticle(user,articleId);
        });
    }

    @Test
    public void addTag(){
        Random random = new Random();
        IntStream.range(1,31).forEach(x -> {
            Long articleId = random.nextInt(100) + 1L;
            String tag = "tag" + (random.nextInt(5) + 1);
            voteService.tagArticle(articleId, new String[]{tag});
        });
    }

    @Test
    public void getByScore(){
        List<ArticleBean> data = voteService.getArticleRankByScore(1);
        data.stream().forEach(System.out::println);
    }

    @Test
    public void getByTime(){
        List<ArticleBean> data = voteService.getArticleRankByTime(1);
        data.stream().forEach(System.out::println);
    }

    @Test
    public void getByTag(){
        List<ArticleBean> data = voteService.getArticleRankByTag(1, VoteService.KEY_SCORE,"tag1");
        data.stream().forEach(System.out::println);
    }
}
相關文章
相關標籤/搜索