这里使用Redis实现发布文章,获取文章,对文章进行分组作为一个Redis的Helloworld
1.Redis发布文章功能实现
对于每一篇文章,程序都使用一个散列来存储,存储的内容包括:文章的标题,文章的链接
,发布文章的用户,文章发布的时间,投票数量等信息。
发布文章代码:
package com.helloredis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ZParams;
import java.util.*;
/**
* FileName: 文章
* Author: sujinran
* Date: 2018/6/16 20:37
*/
public class Article {
//一天24小时,一小时60分,一分60秒,因此一天有:24*60*60=86400秒,一个星期有7天,因此一周有:7*86400
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;
/**
* 发布文章
*
* @param conn 连接
* @param user 网站用户
* @param title 文章标题
* @param link 文章链接
* @return
*/
public String post_Article(Jedis conn, String user, String title, String link) {
//生成一个x[incr 命令将 key 中储存的数字值+1,String.valueOf()转换为字符串]
String articleId = String.valueOf(conn.incr("article:"));
//某个文章的投票:voted:文章编号
String voted = "voted:" + articleId;
//添加某个文章投票和投票的用户(sadd添加元素到集合sadd set-key item )
conn.sadd(voted, user);
//名单过期时间设置为一周
conn.expire(voted, ONE_WEEK_IN_SECONDS);
long now = System.currentTimeMillis() / 1000;
//文章:article:文章编号
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");
//Hmset 命令用于同时将多个 field-value (字段-值)对设置到哈希表中
conn.hmset(article, articleData);
//Zadd 命令用于将一个或多个成员元素及其分数值加入到有序集当中zadd zset-key 728 member1
conn.zadd("score:", now + VOTE_SCORE, article);
conn.zadd("time:", now, article);
return articleId;
}
}
文章发布功能测试:
import com.helloredis.Article;
import redis.clients.jedis.Jedis;
import java.util.Map;
/**
* FileName: 发布文章测试
* Author: sujinran
* Date: 2018/6/16 22:07
*/
public class post_ArticleTest {
public static void main(String[] args) {
Article article = new Article();
Jedis jedis = new Jedis("127.0.0.1");
jedis.auth("123456");
//发布文章
String articleId = article.post_Article(jedis, "user:1", "redisArticle1", "https://blog.csdn.net/qq_35136982/article/details/80709093#t5");
//遍历获取文章
Map<String, String> articleData = jedis.hgetAll("article:" + articleId);
for (Map.Entry<String, String> entry : articleData.entrySet()) {
System.out.println(" " + entry.getKey() + ": " + entry.getValue());
}
}
}
2.Redis文章投票功能实现
使用有序集合来存储文章发布的时间和文章的评分.使用集合来存储已投票的用户
package com.helloredis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ZParams;
import java.util.*;
/**
* FileName: 文章
* Author: sujinran
* Date: 2018/6/16 20:37
*/
public class Article {
//一天24小时,一小时60分,一分60秒,因此一天有:24*60*60=86400秒,一个星期有7天,因此一周有:7*86400
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;
/**
* 投票功能实现
*
* @param conn redis连接
* @param user 用户
* @param article 发布的文章
*/
public void article_vote(Jedis conn, String user, String article) {
//获得系统的时间,单位为毫秒(1秒=1000毫秒(ms)),转换为秒
long cutoff = (System.currentTimeMillis() / 1000);
判读文章是否过期:zscore 命令返回有序集中,成员的分数值
if (conn.zscore("time:", article) + ONE_WEEK_IN_SECONDS < cutoff) {
System.out.println("投票时间已截止");
return;
}
//从article:id获得文章的ID:article.indexOf(":")转换为:分割的字符串 substring生成新的字符串
String articleId = article.substring(article.indexOf(":") + 1);
判读该用户是否对这篇文章投过票
if (conn.sadd("voted:" + articleId, user) == 1) {
//zincrby()命令对有序集合中指定成员的分数加上增量
conn.zincrby("score:", VOTE_SCORE, article);为score:表中的article自增分数
//HINCRBY命令用于增加存储在字段中存储由增量键哈希的数量
conn.hincrBy(article, "votes", 1);//为article:表中"votes"自增1
}
}
}
投票功能测试:
import com.helloredis.Article;
import redis.clients.jedis.Jedis;
/**
* FileName: 投票功能测试
* Author: sujinran
* Date: 2018/6/16 21:01
*/
public class article_voteTest {
public static void main(String[] args) {
Article article = new Article();
Jedis jedis = new Jedis("127.0.0.1");
jedis.auth("123456");
//获取文章的票数,"article:" + 1="article:1"
String votes1 = jedis.hget("article:" + 1, "votes");
System.out.println("投票之前的票数是: " + votes1);
//投票
article.article_vote(jedis, "user:2", "article:" + 1);
//投票之后
String votes = jedis.hget("article:" + 1, "votes");
System.out.println("我投票了,现在的票数是: " + votes);
assert Integer.parseInt(votes) > 1;
}
}
3.Redis获取文章功能实现
package com.helloredis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ZParams;
import java.util.*;
/**
* FileName: 文章
* Author: sujinran
* Date: 2018/6/16 20:37
*/
public class Article {
//一天24小时,一小时60分,一分60秒,因此一天有:24*60*60=86400秒,一个星期有7天,因此一周有:7*86400
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;
/**
* 获取文章
*
* @param conn
* @param page
* @param order
* @return
*/
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;
}
}
获取文章功能测试:
import com.helloredis.Article;
import redis.clients.jedis.Jedis;
import java.util.List;
import java.util.Map;
/**
* FileName: 获取文章功能测试
* Author: sujinran
* Date: 2018/6/17 13:44
*/
public class getArticlesTest {
public static void main(String[] args) {
Article article = new Article();
Jedis jedis = new Jedis("127.0.0.1");
jedis.auth("123456");
List<Map<String, String>> articles = article.getArticles(jedis, 0, "score:");
getArticlesTest getArticlesTest1 = new getArticlesTest();
getArticlesTest1.printArticles(articles);
}
public 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());
}
}
}
}
4.Redis对文章进行分组功能
package com.helloredis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ZParams;
import java.util.*;
/**
* FileName: 文章
* Author: sujinran
* Date: 2018/6/16 20:37
*/
public class Article {
//一天24小时,一小时60分,一分60秒,因此一天有:24*60*60=86400秒,一个星期有7天,因此一周有:7*86400
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;
/**
* 对文章添加分组
*
* @param conn 连接
* @param articleId 文章编号
* @param toAdd
*/
public void add_Group(Jedis conn, String articleId, String[] toAdd) {
String article = "article:" + articleId;
for (String group : toAdd) {
conn.sadd("group:" + group, article);
}
}}
文章分组测试:
import com.helloredis.Article;
import redis.clients.jedis.Jedis;
/**
* FileName: 添加文章分组
* Author: sujinran
* Date: 2018/6/17 13:57
*/
public class add_GroupTest {
public static void main(String[] args) {
Article article = new Article();
Jedis jedis = new Jedis("127.0.0.1");
jedis.auth("123456");
//发布文章
// String articleId4 = article.post_Article(jedis, "user:4", "redisArticle4", "https://blog.csdn.net/qq_35136982/article/details/80709093#t5");
// String articleId3 = article.post_Article(jedis, "user:3", "redisArticle3", "https://blog.csdn.net/qq_35136982/article/details/80709093#t5");
// String articleId2 = article.post_Article(jedis, "user:2", "redisArticle2", "https://blog.csdn.net/qq_35136982/article/details/80709093#t5");
article.add_Group(jedis,"article:" + 1,new String[]{"new-group1"} );
article.add_Group(jedis,"article:" + 2,new String[]{"new-group1"} );
article.add_Group(jedis,"article:" + 3,new String[]{"new-group2"} );
article.add_Group(jedis,"article:" + 4,new String[]{"new-group2"} );
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/36d3a3fd11bed240736bd5e1225085c8.png)