Skip to content

Commit

Permalink
修复开启redis后,新注册用户点击用户名出错的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
atjiu committed Apr 19, 2021
1 parent 65d0656 commit 80328ee
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@
import co.yiiu.pybbs.service.ISystemConfigService;
import co.yiiu.pybbs.service.ITopicService;
import co.yiiu.pybbs.util.JsonUtil;
import com.alibaba.fastjson.TypeReference;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import java.util.List;
import javax.annotation.Resource;

/**
* Created by tomoya.
Expand All @@ -40,13 +36,11 @@ public class RedisCachePlugin {

@Around("co.yiiu.pybbs.hook.TopicServiceHook.selectById()")
public Object topicSelectById(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
String topicJson = redisService.getString(String.format(RedisKeys.REDIS_TOPIC_KEY, proceedingJoinPoint.getArgs()
[0]));
String topicJson = redisService.getString(String.format(RedisKeys.REDIS_TOPIC_KEY, proceedingJoinPoint.getArgs()[0]));
if (topicJson == null) {
Object topic = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
// 缓存在redis里
redisService.setString(String.format(RedisKeys.REDIS_TOPIC_KEY, proceedingJoinPoint.getArgs()[0]), JsonUtil
.objectToJson(topic));
redisService.setString(String.format(RedisKeys.REDIS_TOPIC_KEY, proceedingJoinPoint.getArgs()[0]), JsonUtil.objectToJson(topic));
return topic;
} else {
return JsonUtil.jsonToObject(topicJson, Topic.class);
Expand All @@ -63,15 +57,22 @@ public Object topicUpdateViewCount(ProceedingJoinPoint proceedingJoinPoint) thro
topic.setView(topic.getView() + 1);
topicService.update(topic, null);
redisService.setString(String.format(RedisKeys.REDIS_TOPIC_VIEW_IP_ID_KEY, ip, topic.getId()), String.valueOf
(topic.getId()), Integer.parseInt(systemConfigService.selectAllConfig().get
("topic_view_increase_interval").toString()));
(topic.getId()), Integer.parseInt(systemConfigService.selectAllConfig().get("topic_view_increase_interval").toString()));
}
return topic;
} else {
return proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
}
}

@Around("co.yiiu.pybbs.hook.TopicServiceHook.vote()")
public Object voteTopic(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
// 点赞后删除redis内缓存的topic数据
Topic topic = (Topic) proceedingJoinPoint.getArgs()[0];
redisService.delString(String.format(RedisKeys.REDIS_TOPIC_KEY, topic.getId()));
return proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
}

@After("co.yiiu.pybbs.hook.TopicServiceHook.update()")
public void topicUpdate(JoinPoint joinPoint) {
Topic topic = (Topic) joinPoint.getArgs()[0];
Expand Down Expand Up @@ -115,6 +116,13 @@ public void commentDelete(JoinPoint joinPoint) {
redisService.delString(String.format(RedisKeys.REDIS_COMMENTS_KEY, comment.getTopicId()));
}

@Around("co.yiiu.pybbs.hook.CommentServiceHook.vote()")
public Object voteComment(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Comment comment = (Comment) proceedingJoinPoint.getArgs()[0];
redisService.delString(String.format(RedisKeys.REDIS_COMMENTS_KEY, comment.getTopicId()));
return proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
}

// ---------- comment cache end ----------

// ---------- user cache start ----------
Expand All @@ -128,37 +136,9 @@ public Object userSelectByUsername(ProceedingJoinPoint proceedingJoinPoint) thro
return JsonUtil.jsonToObject(userJson, User.class);
} else {
Object returnValue = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
redisService.setString(String.format(RedisKeys.REDIS_USER_USERNAME_KEY, username), JsonUtil.objectToJson
(returnValue));
return returnValue;
}
}

@Around("co.yiiu.pybbs.hook.UserServiceHook.selectByEmail()")
public Object userSelectByEmail(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
String email = (String) proceedingJoinPoint.getArgs()[0];
String userJson = redisService.getString(String.format(RedisKeys.REDIS_USER_EMAIL_KEY, email));
if (userJson != null) {
// 带泛型转换, 这里如果不带泛型转换,会报错
return JsonUtil.jsonToObject(userJson, User.class);
} else {
Object returnValue = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
redisService.setString(String.format(RedisKeys.REDIS_USER_EMAIL_KEY, email), JsonUtil.objectToJson(returnValue));
return returnValue;
}
}

@Around("co.yiiu.pybbs.hook.UserServiceHook.selectByMobile()")
public Object userSelectByMobile(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
String mobile = (String) proceedingJoinPoint.getArgs()[0];
String userJson = redisService.getString(String.format(RedisKeys.REDIS_USER_MOBILE_KEY, mobile));
if (userJson != null) {
// 带泛型转换, 这里如果不带泛型转换,会报错
return JsonUtil.jsonToObject(userJson, User.class);
} else {
Object returnValue = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
redisService.setString(String.format(RedisKeys.REDIS_USER_MOBILE_KEY, mobile), JsonUtil.objectToJson
(returnValue));
if (returnValue != null) {
redisService.setString(String.format(RedisKeys.REDIS_USER_USERNAME_KEY, username), JsonUtil.objectToJson(returnValue));
}
return returnValue;
}
}
Expand All @@ -172,7 +152,9 @@ public Object userSelectByToken(ProceedingJoinPoint proceedingJoinPoint) throws
return JsonUtil.jsonToObject(userJson, User.class);
} else {
Object returnValue = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
redisService.setString(String.format(RedisKeys.REDIS_USER_TOKEN_KEY, token), JsonUtil.objectToJson(returnValue));
if (returnValue != null) {
redisService.setString(String.format(RedisKeys.REDIS_USER_TOKEN_KEY, token), JsonUtil.objectToJson(returnValue));
}
return returnValue;
}
}
Expand All @@ -186,7 +168,9 @@ public Object userSelectById(ProceedingJoinPoint proceedingJoinPoint) throws Thr
return JsonUtil.jsonToObject(userJson, User.class);
} else {
Object returnValue = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
redisService.setString(String.format(RedisKeys.REDIS_USER_ID_KEY, id), JsonUtil.objectToJson(returnValue));
if (returnValue != null) {
redisService.setString(String.format(RedisKeys.REDIS_USER_ID_KEY, id), JsonUtil.objectToJson(returnValue));
}
return returnValue;
}
}
Expand All @@ -197,21 +181,17 @@ public void userDelRedisUser(JoinPoint joinPoint) {
redisService.delString(String.format(RedisKeys.REDIS_USER_ID_KEY, user.getId()));
redisService.delString(String.format(RedisKeys.REDIS_USER_USERNAME_KEY, user.getUsername()));
redisService.delString(String.format(RedisKeys.REDIS_USER_TOKEN_KEY, user.getToken()));
redisService.delString(String.format(RedisKeys.REDIS_USER_MOBILE_KEY, user.getMobile()));
redisService.delString(String.format(RedisKeys.REDIS_USER_EMAIL_KEY, user.getEmail()));
}

// ---------- user cache end ----------

class RedisKeys {
static class RedisKeys {
public static final String REDIS_TOPIC_KEY = "pybbs_topic_%s"; // 后面还要拼上话题的id
public static final String REDIS_TOPIC_VIEW_IP_ID_KEY = "pybbs_topic_view_ip_%s_topic_%s"; // 需要格式化字符串填充上ip地址跟话题id
public static final String REDIS_COMMENTS_KEY = "pybbs_comments_%s"; // 后面还要拼上话题的id

public static final String REDIS_USER_ID_KEY = "pybbs_user_id_%s"; // 后面还要拼上用户的id
public static final String REDIS_USER_USERNAME_KEY = "pybbs_user_username_%s"; // 后面还要拼上用户名
public static final String REDIS_USER_TOKEN_KEY = "pybbs_user_token_%s"; // 后面还要拼上用户的token
public static final String REDIS_USER_MOBILE_KEY = "pybbs_user_mobile_%s"; // 后面还要拼上用户的mobile
public static final String REDIS_USER_EMAIL_KEY = "pybbs_user_email_%s"; // 后面还要拼上用户的email
public static final String REDIS_USER_ID_KEY = "pybbs_user_id_%s"; // 后面还要拼上用户的id
}
}
12 changes: 9 additions & 3 deletions src/main/java/co/yiiu/pybbs/plugin/RedisCachePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ public Object userSelectByUsername(ProceedingJoinPoint proceedingJoinPoint) thro
return JsonUtil.jsonToObject(userJson, User.class);
} else {
Object returnValue = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
redisService.setString(String.format(RedisKeys.REDIS_USER_USERNAME_KEY, username), JsonUtil.objectToJson(returnValue));
if (returnValue != null) {
redisService.setString(String.format(RedisKeys.REDIS_USER_USERNAME_KEY, username), JsonUtil.objectToJson(returnValue));
}
return returnValue;
}
}
Expand All @@ -150,7 +152,9 @@ public Object userSelectByToken(ProceedingJoinPoint proceedingJoinPoint) throws
return JsonUtil.jsonToObject(userJson, User.class);
} else {
Object returnValue = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
redisService.setString(String.format(RedisKeys.REDIS_USER_TOKEN_KEY, token), JsonUtil.objectToJson(returnValue));
if (returnValue != null) {
redisService.setString(String.format(RedisKeys.REDIS_USER_TOKEN_KEY, token), JsonUtil.objectToJson(returnValue));
}
return returnValue;
}
}
Expand All @@ -164,7 +168,9 @@ public Object userSelectById(ProceedingJoinPoint proceedingJoinPoint) throws Thr
return JsonUtil.jsonToObject(userJson, User.class);
} else {
Object returnValue = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
redisService.setString(String.format(RedisKeys.REDIS_USER_ID_KEY, id), JsonUtil.objectToJson(returnValue));
if (returnValue != null) {
redisService.setString(String.format(RedisKeys.REDIS_USER_ID_KEY, id), JsonUtil.objectToJson(returnValue));
}
return returnValue;
}
}
Expand Down

0 comments on commit 80328ee

Please sign in to comment.