规划RedisKeyUtil
为统计方便,同时存两份数据,使用zset数据结构,以时间为分数进行排序
private static final String PREFIX_FOLLOWEE = "followee";
private static final String PREFIX_FOLLOWER = "follower";
//某个用户关注的实体 以当前时间加入set,以便更好的进行业务变化
//followee:userId:entityType -> zset(entityType, now)
public static String getFolloweeKey(int userId, int entityType){
return PREFIX_FOLLOWEE + SPLIT + userId + SPLIT + entityType;
}
//某个实体拥有的粉丝
//follower:entityType:entityId -> zset(userId, now)
public static String getFollowerKey(int entityType, int entityId){
return PREFIX_FOLLOWER + SPLIT + entityType + SPLIT + entityId;
}
FollowService
@Service
public class FollowService implements CommunityConstant {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private UserService userService;
//关注
public void follow(int userId, int entityType, int entityId){
redisTemplate.execute(new SessionCallback() {
@Override
public Object execute(RedisOperations redisOperations) throws DataAccessException {
//用户的关注集合
String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
//实体的粉丝集合
String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
redisOperations.multi();
//opsForZSet:有序集合
redisOperations.opsForZSet().add(followeeKey, entityId, System.currentTimeMillis());
redisOperations.opsForZSet().add(followerKey, userId, System.currentTimeMillis());
return redisOperations.exec();
}
});
}
public void unfollow(int userId, int entityType, int entityId){
redisTemplate.execute(new SessionCallback() {
@Override
public Object execute(RedisOperations redisOperations) throws DataAccessException {
//用户的关注集合
String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
//实体的粉丝集合
String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
redisOperations.multi(); //开启事务
redisOperations.opsForZSet().remove(followeeKey, entityId);
redisOperations.opsForZSet().remove(followerKey, userId);
return redisOperations.exec(); //提交事务
}
});
}
}
FollowController
follow和unfollow处理异步请求
@Controller
public class FollowController implements CommunityConstant{
@Autowired
private FollowService followService;
@Autowired
private HostHolder hostHolder;
@Autowired
private UserService userService;
@RequestMapping(path="/follow", method = RequestMethod.POST)
@ResponseBody
public String follow(int entityType, int entityId){
User user = hostHolder.getUser();
followService.follow(user.getId(), entityType, entityId);
return CommunityUtil.getJSONString(0, "已关注");
}
@RequestMapping(path="/unfollow", method = RequestMethod.POST)
@ResponseBody
public String unfollow(int entityType, int entityId){
User user = hostHolder.getUser();
followService.unfollow(user.getId(), entityType, entityId);
return CommunityUtil.getJSONString(0, "已取消关注");
}
}
profile.js
function follow() {
var btn = this;
if($(btn).hasClass("btn-info")) {
// 关注TA
$.post(
CONTEXT_PATH + "/follow",
{"entityType":3, "entityId":$(btn).prev().val()},
function (data) {
data = $.parseJSON(data);
if(data.code == 0){
window.location.reload();
}
else{
alert(data.msg);
}
}
)
//$(btn).text("已关注").removeClass("btn-info").addClass("btn-secondary");
} else {
// 取消关注
$.post(
CONTEXT_PATH + "/unfollow",
{"entityType":3, "entityId":$(btn).prev().val()},
function (data) {
data = $.parseJSON(data);
if(data.code == 0){
window.location.reload();
}
else{
alert(data.msg);
}
}
)
$(btn).text("关注TA").removeClass("btn-secondary").addClass("btn-info");
}
}
FollowService中添加额外的方法
//查询关注的实体的数量
public long findFolloweeCount(int userId, int entityType){
String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
return redisTemplate.opsForZSet().zCard(followeeKey);
}
//查询实体的粉丝的数量
public long findFollowerCount(int entityType, int entityId){
String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
return redisTemplate.opsForZSet().zCard(followerKey);
}
//查询当前用户是否已关注该实体
public boolean hasFollowed(int userId, int entityType, int entityId){
String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
return redisTemplate.opsForZSet().score(followeeKey, entityId) != null;
}
UserController中getProfilePage方法中补充一些代码逻辑
//关注数量
long followeeCount = followService.findFolloweeCount(userId, ENTITY_TYPE_USER);
model.addAttribute("followeeCount", followeeCount);
//粉丝数量
long followerCount = followService.findFollowerCount(ENTITY_TYPE_USER, userId);
model.addAttribute("followerCount", followerCount);
//是否关注
boolean hasFollowed = false;
if(hostHolder.getUser() != null){
hasFollowed = followService.hasFollowed(hostHolder.getUser().getId(), ENTITY_TYPE_USER, userId);
}
model.addAttribute("hasFollowed", hasFollowed);