| @@ -125,4 +125,21 @@ public class RedisConfig extends CachingConfigurerSupport { | |||||
| return template; | return template; | ||||
| } | } | ||||
| @Bean("cuserTokenRedisTemplate") | |||||
| public RedisTemplate<String, WxCUser> getCUserTokenRedisTemplate(RedisConnectionFactory connectionFactory) { | |||||
| RedisTemplate<String, WxCUser> template = new RedisTemplate<String, WxCUser>(); | |||||
| Jackson2JsonRedisSerializer<WxCUser> j = new Jackson2JsonRedisSerializer<WxCUser>(WxCUser.class); | |||||
| // value值的序列化 | |||||
| template.setValueSerializer(j); | |||||
| template.setHashKeySerializer(j); | |||||
| // key的序列化 | |||||
| template.setKeySerializer(new StringRedisSerializer()); | |||||
| template.setHashKeySerializer(new StringRedisSerializer()); | |||||
| template.setConnectionFactory(connectionFactory); | |||||
| return template; | |||||
| } | |||||
| } | } | ||||
| @@ -87,4 +87,21 @@ public class RedisConfig extends CachingConfigurerSupport { | |||||
| return template; | return template; | ||||
| } | } | ||||
| @Bean("cuserTokenRedisTemplate") | |||||
| public RedisTemplate<String, WxCUser> getCUserTokenRedisTemplate(RedisConnectionFactory connectionFactory) { | |||||
| RedisTemplate<String, WxCUser> template = new RedisTemplate<String, WxCUser>(); | |||||
| Jackson2JsonRedisSerializer<WxCUser> j = new Jackson2JsonRedisSerializer<WxCUser>(WxCUser.class); | |||||
| // value值的序列化 | |||||
| template.setValueSerializer(j); | |||||
| template.setHashKeySerializer(j); | |||||
| // key的序列化 | |||||
| template.setKeySerializer(new StringRedisSerializer()); | |||||
| template.setHashKeySerializer(new StringRedisSerializer()); | |||||
| template.setConnectionFactory(connectionFactory); | |||||
| return template; | |||||
| } | |||||
| } | } | ||||
| @@ -87,4 +87,21 @@ public class RedisConfig extends CachingConfigurerSupport { | |||||
| return template; | return template; | ||||
| } | } | ||||
| @Bean("cuserTokenRedisTemplate") | |||||
| public RedisTemplate<String, WxCUser> getCUserTokenRedisTemplate(RedisConnectionFactory connectionFactory) { | |||||
| RedisTemplate<String, WxCUser> template = new RedisTemplate<String, WxCUser>(); | |||||
| Jackson2JsonRedisSerializer<WxCUser> j = new Jackson2JsonRedisSerializer<WxCUser>(WxCUser.class); | |||||
| // value值的序列化 | |||||
| template.setValueSerializer(j); | |||||
| template.setHashKeySerializer(j); | |||||
| // key的序列化 | |||||
| template.setKeySerializer(new StringRedisSerializer()); | |||||
| template.setHashKeySerializer(new StringRedisSerializer()); | |||||
| template.setConnectionFactory(connectionFactory); | |||||
| return template; | |||||
| } | |||||
| } | } | ||||
| @@ -110,4 +110,21 @@ public class RedisConfig extends CachingConfigurerSupport { | |||||
| return template; | return template; | ||||
| } | } | ||||
| @Bean("cuserTokenRedisTemplate") | |||||
| public RedisTemplate<String, WxCUser> getCUserTokenRedisTemplate(RedisConnectionFactory connectionFactory) { | |||||
| RedisTemplate<String, WxCUser> template = new RedisTemplate<String, WxCUser>(); | |||||
| Jackson2JsonRedisSerializer<WxCUser> j = new Jackson2JsonRedisSerializer<WxCUser>(WxCUser.class); | |||||
| // value值的序列化 | |||||
| template.setValueSerializer(j); | |||||
| template.setHashKeySerializer(j); | |||||
| // key的序列化 | |||||
| template.setKeySerializer(new StringRedisSerializer()); | |||||
| template.setHashKeySerializer(new StringRedisSerializer()); | |||||
| template.setConnectionFactory(connectionFactory); | |||||
| return template; | |||||
| } | |||||
| } | } | ||||
| @@ -1,5 +1,6 @@ | |||||
| package com.iformall.domain.po; | package com.iformall.domain.po; | ||||
| import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |||||
| import com.iformall.utils.BaseConstant; | import com.iformall.utils.BaseConstant; | ||||
| import javax.persistence.*; | import javax.persistence.*; | ||||
| @@ -11,6 +12,7 @@ import javax.persistence.Id; | |||||
| import java.io.Serializable; | import java.io.Serializable; | ||||
| @Table(name = "wx_c_user") | @Table(name = "wx_c_user") | ||||
| @JsonIgnoreProperties(ignoreUnknown = true) | |||||
| public class WxCUser implements Serializable { | public class WxCUser implements Serializable { | ||||
| private static final long serialVersionUID = 1L; | private static final long serialVersionUID = 1L; | ||||
| @@ -43,6 +43,11 @@ public class WxCUserServiceImpl implements WxCUserService { | |||||
| WxScoreHistoryMapper wxScoreHistoryMapper; | WxScoreHistoryMapper wxScoreHistoryMapper; | ||||
| @Autowired | |||||
| @Qualifier("cuserTokenRedisTemplate") | |||||
| RedisTemplate<String, WxCUser> cUserTokenRedisTemplate; | |||||
| @Override | @Override | ||||
| public PageInfo<WxCUser> listAsPage(WxCUser record, Integer pageIndex, Integer pageSize) { | public PageInfo<WxCUser> listAsPage(WxCUser record, Integer pageIndex, Integer pageSize) { | ||||
| return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxCUserMapper.findList(record)); | return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxCUserMapper.findList(record)); | ||||
| @@ -60,7 +65,22 @@ public class WxCUserServiceImpl implements WxCUserService { | |||||
| @Override | @Override | ||||
| public WxCUser getByToken(String token) { | public WxCUser getByToken(String token) { | ||||
| return wxCUserMapper.findByToken(token); | |||||
| logger.info("获取用户信息start ..."); | |||||
| String key = "token_" + token; | |||||
| ValueOperations<String, WxCUser> operations = cUserTokenRedisTemplate.opsForValue(); | |||||
| // 缓存 | |||||
| boolean hasKey = cUserTokenRedisTemplate.hasKey(key); | |||||
| if(hasKey) { | |||||
| // 从缓存获取用户信息 | |||||
| WxCUser user = operations.get(key); | |||||
| return user; | |||||
| } | |||||
| WxCUser user = wxCUserMapper.findByToken(token); | |||||
| // 插入缓存 | |||||
| operations.set(key, user, 3600, TimeUnit.SECONDS); | |||||
| return user; | |||||
| } | } | ||||
| @Override | @Override | ||||
| @@ -80,6 +100,14 @@ public class WxCUserServiceImpl implements WxCUserService { | |||||
| user.setUpdateDate(curr); | user.setUpdateDate(curr); | ||||
| ret = wxCUserMapper.updateByPrimaryKeySelective(user); | ret = wxCUserMapper.updateByPrimaryKeySelective(user); | ||||
| } | } | ||||
| String key = "token_" + user.getToken(); | |||||
| // 缓存已存在,删除 | |||||
| boolean hasKey = cUserTokenRedisTemplate.hasKey(key); | |||||
| if(hasKey) { | |||||
| cUserTokenRedisTemplate.delete(key); | |||||
| logger.info("更新用户,从缓存中删除用户 token >> " + user.getToken()); | |||||
| } | |||||
| return ret; | return ret; | ||||
| } | } | ||||
| @@ -108,4 +108,22 @@ public class RedisConfig extends CachingConfigurerSupport { | |||||
| template.setConnectionFactory(connectionFactory); | template.setConnectionFactory(connectionFactory); | ||||
| return template; | return template; | ||||
| } | } | ||||
| @Bean("cuserTokenRedisTemplate") | |||||
| public RedisTemplate<String, WxCUser> getCUserTokenRedisTemplate(RedisConnectionFactory connectionFactory) { | |||||
| RedisTemplate<String, WxCUser> template = new RedisTemplate<String, WxCUser>(); | |||||
| Jackson2JsonRedisSerializer<WxCUser> j = new Jackson2JsonRedisSerializer<WxCUser>(WxCUser.class); | |||||
| // value值的序列化 | |||||
| template.setValueSerializer(j); | |||||
| template.setHashKeySerializer(j); | |||||
| // key的序列化 | |||||
| template.setKeySerializer(new StringRedisSerializer()); | |||||
| template.setHashKeySerializer(new StringRedisSerializer()); | |||||
| template.setConnectionFactory(connectionFactory); | |||||
| return template; | |||||
| } | |||||
| } | } | ||||