redis投票系统--学习准备

近来学习redis,主要是看书籍为主,看的是redis实战(redis in action),里面有一个投票的章节,就打算以javaWeb的形式展现出来,显示研究了redis api,书中用的是Python,然后找到了java语言的,并通过测试的方式把所有功能代码实现了一遍,为后续的开发做准备。一下是主要代码:

 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;
	 /**
	  * 发布文章
	  */
	@Test
	public void postArticle(){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:config/spring-*.xml");
		JedisPool pool = (JedisPool) applicationContext.getBean("redisClient");
		Jedis jedis = pool.getResource();
		//articleId自增长
		String articleId = String.valueOf(jedis.incr("article:"));
		//将发布文章的用户关联到一头偏偏名单中,并设置过期时间为一周
		   String voted = "voted:" + articleId;
	       jedis.sadd(voted, "user");
	       jedis.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");
	        jedis.hmset(article, articleData);
	        
	        //经文章添加到按时间和按投票次数排序的有序集合中
	        jedis.zadd("score:", now + VOTE_SCORE, article);
	        jedis.zadd("time:", now, article);
		System.out.println(articleData);
	}
	//投票
	@Test
	 public void articleVote() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:config/spring-*.xml");
		JedisPool pool = (JedisPool) applicationContext.getBean("redisClient");
		Jedis jedis = pool.getResource();
		String article ="article:5" ;
	        long cutoff = (System.currentTimeMillis() / 1000) - ONE_WEEK_IN_SECONDS;
	        //判断是否过期
	        if (jedis.zscore("time:", "article:5") < cutoff){
	        	System.out.println("投票已经结束");
	            return;
	        }
	        String articleId = article.substring(article.indexOf(':') + 1);
	        if (jedis.sadd("voted:" + articleId, "other_user2") == 1) {
	            jedis.zincrby("score:", VOTE_SCORE, article);
	            jedis.hincrBy(article, "votes", 1l);
	        }
	    }
	//根据投票数查看
	@Test
	 public void getArticles() {
			ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:config/spring-*.xml");
			JedisPool pool = (JedisPool) applicationContext.getBean("redisClient");
			Jedis jedis = pool.getResource();
			int page = 1;
			//根据投票数
			//String order ="score:";
			//根据发起时间
			String order ="time:";
	        int start = (page - 1) * ARTICLES_PER_PAGE;
	        int end = start + ARTICLES_PER_PAGE - 1;
	        Set ids = jedis.zrevrange(order, start, end);
	        List> articles = new ArrayList>();
	        for (String id : ids){
	            Map articleData = jedis.hgetAll(id);
	            articleData.put("id", id);
	            articles.add(articleData);
	        }
	        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());
	            }
	        }
	    }
	//添加分组
	@Test
	 public void addGroups() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:config/spring-*.xml");
		JedisPool pool = (JedisPool) applicationContext.getBean("redisClient");
		Jedis jedis = pool.getResource();
		 String[] toAdd = new String[]{"new-group"};
	        String article = "article:" + "2";
	        for (String group : toAdd) {
	        	//添加至分组
	           // jedis.sadd("group:" + group, article);
	        	//从分组中删除
	            jedis.srem("group:" + group, article);
	        }
	    }
	//的到分组中的文章
	@Test
	public void getGroupArticles() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:config/spring-*.xml");
		JedisPool pool = (JedisPool) applicationContext.getBean("redisClient");
		Jedis jedis = pool.getResource();
		String group="new-group";
		int page =1;
		String order="score:";
        String key = order + group;
        if (!jedis.exists(key)) {
            ZParams params = new ZParams().aggregate(ZParams.Aggregate.MAX);
            jedis.zinterstore(key, params, "group:" + group, order);
            jedis.expire(key, 60);
        }
        getArticles(jedis, page, key);
    }
	 public List> getArticles(Jedis jedis, int page, String order) {
	        int start = (page - 1) * ARTICLES_PER_PAGE;
	        int end = start + ARTICLES_PER_PAGE - 1;

	        Set ids = jedis.zrevrange(order, start, end);
	        List> articles = new ArrayList>();
	        for (String id : ids){
	            Map articleData = jedis.hgetAll(id);
	            articleData.put("id", id);
	            articles.add(articleData);
	        }
	        return articles;
	    }

(参考书籍redis in action)

你可能感兴趣的:(Redis)