//一个接口UserService.java
//一个接口的实现UserServiceImpl.java
//两个采用Jedis的客户端测试类FollowTestMain.java,IsFollowTestMain.java
//UserService.java接口如下
package com.redis.test;
import java.util.Set;
public interface UserService {
public void follow(String userId);
public void unfollow(String userId);
public Set<String> following();
public Set<String> followedBy();
public boolean isfollowing(String userId);
public boolean isfollowedBy(String userId);
public Long followingCount();
public Long followerCount();
public Set<String> commonfollowing(String userId);
public Set<String> commonfollowedBy(String userId);
}
//接口实现方法如下:
package com.redis.test;
import java.util.Set;
import redis.clients.jedis.Jedis;
public class UserServiceImpl implements UserService {
private String userId;
private Jedis redisService;
public UserServiceImpl(String userId) {
this.userId = userId;
this.redisService = new Jedis("localhost");
}
/**
*@description 关注对应用户编号方法实现
*@date 2012-4-1
*@parameter
*@see com.redis.test.UserService#follow(java.lang.String)
*/
@Override
public void follow(String userId) {
this.redisService.sadd("graph:user:" + this.userId + ":following",userId); //add
this.redisService.sadd("graph:user:" + userId + ":followed_by",this.userId); //add
}
/**
*@description 获取当前用户所有关注对象的用户编号集合
*@date 2012-4-1
*@parameter
*@see com.redis.test.UserService#following()
*/
@Override
public Set<String> following() {
return this.redisService.smembers("graph:user:" + this.userId + ":following"); //member
}
/**
*@description 获取当前用户被哪些人关注的用户编号集合
*@date 2012-4-1
*@parameter
*@see com.redis.test.UserService#followedBy()
*/
@Override
public Set<String> followedBy() {
return this.redisService.smembers("graph:user:" + this.userId + ":followed_by"); //member
}
/**
*@description 取消关注某人(传入的用户编号标识操作)方法实现
*@date 2012-4-1
*@parameter
*@see com.redis.test.UserService#unfollow(java.lang.String)
*/
@Override
public void unfollow(String userId) {
this.redisService.srem("graph:user:" + this.userId + ":following",userId); //remove
this.redisService.srem("graph:user:" + userId + ":followed_by",this.userId); //remove
}
/**
*@description 判断当前用户是否关注了对应用户编号的用户方法实现
*@date 2012-4-1
*@parameter
*@see com.redis.test.UserService#isfollowing(java.lang.String)
*/
@Override
public boolean isfollowing(String userId) {
return this.redisService.sismember("graph:user:"+this.userId+":following", userId); //is member
}
/**
*@description 判断是否存在对应用户编号的粉丝方法实现
*@date 2012-4-1
*@parameter
*@see com.redis.test.UserService#isfollowedBy(java.lang.String)
*/
@Override
public boolean isfollowedBy(String userId) {
return this.redisService.sismember("graph:user:"+this.userId+":followed_by", userId);//is member
}
/**
*@description 统计当前用户关注的人数总和
*@date 2012-4-1
*@parameter
*@see com.redis.test.UserService#followingCount()
*/
@Override
public Long followingCount() {
return this.redisService.scard("graph:user:"+this.userId+":following"); //card
}
/**
*@description 统计当前用户有多少粉丝数目方法实现
*@date 2012-4-1
*@parameter
*@see com.redis.test.UserService#followerCount()
*/
@Override
public Long followerCount() {
return this.redisService.scard("graph:user:"+this.userId+":followed_by"); //card
}
/**
*@description 获取当前用户和传入用户编号对应的用户共同关注的用户编号集合方法实现
*@date 2012-4-1
*@parameter
*@see com.redis.test.UserService#commonfollowing(java.lang.String)
*/
@Override
public Set<String> commonfollowing(String userId) {
String s1 = "graph:user:" + this.userId + ":following";
String s2 = "graph:user:" + userId + ":following";
return this.redisService.sinter(new String[] { s1, s2 });
}
/**
*@description 获取当前用户和传入用户编号对应的用户共同粉丝的用户编号集合方法实现
*@date 2012-4-1
*@parameter
*@see com.redis.test.UserService#commonfollowedBy(java.lang.String)
*/
@Override
public Set<String> commonfollowedBy(String userId) {
String s1 = "graph:user:" + this.userId + ":followed_by";
String s2 = "graph:user:" + userId + ":followed_by";
return this.redisService.sinter(new String[] { s1, s2 });
}
}
//两个测试类如下:
package com.redis.test;
import java.util.Iterator;
public class FollowTestMain {
//简单测试、没有采用单元测试Junit
public static void main(String[] args) {
UserService user1 = new UserServiceImpl("1");
UserService user2 = new UserServiceImpl("2");
UserService user3 = new UserServiceImpl("3");
user1.follow("2");
user1.follow("3");
user2.follow("1");
user2.follow("3");
user3.follow("1");
user3.follow("2");
Iterator<String> it1 = user1.following().iterator();
System.out.println("user1 following:");
while(it1.hasNext()){
System.out.print(it1.next()+" "); //2 3
}
Iterator<String> it2 = user2.following().iterator();
System.out.println("\nuser2 following:");
while(it2.hasNext()){
System.out.print(it2.next()+" "); //1 3
}
Iterator<String> it3 = user3.following().iterator();
System.out.println("\nuser3 following:");
while(it3.hasNext()){
System.out.print(it3.next()+" "); //1 2
}
Iterator<String> it11 = user1.followedBy().iterator();
System.out.println("\nuser1 followed_by:");
while(it11.hasNext()){
System.out.print(it11.next()+" "); //2 3
}
Iterator<String> it22 = user2.followedBy().iterator();
System.out.println("\nuser2 followed_by:");
while(it22.hasNext()){
System.out.print(it22.next()+" "); //1 3
}
Iterator<String> it33 = user3.followedBy().iterator();
System.out.println("\nuser3 followed_by:");
while(it33.hasNext()){
System.out.print(it33.next()+" "); //1 2
}
}
}
package com.redis.test;
import java.util.Iterator;
public class IsFollowTestMain {
//简单测试、没有采用单元测试Junit
public static void main(String[] args) {
UserService user1 = new UserServiceImpl("1");
UserService user2 = new UserServiceImpl("2");
UserService user3 = new UserServiceImpl("3");
user1.follow("2");
user2.follow("1");
user2.follow("3");
user3.follow("1");
user3.follow("2");
boolean isfollowing = user1.isfollowing("2");
System.out.println("user1 isfollowing user2 result = "+isfollowing); //true
boolean isfollowed = user1.isfollowedBy("3");
System.out.println("user1 isfollowed by user3 result = "+isfollowed); //true
Long followingCount = user1.followingCount();
System.out.println("user1 followingCount = "+followingCount); //1
Long followedCount = user2.followerCount();
System.out.println("user2 followedCount = "+followedCount); //2
Iterator<String> commonFollowing = user1.commonfollowing("3").iterator();
System.out.println("user1 and user3 common following = ");
while(commonFollowing.hasNext()){
System.out.println(commonFollowing.next()); //2
}
Iterator<String> commonFans = user3.commonfollowedBy("1").iterator();
System.out.println("user1 and user3 common fans = ");
while(commonFans.hasNext()){
System.out.println(commonFans.next()); //2
}
}
}
已有 0 人发表留言,猛击->> 这里<<-参与讨论
ITeye推荐