事先说明:文章所使用的代码均为书籍赠送的代码,非本人写的。只是在上面做了点注解与解释
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
运行结果: