Redis实战-chapter1

事先说明:文章所使用的代码均为书籍赠送的代码,非本人写的。只是在上面做了点注解与解释

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

import java.security.IdentityScope;
import java.util.*;

import javax.print.DocFlavor.STRING;
public class chapter1 {
	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 void main(String[] args) {
		new chapter1().run();
	}
	
	public void run(){
		Jedis conn = new Jedis("localhost");
		conn.select(15);
		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 articleData = conn.hgetAll("article:" + articleId);
	    for (Map.Entry entry : articleData.entrySet()){
	    	System.out.println("  " + entry.getKey() + ": " + entry.getValue());
	    }

	    System.out.println("1");
	    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("2");
	    articleOppose(conn, "other_user", "article:" + articleId);
	    String opposes = conn.hget("article:" + articleId, "oppose");
	    System.out.println("We oppose for the article, it now has votes: " + opposes);
	    assert Integer.parseInt(opposes) > 1;
	    
	    System.out.println("3");
	    System.out.println("The currently highest-scoring articles are:");
	    List> articles = getArticles(conn, 1);
	    printArticles(articles);
	    assert articles.size() >= 1;

	    System.out.println("4");
	    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 boolean articleOppose(Jedis conn,String user,String article){
		long cutoff = (System.currentTimeMillis()/1000)-ONE_WEEK_IN_SECONDS;
		if(conn.zscore("time:", article)> 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;
		
		Set idsSet = conn.zrevrange(order, start, end);
		List> articles = new ArrayList>();
		for (String id:idsSet)
		{
			Maparticles_data = conn.hgetAll(id);
			articles_data.put("id", id);
			articles.add(articles_data);
		}
		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;//score:sport
		if(!conn.exists(key)){
			//设置键值分值,
			ZParams params = new ZParams().aggregate(ZParams.Aggregate.MAX);
			conn.zinterstore(key,params,"group:"+group,order);
			//key设置键的过期时间为60。
			conn.expire(key, 60);
		}
		return getArticles(conn, page,key);
		
		
	}
	
	public void addGroups(Jedis conn,String articleId,String[] toAdd)
	{
		String article = "article:"+articleId;
		for (String group:toAdd){
			conn.sadd("group:"+group,article);
		}
	}
	
	public String postArticle(Jedis conn,String user,String title,String link)
	{
		//hash通过article:来递增文章编号,返回递增后的数值
		String articleId = String.valueOf(conn.incr("article:"));
		
		String voted = "voted:"+articleId;
		String opposed = "opposed" + articleId;
		//set通过voted:92737(文章编号)添加的自己的id(user:111)
		conn.sadd(voted,user);
		conn.sadd(opposed,user);
		//key设置键的过期时间为ONE_WEEK_IN_SECONDS。
		conn.expire(voted, ONE_WEEK_IN_SECONDS);
		conn.expire(opposed, 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("opposes", "0");
		conn.hmset(article, articleData);
		//zset加入到根据发布时间(score:)排序的有序集合
		//zset加入到根据评分(time:)排序的有序集合
		conn.zadd("score:", now+VOTE_SCORE,article);
		conn.zadd("time:", now,article);
		
		return articleId;
	}
	
	private void printArticles(List> articles){
		for(Map article:articles){
			System.out.println(" id: "+ article.get("id"));
			for(Map.Entry entry:article.entrySet()){
				//通过Map.entrySet遍历key和value
				//entry类似于单元,将map内的每个元素作为作为一个单元
				//entry可以通过getKey与getValue方法进行大容量的数据历遍
				if(entry.getKey().equals("id")){
					continue;
				}
				System.out.println("   "+entry.getKey()+": "+entry.getValue());
			}
		}
	}

}

运行结果:

Redis实战-chapter1_第1张图片

你可能感兴趣的:(java,redis)