zoukankan      html  css  js  c++  java
  • redis-投票

      1 package redis.inaction;
      2 
      3 import redis.clients.jedis.Jedis;
      4 import redis.clients.jedis.ZParams;
      5 
      6 import java.util.*;
      7 
      8 public class Chapter01 {
      9     private static final int ONE_WEEK_IN_SECONDS = 7 * 86400;
     10     private static final int VOTE_SCORE = 432;
     11     private static final int ARTICLES_PER_PAGE = 25;
     12 
     13     public static final void main(String[] args) {
     14         new Chapter01().run();
     15     }
     16 
     17     public void run() {
     18         Jedis conn = new Jedis("192.168.7.146",6379);
     19         conn.select(15);
     20         conn.flushAll();
     21         String articleId = postArticle(
     22             conn, "username", "A title", "http://www.google.com");
     23         System.out.println("We posted a new article with id: " + articleId);
     24         System.out.println("Its HASH looks like:");
     25         Map<String,String> articleData = conn.hgetAll("article:" + articleId);
     26         for (Map.Entry<String,String> entry : articleData.entrySet()){
     27             System.out.println("  " + entry.getKey() + ": " + entry.getValue());
     28         }
     29 
     30         System.out.println();
     31 
     32         articleVote(conn, "other_user", "article:" + articleId);
     33         String votes = conn.hget("article:" + articleId, "votes");
     34         System.out.println("We voted for the article, it now has votes: " + votes);
     35         assert Integer.parseInt(votes) > 1;
     36 
     37         System.out.println("The currently highest-scoring articles are:");
     38         List<Map<String,String>> articles = getArticles(conn, 1);
     39         printArticles(articles);
     40         assert articles.size() >= 1;
     41 
     42         addGroups(conn, articleId, new String[]{"new-group"});
     43         System.out.println("We added the article to a new group, other articles include:");
     44         articles = getGroupArticles(conn, "new-group", 1);
     45         printArticles(articles);
     46         assert articles.size() >= 1;
     47     }
     48 
     49     public String postArticle(Jedis conn, String user, String title, String link) {
     50         String articleId = String.valueOf(conn.incr("article:"));
     51 
     52         String voted = "voted:" + articleId;
     53         conn.sadd(voted, user);
     54         conn.expire(voted, ONE_WEEK_IN_SECONDS);
     55 
     56         long now = System.currentTimeMillis() / 1000;
     57         String article = "article:" + articleId;
     58         HashMap<String,String> articleData = new HashMap<String,String>();
     59         articleData.put("title", title);
     60         articleData.put("link", link);
     61         articleData.put("user", user);
     62         articleData.put("now", String.valueOf(now));
     63         articleData.put("votes", "1");
     64         conn.hmset(article, articleData);
     65         conn.zadd("score:", now + VOTE_SCORE, article);
     66         conn.zadd("time:", now, article);
     67 
     68         return articleId;
     69     }
     70 
     71     public void articleVote(Jedis conn, String user, String article) {
     72         long cutoff = (System.currentTimeMillis() / 1000) - ONE_WEEK_IN_SECONDS;
     73         if (conn.zscore("time:", article) < cutoff){
     74             return;
     75         }
     76 
     77         String articleId = article.substring(article.indexOf(':') + 1);
     78         if (conn.sadd("voted:" + articleId, user) == 1) {
     79             conn.zincrby("score:", VOTE_SCORE, article);
     80             conn.hincrBy(article, "votes", 1);
     81         }
     82     }
     83 
     84 
     85     public List<Map<String,String>> getArticles(Jedis conn, int page) {
     86         return getArticles(conn, page, "score:");
     87     }
     88 
     89     public List<Map<String,String>> getArticles(Jedis conn, int page, String order) {
     90          int start = (page - 1) * ARTICLES_PER_PAGE;
     91         int end = start + ARTICLES_PER_PAGE - 1;
     92 
     93         Set<String> ids = conn.zrevrange(order, start, end);
     94         List<Map<String,String>> articles = new ArrayList<Map<String,String>>();
     95         for (String id : ids){
     96             Map<String,String> articleData = conn.hgetAll(id);
     97             articleData.put("id", id);
     98             articles.add(articleData);
     99         }
    100 
    101         return articles;
    102     }
    103 
    104     public void addGroups(Jedis conn, String articleId, String[] toAdd) {
    105         String article = "article:" + articleId;
    106         for (String group : toAdd) {
    107             conn.sadd("group:" + group, article);
    108         }
    109     }
    110 
    111     public List<Map<String,String>> getGroupArticles(Jedis conn, String group, int page) {
    112         return getGroupArticles(conn, group, page, "score:");
    113     }
    114 
    115     public List<Map<String,String>> getGroupArticles(Jedis conn, String group, int page, String order) {
    116         String key = order + group;
    117         if (!conn.exists(key)) {
    118             ZParams params = new ZParams().aggregate(ZParams.Aggregate.MAX);
    119             conn.zinterstore(key, params, "group:" + group, order);
    120             conn.expire(key, 60);
    121         }
    122         return getArticles(conn, page, key);
    123     }
    124 
    125     private void printArticles(List<Map<String,String>> articles){
    126         for (Map<String,String> article : articles){
    127             System.out.println("  id: " + article.get("id"));
    128             for (Map.Entry<String,String> entry : article.entrySet()){
    129                 if (entry.getKey().equals("id")){
    130                     continue;
    131                 }
    132                 System.out.println("    " + entry.getKey() + ": " + entry.getValue());
    133             }
    134         }
    135     }
    136 }

    package redis.inaction;
    import redis.clients.jedis.Jedis;import redis.clients.jedis.ZParams;
    import java.util.*;
    public class Chapter01 {    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 static final void main(String[] args) {        new Chapter01().run();    }
        public void run() {        Jedis conn = new Jedis("192.168.7.146",6379);        conn.select(15);        conn.flushAll();        String articleId = postArticle(            conn, "username", "A title", "http://www.google.com");        System.out.println("We posted a new article with id: " + articleId);        System.out.println("Its HASH looks like:");        Map<String,String> articleData = conn.hgetAll("article:" + articleId);        for (Map.Entry<String,String> entry : articleData.entrySet()){            System.out.println("  " + entry.getKey() + ": " + entry.getValue());        }
            System.out.println();
            articleVote(conn, "other_user", "article:" + articleId);        String votes = conn.hget("article:" + articleId, "votes");        System.out.println("We voted for the article, it now has votes: " + votes);        assert Integer.parseInt(votes) > 1;
            System.out.println("The currently highest-scoring articles are:");        List<Map<String,String>> articles = getArticles(conn, 1);        printArticles(articles);        assert articles.size() >= 1;
            addGroups(conn, articleId, new String[]{"new-group"});        System.out.println("We added the article to a new group, other articles include:");        articles = getGroupArticles(conn, "new-group", 1);        printArticles(articles);        assert articles.size() >= 1;    }
        public String postArticle(Jedis conn, String user, String title, String link) {        String articleId = String.valueOf(conn.incr("article:"));
            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<String,String> articleData = new HashMap<String,String>();        articleData.put("title", title);        articleData.put("link", link);        articleData.put("user", user);        articleData.put("now", String.valueOf(now));        articleData.put("votes", "1");        conn.hmset(article, articleData);        conn.zadd("score:", now + VOTE_SCORE, article);        conn.zadd("time:", now, article);
            return articleId;    }
        public void articleVote(Jedis conn, String user, String article) {        long cutoff = (System.currentTimeMillis() / 1000) - ONE_WEEK_IN_SECONDS;        if (conn.zscore("time:", article) < cutoff){            return;        }
            String articleId = article.substring(article.indexOf(':') + 1);        if (conn.sadd("voted:" + articleId, user) == 1) {            conn.zincrby("score:", VOTE_SCORE, article);            conn.hincrBy(article, "votes", 1);        }    }

        public List<Map<String,String>> getArticles(Jedis conn, int page) {        return getArticles(conn, page, "score:");    }
        public List<Map<String,String>> getArticles(Jedis conn, int page, String order) {         int start = (page - 1) * ARTICLES_PER_PAGE;        int end = start + ARTICLES_PER_PAGE - 1;
            Set<String> ids = conn.zrevrange(order, start, end);        List<Map<String,String>> articles = new ArrayList<Map<String,String>>();        for (String id : ids){            Map<String,String> articleData = conn.hgetAll(id);            articleData.put("id", id);            articles.add(articleData);        }
            return articles;    }
        public void addGroups(Jedis conn, String articleId, String[] toAdd) {        String article = "article:" + articleId;        for (String group : toAdd) {            conn.sadd("group:" + group, article);        }    }
        public List<Map<String,String>> getGroupArticles(Jedis conn, String group, int page) {        return getGroupArticles(conn, group, page, "score:");    }
        public List<Map<String,String>> 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<Map<String,String>> articles){        for (Map<String,String> article : articles){            System.out.println("  id: " + article.get("id"));            for (Map.Entry<String,String> entry : article.entrySet()){                if (entry.getKey().equals("id")){                    continue;                }                System.out.println("    " + entry.getKey() + ": " + entry.getValue());            }        }    }}

  • 相关阅读:
    PHP配置文件处理类
    PHP中实现图片上传的类库
    在PHP中实现StringBuilder类
    微软官方及第三方SDK http://msdn.microsoft.com/zhcn/jj923044
    在PHP中模拟asp的response类
    Atitit.并发测试解决方案(2) 获取随机数据库记录 随机抽取数据 随机排序 原理and实现
    atitit. access token是什么??微信平台公众号开发access_token and Web session保持状态机制
    atitit.二进制数据无损转字符串网络传输
    atitit.重装系统需要备份的资料总结 o84..
    atitit.web ui 结构建模工具总结
  • 原文地址:https://www.cnblogs.com/cowshed/p/11397690.html
Copyright © 2011-2022 走看看