Redis事务
A关注B时,A的关注列表中有B,B的粉丝列表中有A,这两件事必须要同时发生,因此使用事务可以一次执行多个命令,保证了一致性
一个事务从开始到执行会经历以下三个阶段:
- 开始事务。jedis.multi()(返回事务Transaction类型,如trans)
- 命令入队。在上一步返回的事务类型上执行操作,如trans.zadd(xxx,xxx,xxx)
- 执行事务。使用exec()执行事务,如trans.exec(),EXEC命令的返回值是这些命令的返回值组成的列表,返回值顺序和命令的顺序相同
注意:事务执行完成之后,要将事务关闭
具体实现
FollowService功能
PS:首先在Redis的Key生成工具中添加关注和粉丝zset的统一命名方法
public String getWhoIFollowListKey(int user_id,int entity_type)
{
return String.valueOf(user_id)+"'s-Follow-List"+String.valueOf(entity_type);
}
public String getWhoFollowedMeList(int entity_id,int entity_type)
{
return String.valueOf(entity_id)+"-"+String.valueOf(entity_type)+"-Fans-List";
}
1.关注
public boolean followSomeone(int entity_type,int entity_id,int user_id)
{
String fansKey= new RedisKeyUtil().getWhoFollowedMeList(entity_id,entity_type);
String followingKey=new RedisKeyUtil().getWhoIFollowListKey(user_id,entity_type);
//得到关注和粉丝列表的key,这两者需要同时更新
Jedis j=jedis.getJedis();
Date date=new Date();
Transaction tx=jedis.multi(j);
//开始事务
tx.zadd(followingKey,date.getTime(),String.valueOf(entity_id));
tx.zadd(fansKey,date.getTime(),String.valueOf(user_id));
//命令入到队列中
List
2.取消关注
public boolean unfollowSomeone(int entity_type,int entity_id,int user_id)
{
String fansKey= new RedisKeyUtil().getWhoFollowedMeList(entity_id,entity_type);
String followingKey=new RedisKeyUtil().getWhoIFollowListKey(user_id,entity_type);
Jedis j=jedis.getJedis();
Date date=new Date();
Transaction tx=jedis.multi(j);
tx.zrem(followingKey,String.valueOf(entity_id));
tx.zrem(fansKey,String.valueOf(user_id));
List
3.判断是否为关注关系
public boolean isFollowRelationship(int entity_type,int entity_id,int user_id)
{
String fanskey=new RedisKeyUtil().getWhoFollowedMeList(entity_id,entity_type);
Jedis j=jedis.getJedis();
try
{
return j.zscore(fanskey,String.valueOf(user_id))!=null;
//如果上面的结果为null说明fanskey中找不到,就返回false
}
finally {
if(j!=null)
j.close();
}
}
4.获取所有的关注人和粉丝列表
public List getFollowers(int entity_type,int entity_id,int offset,int count)
{
String fansKey=new RedisKeyUtil().getWhoFollowedMeList(entity_id,entity_type);
Jedis j=jedis.getJedis();
try
{
return getIDfromSets(j.zrange(fansKey,offset,count));
}
finally {
if(j!=null)
j.close();
}
}
public List getFollowees(int entity_type,int user_id,int offset,int count)
{
String FolloweeKeys=new RedisKeyUtil().getWhoIFollowListKey(user_id,entity_type);
Jedis j=jedis.getJedis();
try
{
return getIDfromSets(j.zrevrange(FolloweeKeys,offset,count));
}
finally {
if(j!=null)
j.close();
}
}
//将zrange返回的Set转换成List的辅助函数
private List getIDfromSets(Set idset)
{
List ids=new ArrayList<>();
for(String tmp:idset)
ids.add(Integer.parseInt(tmp));
return ids;
}
5.获得关注数量和粉丝数量
public long getFansCount(int entity_type,int entity_id)
{
String fanskey=new RedisKeyUtil().getWhoFollowedMeList(entity_id,entity_type);
Jedis j=jedis.getJedis();
try
{
return j.zcard(fanskey);
}
finally {
if(j!=null)
j.close();
}
}
public long getFolloweeCount(int entity_type,int user_id)
{
String followeeCount=new RedisKeyUtil().getWhoIFollowListKey(user_id,entity_type);
Jedis j=jedis.getJedis();
try
{
return j.zcard(followeeCount);
}
finally {
if(j!=null)
j.close();
}
}
Controller中要添加和修改的地方
1.首页上的问题关注数量
ViewObject中添加关注者数量
vo.set("followercount",followService.getFansCount(EntityType.ENTITY_QUESTION,a.getId()));
2.问题详情页中的关注和取消关注按键
@RequestMapping(value = {"/unfollowquestion"}, method = {RequestMethod.POST})
@ResponseBody
public String unFollowQuestion(@RequestParam("questionId") int questionid) {
User user = hostHolder.getuser();
if (user == null)
return WendaUtil.generatejson(999);
if(questionService.getQuestionByID(questionid)==null)
return WendaUtil.generatejson(1,"问题不存在");
boolean ret = followService.unfollowSomeone(EntityType.ENTITY_QUESTION, questionid, user.getId());
Map map=new HashMap();
map.put("headUrl",user.getHeadUrl());
map.put("id",user.getId());
map.put("name",user.getName());
map.put("count",followService.getFansCount(EntityType.ENTITY_QUESTION,questionid));
return WendaUtil.getJSONString(0,map);
}
@RequestMapping(value = {"/followquestion"}, method = {RequestMethod.POST})
@ResponseBody
public String FollowQuestion(@RequestParam("questionId") int questionid) {
User user = hostHolder.getuser();
if (user == null)
return WendaUtil.generatejson(999);
if(questionService.getQuestionByID(questionid)==null)
return WendaUtil.generatejson(1,"问题不存在");
boolean ret = followService.followSomeone(EntityType.ENTITY_QUESTION, questionid, user.getId());
Map map=new HashMap();
map.put("headUrl",user.getHeadUrl());
map.put("id",user.getId());
map.put("name",user.getName());
map.put("count",followService.getFansCount(EntityType.ENTITY_QUESTION,questionid));
eventProducer.fireEvent(new EventModel().setEventype(EventType.Follow).setUserid(user.getId()).setEntityownerid(questionService.getQuestionByID(questionid).getUserid()).setEntitytype(EntityType.ENTITY_QUESTION).setEntityid(questionid));
//异步队列添加事件(发送站内信),关注时发取关时不发
return WendaUtil.getJSONString(0,map);
}
3.粉丝和关注人列表
@RequestMapping(value = "/followee/{user}", method = {RequestMethod.GET})
public String getFolloweeList(@PathVariable("user")int userid, Model model) {
model.addAttribute("name",userService.getuserbyid(userid).getName());
model.addAttribute("FolloweeCount",followService.getFolloweeCount(EntityType.ENTITY_USER,userid));
model.addAttribute("type",0);
User user = hostHolder.getuser();
if (user == null)
return "redirect:/reglogin";
List followeeList = followService.getFollowees(EntityType.ENTITY_USER, userid, 0, 10);
List vos = new ArrayList();
for (Integer a : followeeList) {
ViewObject vo = new ViewObject();
User userr = userService.getuserbyid(a);
vo.set("user", userr);
vo.set("fans",followService.getFansCount(EntityType.ENTITY_USER,userr.getId()));
System.out.println(userr.getName());
vo.set("followee",followService.getFolloweeCount(EntityType.ENTITY_USER,userr.getId()));
vo.set("followed",followService.isFollowRelationship(EntityType.ENTITY_USER,userr.getId(),user.getId()));
vo.set("ask",questionService.getQuestionCountByUserID(userr.getId()));
vo.set("comment",commentService.getCommentCountByUserID(userr.getId()));
vos.add(vo);
}
model.addAttribute("vos", vos);
return "follow";
}
@RequestMapping(value = "/follower/{user}", method = {RequestMethod.GET})
public String getFollowerList(@PathVariable("user")int userid, Model model) {
model.addAttribute("name",userService.getuserbyid(userid).getName());
model.addAttribute("FolloweeCount",followService.getFansCount(EntityType.ENTITY_USER,userid));
model.addAttribute("type",1);
User user = hostHolder.getuser();
if (user == null)
return "redirect:/reglogin";
List followerList = followService.getFollowers(EntityType.ENTITY_USER, userid, 0, 10);
List vos = new ArrayList();
for (Integer a : followerList) {
ViewObject vo = new ViewObject();
User userr = userService.getuserbyid(a);
vo.set("user", userr);
vo.set("fans",followService.getFansCount(EntityType.ENTITY_USER,userr.getId()));
vo.set("followee",followService.getFolloweeCount(EntityType.ENTITY_USER,userr.getId()));
vo.set("followed",followService.isFollowRelationship(EntityType.ENTITY_USER,userr.getId(),user.getId()));
vo.set("ask",questionService.getQuestionCountByUserID(userr.getId()));
vo.set("comment",commentService.getCommentCountByUserID(userr.getId()));
vos.add(vo);
}
model.addAttribute("vos", vos);
return "follow";
}
4.关注用户和取消关注用户按键
@RequestMapping(value = {"/followuser"}, method = {RequestMethod.POST})
@ResponseBody
public String followUser(@RequestParam("userId") int user_id) {
User user = hostHolder.getuser();
if (user == null)
return WendaUtil.generatejson(999);
boolean ret = followService.followSomeone(EntityType.ENTITY_USER, user_id, user.getId());
eventProducer.fireEvent(new EventModel().setEventype(EventType.Follow).setUserid(user.getId()).setEntityownerid(user_id).setEntitytype(EntityType.ENTITY_USER).setEntityid(user_id));
return WendaUtil.generatejson(ret ? 0 : 1, String.valueOf(followService.getFolloweeCount(EntityType.ENTITY_USER, user.getId())));
}
@RequestMapping(value = {"/unfollowuser"}, method = {RequestMethod.POST})
@ResponseBody
public String unFollowSomeone(@RequestParam("userId") int user_id) {
User user = hostHolder.getuser();
if (user == null)
return WendaUtil.generatejson(999);
boolean ret = followService.unfollowSomeone(EntityType.ENTITY_USER, user_id, user.getId());
return WendaUtil.generatejson(ret ? 0 : 1, String.valueOf(followService.getFolloweeCount(EntityType.ENTITY_USER, user.getId())));
}
5.关注事件添加Handler,来进行站内信的发送
@Component
public class FollowHandler implements EventHandler {
@Autowired
UserService userService;
@Autowired
MessageService messageService;
//给被点赞用户发送站内信
@Override
public void doHandle(EventModel event) {
Message msg=new Message();
msg.setToid(event.getEntityownerid());
msg.setCreateddate(new Date());
msg.setFromid(888);
User user=userService.getuserbyid(event.getUserid());
String content=getMessageContent(event);
if(content==null)
return;
msg.setContent(content);
msg.setHasread(0);
msg.setConversationid(event.getEntityownerid(),888);
messageService.insertMessage(msg);
}
public String getMessageContent(EventModel event)
{
User user=userService.getuserbyid(event.getUserid());
int type=event.getEntitytype();
int entity_id=event.getEntityid();
StringBuilder str=new StringBuilder();
str.append("用户"+user.getName()+"关注了您");
if(event.getEventype()==EventType.Follow)
{
if(type== EntityType.ENTITY_USER)
return str.toString();
else
{
str.append("的问题 "+"http://127.0.0.1:8080/question/"+String.valueOf(entity_id));
return str.toString();
}
}
return null;
}
//将该Handler和Follow,UnFollow事件绑定
@Override
public List getSupportEventType() {
return Arrays.asList(EventType.Follow,EventType.UnFollow);
}
}