用redis实现一个投票(java版)

代码

import java.util.*;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.ZParams;

public class Vote {
    private static final int ONE_WEEK_IN_SECONDS = 7 * 86400;
    private static final int VOTE_SCORE = 432;
    private static final int ARTICLES_PER_PAGE = 25;

    public String postArticle(Jedis conn,String user,String title,String link){//发表文章
        String articleId=String.valueOf(conn.incr("article:"));//自动匹配一个文章id号
        String voted="voted:"+articleId;
        conn.sadd(voted, user);  //表示该用户已为这篇文章投票。
        conn.expire(voted, ONE_WEEK_IN_SECONDS);//给这篇文章设定投票期限
        long now=System.currentTimeMillis()/1000;
        String article="article:"+articleId;
        HashMap articleData=new HashMap<>();//对文章的信息进行封装
        articleData.put("title",title);
        articleData.put("link", link);
        articleData.put("user", user);
        articleData.put("now", String.valueOf(now));
        articleData.put("votes", "1");
        articleData.put("againsts", "0");
        conn.hmset(article, articleData);//hmset用于同时将多个键值对存储于一个哈希表内
        conn.zadd("score:", now+VOTE_SCORE,article);//将文章添加到根据评分排序的有序集合
        conn.zadd("time:", now, article);//将文章添加到根据时间排序的有序集合中
        return articleId;
    }
    public void articleVote(Jedis conn,String user,String article,boolean against){//投票或者投反对票
        long cutoff=(System.currentTimeMillis()/1000);//
        if(conn.zscore("time:", article)+ONE_WEEK_IN_SECONDS//判读文章是否过期
            return;
        }
        String articleId=article.substring(article.indexOf(":")+1);
        if(against){ //投反对票
            if(conn.sadd("against:"+articleId, user)==1){
                conn.hincrBy(article, "against", 1);  //给反对票数组自增1
            }
        }
        else if(conn.sadd("voted:"+articleId, user)==1){//判读该用户是否对这篇文章投过票
            conn.zincrby("score:", VOTE_SCORE, article);//为score:表中的article自增分数
            conn.hincrBy(article, "votes", 1);//为article:表中"votes"自增1
        }
    }
    public void addGroups(Jedis conn,String articleId,String[] toAdd){
        String article="article:"+articleId;
        for(String group:toAdd){
            conn.sadd("group:"+group, article);
        }
    }
    public void removeGroups(Jedis conn,String articleId,String[] toRemove){
        String article="article:"+articleId;
        for(String group:toRemove){
            conn.srem("group:"+group, article);
        }
    }
    public List> getArticles(Jedis conn,int page){
        return getArticles(conn, page, "score:");
    }
    public List> getArticles(Jedis conn,int page,String order){//根据页数和排序方式返回所有文章
        int start=(page-1)*ARTICLES_PER_PAGE;
        int end=start+ARTICLES_PER_PAGE-1;
        Set ids=conn.zrevrange(order, start, end);//获取从大到小的顺序的集合
        List> articles=new ArrayList<>();
        for(String id:ids){
            Map articleData=conn.hgetAll(id);
            articleData.put("id", id);
            articles.add(articleData);  
        }
        return articles;
    }
    public List> getGroupArticles(Jedis conn,String group,int page){
        return getGroupArticles(conn, group, page, "score:");
    }
    public List> getGroupArticles(Jedis conn,String group,int page,String order){
        String key=order+group;
        if(!conn.exists(key)){
            ZParams params=new ZParams().aggregate(ZParams.Aggregate.MAX);
            conn.zinterstore(key, params,"group:"+group,order);
            conn.expire(key, 60);                                    
        }
        return getArticles(conn, page, key);
    }
    private void printArticles(List> articles){
        for(Map article:articles){
            System.out.println("  id: "+article.get("id"));
            for(Map.Entry entry:article.entrySet()){
                if(entry.getKey().equals("id"))
                    continue;
                System.out.println("    "+entry.getKey()+": "+entry.getValue());
            }       
        }
    }
}

你可能感兴趣的:(redis学习笔记)