Commit ccff03f0 by guanchen

添加Redis缓存支持到relation接口

parent 5c1364fe
...@@ -6,11 +6,13 @@ import org.springframework.boot.SpringApplication; ...@@ -6,11 +6,13 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
/** /**
* @author houseme * @author houseme
*/ */
@SpringBootApplication @SpringBootApplication
@EnableCaching
public class PartnerApplication extends SpringBootServletInitializer { public class PartnerApplication extends SpringBootServletInitializer {
private static Logger logger = LoggerFactory.getLogger(PartnerApplication.class); private static Logger logger = LoggerFactory.getLogger(PartnerApplication.class);
......
package com.lanren.huhu.partner.config; package com.lanren.huhu.partner.config;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer; import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import com.lanren.huhu.partner.controller.UserController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -18,8 +22,15 @@ import org.springframework.data.redis.serializer.StringRedisSerializer; ...@@ -18,8 +22,15 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
* File * File
*/ */
@Configuration @Configuration
@EnableCaching
public class RedisConfiguration { public class RedisConfiguration {
private static Logger logger = LoggerFactory.getLogger(RedisConfiguration.class);
static {
logger.info("加载redis配置");
ParserConfig.getGlobalInstance().addAccept("com.lanren.huhu.partner.model.");
ParserConfig.getGlobalInstance().addAccept("com.lanren.huhu.partner.result.");
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
}
@Bean @Bean
public RedisCacheManager getRedisCacheManager(RedisConnectionFactory connectionFactory) { public RedisCacheManager getRedisCacheManager(RedisConnectionFactory connectionFactory) {
......
...@@ -26,9 +26,22 @@ public class UserController { ...@@ -26,9 +26,22 @@ public class UserController {
@RequestMapping(value = "/relation", method = RequestMethod.POST) @RequestMapping(value = "/relation", method = RequestMethod.POST)
public Result<User> getRelation(@RequestBody @Valid User user, @RequestHeader HttpHeaders headers) { public Result<User> getRelation(@RequestBody @Valid User user, @RequestHeader HttpHeaders headers) {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info("getPartnerLevel userId:{ },header:{},time:{}", user.getUserId(), headers.keySet().toArray(), LocalDateTime.now()); logger.info("getRelation userId:{ },header:{},time:{}", user.getUserId(), headers.keySet().toArray(), LocalDateTime.now());
} }
return userManager.getRelation(user); User resutrnUser = userManager.getRelationByUserId(user.getUserId());
Result<User> result = new Result<User>();
result.setData(resutrnUser);
return result;
} }
@RequestMapping(value = "/relation/delete", method = RequestMethod.POST)
public Result<String> deletrRelation(@RequestBody @Valid User user, @RequestHeader HttpHeaders headers) {
if (logger.isInfoEnabled()) {
logger.info("deletrRelation userId:{ },header:{},time:{}", user.getUserId(), headers.keySet().toArray(), LocalDateTime.now());
}
userManager.deleteRalationByUserId(user.getUserId());
Result<String> result = new Result<String>();
result.setData("删除成功");
return result;
}
} }
...@@ -2,7 +2,6 @@ package com.lanren.huhu.partner.manager; ...@@ -2,7 +2,6 @@ package com.lanren.huhu.partner.manager;
import com.lanren.huhu.partner.domain.UserAgent; import com.lanren.huhu.partner.domain.UserAgent;
import com.lanren.huhu.partner.model.AgentRsp; import com.lanren.huhu.partner.model.AgentRsp;
import com.lanren.huhu.partner.model.PartnerRsp;
import com.lanren.huhu.partner.result.Result; import com.lanren.huhu.partner.result.Result;
import com.lanren.huhu.partner.service.UserAgentService; import com.lanren.huhu.partner.service.UserAgentService;
import org.slf4j.Logger; import org.slf4j.Logger;
......
...@@ -11,6 +11,9 @@ import com.lanren.huhu.partner.service.UserAgentService; ...@@ -11,6 +11,9 @@ import com.lanren.huhu.partner.service.UserAgentService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -19,6 +22,7 @@ import java.util.ArrayList; ...@@ -19,6 +22,7 @@ import java.util.ArrayList;
* @author houseme * @author houseme
*/ */
@Component @Component
@CacheConfig(cacheNames = "user_cache")
public class UserManager { public class UserManager {
private static Logger logger = LoggerFactory.getLogger(UserManager.class); private static Logger logger = LoggerFactory.getLogger(UserManager.class);
...@@ -30,18 +34,31 @@ public class UserManager { ...@@ -30,18 +34,31 @@ public class UserManager {
/** /**
* @description: 根据用户id, 查找一个用户的全部合伙人上级 和 代理商上级 * @description: 根据用户id, 查找一个用户的全部合伙人上级 和 代理商上级
* @param user * 缓存用户邀请关系和代理商关系数据
* 缓存不能带有构造方法的Bean 所以只能返回User的Bean
* 包装Result在Cacheable注解的方法之外进行操作
* @param userId
* @return Result<User> * @return Result<User>
* @throws * @throws
* @author chen * @author chen
* @date 2019-06-26 20:01 * @date 2019-06-26 20:01
*/ */
public Result<User> getRelation(User user) { @Cacheable
public User getRelationByUserId(Integer userId) {
logger.info("new a user object ");
User user = new User();
user.setUserId(userId);
setPartnerList(user); setPartnerList(user);
setAgentList(user); setAgentList(user);
Result<User> result = new Result<User>(); return user;
result.setData(user); }
return result;
/**
* 清理用户缓存
*/
@CacheEvict(key = "#userId")
public void deleteRalationByUserId(Integer userId) {
logger.info("delete user relation cache, userId: {}", userId);
} }
/** /**
......
package com.lanren.huhu.partner.model; package com.lanren.huhu.partner.model;
import com.lanren.huhu.partner.controller.UserController;
import com.lanren.huhu.partner.model.base.BaseModel; import com.lanren.huhu.partner.model.base.BaseModel;
import lombok.Data; import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.constraints.Min; import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
...@@ -17,6 +21,8 @@ import java.util.ArrayList; ...@@ -17,6 +21,8 @@ import java.util.ArrayList;
*/ */
@Data @Data
public class User extends BaseModel { public class User extends BaseModel {
private static Logger logger = LoggerFactory.getLogger(User.class);
@NotNull(message = "userId 不能为空") @NotNull(message = "userId 不能为空")
@Min(value = 1, message = "userId 需要大于0") @Min(value = 1, message = "userId 需要大于0")
private int userId; private int userId;
...@@ -24,4 +30,28 @@ public class User extends BaseModel { ...@@ -24,4 +30,28 @@ public class User extends BaseModel {
private ArrayList<ParentPartner> partnerList; private ArrayList<ParentPartner> partnerList;
private ArrayList<ParentAgent> agentList; private ArrayList<ParentAgent> agentList;
/**
* 坑哦!!!!
* 这里不能添加任何的构造方法
* 不然autotype哪里会报错
* com.alibaba.fastjson.parser.ParserConfig.checkAutoType(ParserConfig.java:1180)
* if (beanInfo.creatorConstructor != null && autoTypeSupport) {
* throw new JSONException("autoType is not support. " + typeName);
* }
* 问题出在第一个判定 如果自己写了构造方法
*/
// public User(@NotNull(message = "userId 不能为空") @Min(value = 1, message = "userId 需要大于0") int userId) {
// this.userId = userId;
// logger.info("do set name");
// this.name = "testName";
// }
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", partnerList=" + partnerList +
", agentList=" + agentList +
'}';
}
} }
package com.lanren.huhu.partner.result; package com.lanren.huhu.partner.result;
import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.annotation.JSONField;
import com.lanren.huhu.partner.model.base.BaseModel;
import com.lanren.huhu.partner.util.MessageUtil; import com.lanren.huhu.partner.util.MessageUtil;
/** /**
...@@ -10,7 +11,7 @@ import com.lanren.huhu.partner.util.MessageUtil; ...@@ -10,7 +11,7 @@ import com.lanren.huhu.partner.util.MessageUtil;
* @version 1.0 * @version 1.0
* @date 2016年7月18日 上午10:27:00 * @date 2016年7月18日 上午10:27:00
*/ */
public class Result<T> { public class Result<T> extends BaseModel {
@JSONField(name="errorCode") @JSONField(name="errorCode")
private int code; private int code;
private String message; private String message;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment