Browse Source

fix cache

release_toaliyun_real
xiaohanzi 5 years ago
parent
commit
7e3cda8462
9 changed files with 228 additions and 167 deletions
  1. +58
    -79
      mallinkCApi/src/main/java/com/iformall/config/RedisConfig.java
  2. +11
    -19
      mallinkCApi/src/main/java/com/iformall/controller/BaseController.java
  3. +0
    -2
      mallinkService/src/main/java/com/iformall/domain/po/WxLevelConfig.java
  4. +5
    -0
      mallinkService/src/main/java/com/iformall/domain/po/base/BaseCUserEntity.java
  5. +11
    -26
      mallinkService/src/main/java/com/iformall/service/impl/CUserTokenServiceImpl.java
  6. +24
    -30
      mallinkService/src/main/java/com/iformall/service/impl/WxAppinfoServiceImpl.java
  7. +12
    -11
      mallinkService/src/main/java/com/iformall/service/impl/WxLevelConfigServiceImpl.java
  8. +106
    -0
      mallinkService/src/main/java/com/iformall/utils/RedisCacheUtils.java
  9. +1
    -0
      mallinkService/src/main/java/com/iformall/utils/RedisUtils.java

+ 58
- 79
mallinkCApi/src/main/java/com/iformall/config/RedisConfig.java View File

@@ -65,47 +65,47 @@ public class RedisConfig extends CachingConfigurerSupport {
@Bean("pushLimitRedisTemplate")
public RedisTemplate<String, PushLimit> getPushLimitRedisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, PushLimit> template = new RedisTemplate<String, PushLimit>();
template.setConnectionFactory(connectionFactory);
Jackson2JsonRedisSerializer<PushLimit> j = new Jackson2JsonRedisSerializer<PushLimit>(PushLimit.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
j.setObjectMapper(om);
// value值的序列化
template.setValueSerializer(j);
template.setHashKeySerializer(j);
template.setHashValueSerializer(j);

// key的序列化
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(connectionFactory);
template.afterPropertiesSet();
return template;
}

@Bean("scoreRuleRedisTemplate")
public RedisTemplate<String, WxScoreRules> getScoreRuleRedisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, WxScoreRules> template = new RedisTemplate<String, WxScoreRules>();
template.setConnectionFactory(connectionFactory);
Jackson2JsonRedisSerializer<WxScoreRules> j = new Jackson2JsonRedisSerializer<WxScoreRules>(WxScoreRules.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
j.setObjectMapper(om);
// value值的序列化
template.setValueSerializer(j);
template.setHashKeySerializer(j);
template.setHashValueSerializer(j);

// key的序列化
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(connectionFactory);
template.afterPropertiesSet();
return template;
}

@Bean("cuserTokenRedisTemplate")
public RedisTemplate<String, WxCUser> getCUserTokenRedisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, WxCUser> template = new RedisTemplate<String, WxCUser>();
template.setConnectionFactory(connectionFactory);
Jackson2JsonRedisSerializer<WxCUser> j = new Jackson2JsonRedisSerializer<WxCUser>(WxCUser.class);
ObjectMapper om = new ObjectMapper();
@@ -114,13 +114,13 @@ public class RedisConfig extends CachingConfigurerSupport {
// value值的序列化
template.setValueSerializer(j);
template.setHashKeySerializer(j);
template.setHashValueSerializer(j);

// key的序列化
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(connectionFactory);
template.afterPropertiesSet();
return template;
}

@@ -128,6 +128,7 @@ public class RedisConfig extends CachingConfigurerSupport {
public RedisTemplate<String, BaseCUserEntity> getBaseCUserTokenRedisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, BaseCUserEntity> template = new RedisTemplate<String, BaseCUserEntity>();

template.setConnectionFactory(connectionFactory);
Jackson2JsonRedisSerializer<BaseCUserEntity> j = new Jackson2JsonRedisSerializer<BaseCUserEntity>(BaseCUserEntity.class);
ObjectMapper om = new ObjectMapper();
@@ -136,20 +137,20 @@ public class RedisConfig extends CachingConfigurerSupport {
// value值的序列化
template.setValueSerializer(j);
template.setHashKeySerializer(j);
template.setHashValueSerializer(j);

// key的序列化
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(connectionFactory);
template.afterPropertiesSet();
return template;
}
@Bean("cUserBasicInfoRedisTemplate")
public RedisTemplate<String, WxCUserBasicInfo> getCUserBasicInfoRedisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, WxCUserBasicInfo> template = new RedisTemplate<String, WxCUserBasicInfo>();
template.setConnectionFactory(connectionFactory);
Jackson2JsonRedisSerializer<WxCUserBasicInfo> j = new Jackson2JsonRedisSerializer<WxCUserBasicInfo>(WxCUserBasicInfo.class);
ObjectMapper om = new ObjectMapper();
@@ -157,20 +158,20 @@ public class RedisConfig extends CachingConfigurerSupport {
j.setObjectMapper(om);
// value值的序列化
template.setValueSerializer(j);
template.setHashKeySerializer(j);
template.setHashValueSerializer(j);

// key的序列化
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(connectionFactory);
template.afterPropertiesSet();
return template;
}

@Bean("mallRedisTemplate")
public RedisTemplate<String, WxMall> getMallRedisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, WxMall> template = new RedisTemplate<String, WxMall>();
template.setConnectionFactory(connectionFactory);
Jackson2JsonRedisSerializer<WxMall> j = new Jackson2JsonRedisSerializer<WxMall>(WxMall.class);
ObjectMapper om = new ObjectMapper();
@@ -178,60 +179,60 @@ public class RedisConfig extends CachingConfigurerSupport {
j.setObjectMapper(om);
// value值的序列化
template.setValueSerializer(j);
template.setHashKeySerializer(j);
template.setHashValueSerializer(j);

// key的序列化
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(connectionFactory);
template.afterPropertiesSet();
return template;
}

@Bean("subMallListRedisTemplate")
public RedisTemplate<String, List<WxMall>> getSubMallListRedisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, List<WxMall>> template = new RedisTemplate<String, List<WxMall>>();
template.setConnectionFactory(connectionFactory);
Jackson2JsonRedisSerializer<List> j = new Jackson2JsonRedisSerializer<List>(List.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
j.setObjectMapper(om);
// value值的序列化
template.setValueSerializer(j);
template.setHashKeySerializer(j);
template.setHashValueSerializer(j);

// key的序列化
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(connectionFactory);
template.afterPropertiesSet();
return template;
}

@Bean("couponDetailRedisTemplate")
public RedisTemplate<String, WxCouponCVo> getCouponDetailRedisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, WxCouponCVo> template = new RedisTemplate<String, WxCouponCVo>();
template.setConnectionFactory(connectionFactory);
Jackson2JsonRedisSerializer<WxCouponCVo> j = new Jackson2JsonRedisSerializer<WxCouponCVo>(WxCouponCVo.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
j.setObjectMapper(om);
// value值的序列化
template.setValueSerializer(j);
template.setHashKeySerializer(j);
template.setHashValueSerializer(j);

// key的序列化
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(connectionFactory);
template.afterPropertiesSet();
return template;
}

@Bean("couponChannelRedisTemplate")
public RedisTemplate<String, PageInfo<WxCouponChannelVo>> getCouponChannelRedisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, PageInfo<WxCouponChannelVo>> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
Jackson2JsonRedisSerializer<PageInfo> j = new Jackson2JsonRedisSerializer<PageInfo>(PageInfo.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
@@ -239,20 +240,20 @@ public class RedisConfig extends CachingConfigurerSupport {

// value值的序列化
template.setValueSerializer(j);
template.setHashKeySerializer(j);
template.setHashValueSerializer(j);

// key的序列化
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(connectionFactory);
template.afterPropertiesSet();
return template;
}

@Bean("buserTokenRedisTemplate")
public RedisTemplate<String, WxBuser> getBuserTokenRedisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, WxBuser> template = new RedisTemplate();
template.setConnectionFactory(connectionFactory);
Jackson2JsonRedisSerializer<WxBuser> j = new Jackson2JsonRedisSerializer(WxBuser.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
@@ -260,84 +261,62 @@ public class RedisConfig extends CachingConfigurerSupport {

// value值的序列化
template.setValueSerializer(j);
template.setHashKeySerializer(j);
template.setHashValueSerializer(j);

// key的序列化
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(connectionFactory);
template.afterPropertiesSet();
return template;
}

@Bean("pressOrderRedisTemplate")
public RedisTemplate<String, WxOrder> getPressOrderRedisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, WxOrder> template = new RedisTemplate<>();
Jackson2JsonRedisSerializer<WxOrder> j = new Jackson2JsonRedisSerializer(WxOrder.class);
template.setConnectionFactory(connectionFactory);
Jackson2JsonRedisSerializer<WxOrder> j = new Jackson2JsonRedisSerializer<WxOrder>(WxOrder.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
j.setObjectMapper(om);

// value值的序列化
template.setValueSerializer(j);
template.setHashKeySerializer(j);
template.setHashValueSerializer(j);

// key的序列化
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(connectionFactory);
template.afterPropertiesSet();
return template;
}

@Bean("wxAppinfoRedisTemplate")
public RedisTemplate<String, WxAppinfo> getWxAppinfoRedisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, WxAppinfo> template = new RedisTemplate<>();

Jackson2JsonRedisSerializer<WxAppinfo> j = new Jackson2JsonRedisSerializer(WxAppinfo.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
j.setObjectMapper(om);

// value值的序列化
template.setValueSerializer(j);
template.setHashKeySerializer(j);

// key的序列化
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());

@Bean("stringValueOperations")
public ValueOperations<String, String> getStringValueOperations(RedisConnectionFactory connectionFactory) {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(connectionFactory);
return template;
template.afterPropertiesSet();
return template.opsForValue();
}
@Bean("wxLevelConfigListRedisTemplate")
public RedisTemplate<String, List<WxLevelConfig>> getWxLevelConfigListRedisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, List<WxLevelConfig>> template = new RedisTemplate<>();
Jackson2JsonRedisSerializer<List> j = new Jackson2JsonRedisSerializer(List.class);
@Bean("objectCommonRedisTemplate")
public RedisTemplate<String, Object> getObjectValueOperations(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
Jackson2JsonRedisSerializer<Object> j = new Jackson2JsonRedisSerializer<Object>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
j.setObjectMapper(om);

// value值的序列化
template.setValueSerializer(j);
template.setHashKeySerializer(j);
template.setHashValueSerializer(j);

// key的序列化
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());

template.setConnectionFactory(connectionFactory);
return template;
}

@Bean("stringValueOperations")
public ValueOperations<String, String> getStringValueOperations(RedisConnectionFactory connectionFactory) {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(connectionFactory);
template.afterPropertiesSet();
return template.opsForValue();
return template;
}
}

+ 11
- 19
mallinkCApi/src/main/java/com/iformall/controller/BaseController.java View File

@@ -19,6 +19,8 @@ import com.iformall.service.wechat.FmOpenService;
import com.iformall.utils.Constant;
import com.iformall.utils.IPUtil;
import com.iformall.utils.MaUtil;
import com.iformall.utils.RedisCacheUtils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -58,8 +60,8 @@ public class BaseController {
private CUserTokenService cUserTokenService;
@Autowired
@Qualifier("cUserBasicInfoRedisTemplate")
RedisTemplate<String, WxCUserBasicInfo> cuserBasicInfoTemplate;
@Qualifier("objectCommonRedisTemplate")
RedisTemplate<String, Object> cuserBasicInfoTemplate;
@InitBinder
public void InitBinder(WebDataBinder dataBinder) {
@@ -131,7 +133,7 @@ public class BaseController {
if (baseuser.getRealUser() == null) {
throw new MallinkException(ErrorCode.USER_IS_EMPTY);
}
WxCUser user = (WxCUser) baseuser.getRealUser();
WxCUser user = baseuser.getRealUserObject(WxCUser.class);
TenantEntity tenantEntity = getTenantInfo();
if (user.getTenantId() == null) {
user.setTenantId(tenantEntity.getTenantId());
@@ -153,21 +155,16 @@ public class BaseController {
}
private WxCUserBasicInfo getCacheMember(Long memberId,String key,long seconds) throws Exception {
ValueOperations<String, WxCUserBasicInfo> operations = cuserBasicInfoTemplate.opsForValue();
WxCUserBasicInfo member;
// 缓存
boolean hasKey = cuserBasicInfoTemplate.hasKey(key);
if(hasKey) {
member = operations.get(key);
return member;
}else {
WxCUserBasicInfo member = RedisCacheUtils.getCacheObject(cuserBasicInfoTemplate, key, WxCUserBasicInfo.class);
if(null == member) {
member = wxCUserBasicInfoService.getById(memberId);
if (member == null) {
throw new MallinkException(ErrorCode.USER_IS_EMPTY);
}
operations.set(key, member, seconds, TimeUnit.SECONDS);
RedisCacheUtils.cache(cuserBasicInfoTemplate, key, member, seconds);
}
return operations.get(key);
return member;
}
public WxCUserBasicInfo getCacheMember() throws Exception {
@@ -184,14 +181,9 @@ public class BaseController {
public void removeCacheMember(Long memberId) {
String key = "webapp:member:"+memberId;
if (cuserBasicInfoTemplate.hasKey(key)) {
cuserBasicInfoTemplate.delete(key);
}
RedisCacheUtils.removeCache(cuserBasicInfoTemplate, key);
String shortKey = "webapp:short:member:"+memberId;
if (cuserBasicInfoTemplate.hasKey(shortKey)) {
cuserBasicInfoTemplate.delete(shortKey);
}
RedisCacheUtils.removeCache(cuserBasicInfoTemplate, shortKey);
}
// public WxCUserBasicInfo getMember() throws Exception {
// Long memberId = getMemberId();


+ 0
- 2
mallinkService/src/main/java/com/iformall/domain/po/WxLevelConfig.java View File

@@ -2,14 +2,12 @@ package com.iformall.domain.po;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.iformall.domain.po.base.BaseTenantEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.*;

@JsonIgnoreProperties(value = {"handler"})
@TableName(value = "wx_level_config")
@Data
@EqualsAndHashCode(callSuper = true)


+ 5
- 0
mallinkService/src/main/java/com/iformall/domain/po/base/BaseCUserEntity.java View File

@@ -1,5 +1,6 @@
package com.iformall.domain.po.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import com.iformall.utils.Constant;
@@ -35,6 +36,10 @@ public class BaseCUserEntity extends TenantEntity {
@io.swagger.annotations.ApiModelProperty(value="真实对象如wxCUser",name="realUser")
@TableField(exist = false)
private Object realUser;
public <T> T getRealUserObject(Class<T> clazz) {
return JSON.parseObject(JSON.toJSONString(realUser), clazz);
}

/**
* 是否是会员


+ 11
- 26
mallinkService/src/main/java/com/iformall/service/impl/CUserTokenServiceImpl.java View File

@@ -8,16 +8,14 @@ import com.iformall.mapper.WxCUserMapper;
import com.iformall.service.CUserTokenService;
import com.iformall.service.cuser.CUserServiceFactory;
import com.iformall.utils.Constant;
import com.iformall.utils.RedisCacheUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class CUserTokenServiceImpl implements CUserTokenService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@@ -26,8 +24,8 @@ public class CUserTokenServiceImpl implements CUserTokenService {
WxCUserMapper wxCUserMapper;

@Autowired
@Qualifier("baseCUserTokenRedisTemplate")
RedisTemplate<String, BaseCUserEntity> baseCUserTokenRedisTemplate;
@Qualifier("objectCommonRedisTemplate")
RedisTemplate<String, Object> baseCUserTokenRedisTemplate;

@Autowired
CUserServiceFactory cuserFactory;
@@ -42,42 +40,29 @@ public class CUserTokenServiceImpl implements CUserTokenService {
private void setCache(String token,BaseCUserEntity baseUser) {
if (null != baseUser) {
String key = Constant.tokenPrev + token;
ValueOperations<String, BaseCUserEntity> operations = baseCUserTokenRedisTemplate.opsForValue();
operations.set(key, baseUser, 3600*24*3, TimeUnit.SECONDS);
RedisCacheUtils.cache(baseCUserTokenRedisTemplate, key, baseUser, 3600*24*3);
}
}
@Override
public BaseCUserEntity getByToken(String token) {

BaseCUserEntity baseUser = null;

logger.info("获取用户信息start ..." + token);
String key = Constant.tokenPrev + token;
ValueOperations<String, BaseCUserEntity> operations = baseCUserTokenRedisTemplate.opsForValue();

// 缓存
boolean hasKey = baseCUserTokenRedisTemplate.hasKey(key);
if(hasKey) {
// 从缓存获取用户信息
baseUser = operations.get(key);
logger.info("缓存中获取用户信息End-" + token);
return baseUser;
BaseCUserEntity baseUser =RedisCacheUtils.getCacheObject(baseCUserTokenRedisTemplate, key, BaseCUserEntity.class);
if(null == baseUser) {
baseUser = cuserFactory.getCUserService(getPlat(token)).getByToken(token);
setCache(token,baseUser);
}
baseUser = cuserFactory.getCUserService(getPlat(token)).getByToken(token);
setCache(token,baseUser);
//operations.set(key, user, 3600, TimeUnit.SECONDS);
//logger.info("DB中获取用户信息End-" + token);
return baseUser;
}

@Override
public void removeTokenCache(String token) {
String key = Constant.tokenPrev + token;
boolean hasKey = baseCUserTokenRedisTemplate.hasKey(key);
if (hasKey) {
baseCUserTokenRedisTemplate.delete(key);
}
RedisCacheUtils.removeCache(baseCUserTokenRedisTemplate, key);
}

@Override


+ 24
- 30
mallinkService/src/main/java/com/iformall/service/impl/WxAppinfoServiceImpl.java View File

@@ -12,6 +12,8 @@ import com.iformall.enums.EnumPayWay;
import com.iformall.mapper.WxAppinfoMapper;
import com.iformall.service.WxAppinfoService;
import com.iformall.utils.Constant;
import com.iformall.utils.RedisCacheUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -31,8 +33,8 @@ public class WxAppinfoServiceImpl implements WxAppinfoService {
WxAppinfoMapper wxAppinfoMapper;

@Autowired
@Qualifier("wxAppinfoRedisTemplate")
RedisTemplate<String, WxAppinfo> wxAppinfoRedisTemplate;
@Qualifier("objectCommonRedisTemplate")
RedisTemplate<String, Object> wxAppinfoRedisTemplate;

@Override
public WxAppinfo getCAppInfo(TenantEntity tenantEntity,EnumPayWay payWay) {
@@ -115,15 +117,12 @@ public class WxAppinfoServiceImpl implements WxAppinfoService {
public WxAppinfo getByAppIdFromRedis(String appId) {
WxAppinfo record = null;
String key = Constant.appinfoPrev + appId;
ValueOperations<String, WxAppinfo> operations = wxAppinfoRedisTemplate.opsForValue();

if(wxAppinfoRedisTemplate.hasKey(key)){
record = operations.get(key);
return record;
}
record = this.getByAppId(appId);
if(record != null){
operations.set(key, record, 7, TimeUnit.DAYS);
record = RedisCacheUtils.getCacheObject(wxAppinfoRedisTemplate, key, WxAppinfo.class);
if (null == record) {
record = this.getByAppId(appId);
if(record != null){
RedisCacheUtils.cache(wxAppinfoRedisTemplate, key, record, 3600*24*7);
}
}
return record;
}
@@ -132,15 +131,12 @@ public class WxAppinfoServiceImpl implements WxAppinfoService {
public WxAppinfo getByIdFromRedis(Long id) {
WxAppinfo record = null;
String key = Constant.appinfoPrev + id;
ValueOperations<String, WxAppinfo> operations = wxAppinfoRedisTemplate.opsForValue();

if(wxAppinfoRedisTemplate.hasKey(key)){
record = operations.get(key);
return record;
}
record = this.getById(id);
if(record != null){
operations.set(key, record, 7, TimeUnit.DAYS);
record = RedisCacheUtils.getCacheObject(wxAppinfoRedisTemplate, key, WxAppinfo.class);
if (null == record) {
record = this.getById(id);
if(record != null){
RedisCacheUtils.cache(wxAppinfoRedisTemplate, key, record, 3600*24*7);
}
}
return record;
}
@@ -150,16 +146,14 @@ public class WxAppinfoServiceImpl implements WxAppinfoService {
WxAppinfo record = null;
String key = Constant.appinfoPrev + tenantId + "-" + payWayWechat.getPlat()
+ "-" + EnumAppType.C.getCode();
ValueOperations<String, WxAppinfo> operations = wxAppinfoRedisTemplate.opsForValue();
if(wxAppinfoRedisTemplate.hasKey(key)){
record = operations.get(key);
return record;
}
record = this.getCAppInfo(new TenantEntity() {{
setTenantId(tenantId);
}}, payWayWechat);
if(record != null){
operations.set(key, record, 7, TimeUnit.DAYS);
record = RedisCacheUtils.getCacheObject(wxAppinfoRedisTemplate, key, WxAppinfo.class);
if (null == record) {
record = this.getCAppInfo(new TenantEntity() {{
setTenantId(tenantId);
}}, payWayWechat);
if(record != null){
RedisCacheUtils.cache(wxAppinfoRedisTemplate, key, record, 3600*24*7);
}
}
return record;
}


+ 12
- 11
mallinkService/src/main/java/com/iformall/service/impl/WxLevelConfigServiceImpl.java View File

@@ -19,6 +19,8 @@ import com.iformall.mapper.WxLevelMerchantMapper;
import com.iformall.service.WxCUserBasicInfoService;
import com.iformall.service.WxLevelConfigService;
import com.iformall.utils.Constant;
import com.iformall.utils.RedisCacheUtils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -54,8 +56,8 @@ public class WxLevelConfigServiceImpl implements WxLevelConfigService {
private WxCUserBasicInfoService wxCUserBasicInfoService;

@Autowired
@Qualifier("wxLevelConfigListRedisTemplate")
RedisTemplate<String, List<WxLevelConfig>> wxLevelConfigListRedisTemplate;
@Qualifier("objectCommonRedisTemplate")
RedisTemplate<String, Object> wxLevelConfigListRedisTemplate;

@Override
public PageInfo<WxLevelConfig> listAsPage(TenantEntity tenantEntity,WxLevelConfig record, Integer pageIndex, Integer pageSize) {
@@ -140,16 +142,15 @@ public class WxLevelConfigServiceImpl implements WxLevelConfigService {
@Override
public List<WxLevelConfig> getByTenantInfo(TenantEntity tenantEntity) {
String key = Constant.levelConfigPrev + tenantEntity.getTenantId();
ValueOperations<String, List<WxLevelConfig>> operations = wxLevelConfigListRedisTemplate.opsForValue();
if(wxLevelConfigListRedisTemplate.hasKey(key)){
return operations.get(key);
List<WxLevelConfig> clist = RedisCacheUtils.getCacheListObject(wxLevelConfigListRedisTemplate, key, WxLevelConfig.class);
if (null == clist || clist.size() <= 0) {
WxLevelConfig wxLevelConfig = new WxLevelConfig();
wxLevelConfig.setTenantId(tenantEntity.getTenantId());
wxLevelConfig.setSortColumns(BaseEntity.SortField.Points_ASC);
clist = wxLevelConfigMapper.findList(wxLevelConfig);
RedisCacheUtils.cache(wxLevelConfigListRedisTemplate, key, clist, 3600*24*7);
}
WxLevelConfig wxLevelConfig = new WxLevelConfig();
wxLevelConfig.setTenantId(tenantEntity.getTenantId());
wxLevelConfig.setSortColumns(BaseEntity.SortField.Points_ASC);
List<WxLevelConfig> list = wxLevelConfigMapper.findList(wxLevelConfig);
operations.set(key, list, 7, TimeUnit.DAYS);
return list;
return clist;
}

@Override


+ 106
- 0
mallinkService/src/main/java/com/iformall/utils/RedisCacheUtils.java View File

@@ -0,0 +1,106 @@
package com.iformall.utils;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

public class RedisCacheUtils {

private static <T> T parseJson(String json,Class<T> clazz) {
return JSON.parseObject(json,new TypeReference<T>() {});
}
private static <T> List<T> parseListJson(String json,Class<T> clazz) {
return JSON.parseObject(json,new TypeReference<List<T>>() {});
}
private static <T> Set<T> parseSetJson(String json,Class<T> clazz) {
return JSON.parseObject(json,new TypeReference<Set<T>>() {});
}
public static void cache(RedisTemplate<String, Object> template,String key, Object value,long seconds) {
ValueOperations<String, Object> operations = template.opsForValue();
if (seconds > 0) {
operations.set(key, value, seconds,TimeUnit.SECONDS);
}else {
operations.set(key, value);
}
}
public static <T> T getCacheObject(RedisTemplate<String, Object> template,String key,Class<T> clazz) {
if (template.hasKey(key)) {
ValueOperations<String, Object> operations = template.opsForValue();
Object o = operations.get(key);
return parseJson(JSON.toJSONString(o), clazz);
}
return null;
}
public static <T> List<T> getCacheListObject(RedisTemplate<String, Object> template,String key,Class<T> clazz) {
if (template.hasKey(key)) {
ValueOperations<String, Object> operations = template.opsForValue();
Object o = operations.get(key);
return parseListJson(JSON.toJSONString(o), clazz);
}
return null;
}
public static <T> Set<T> getCacheSetObject(RedisTemplate<String, Object> template,String key,Class<T> clazz) {
if (template.hasKey(key)) {
ValueOperations<String, Object> operations = template.opsForValue();
Object o = operations.get(key);
return parseSetJson(JSON.toJSONString(o), clazz);
}
return null;
}
public static Map getCacheMap(RedisTemplate<String, Object> template,String key) {
if (template.hasKey(key)) {
ValueOperations<String, Object> operations = template.opsForValue();
Object o = operations.get(key);
return parseJson(JSON.toJSONString(o), Map.class);
}
return null;
}
public static String getCacheString(RedisTemplate<String, Object> template,String key) {
if (template.hasKey(key)) {
ValueOperations<String, Object> operations = template.opsForValue();
Object o = operations.get(key);
return JSON.toJSONString(o);
}
return null;
}
public static Long getCacheLong(RedisTemplate<String, Object> template,String key) {
if (template.hasKey(key)) {
ValueOperations<String, Object> operations = template.opsForValue();
Object o = operations.get(key);
return Long.parseLong(o.toString());
}
return null;
}
public static Integer getCacheInteger(RedisTemplate<String, Object> template,String key) {
if (template.hasKey(key)) {
ValueOperations<String, Object> operations = template.opsForValue();
Object o = operations.get(key);
return new Integer(o.toString());
}
return null;
}
public static void removeCache(RedisTemplate<String, Object> template,String key) {
if (template.hasKey(key)) {
template.delete(key);
}
}
}

+ 1
- 0
mallinkService/src/main/java/com/iformall/utils/RedisUtils.java View File

@@ -16,6 +16,7 @@ import java.util.concurrent.TimeUnit;
* @date 2017-07-17 21:12
*/
@Component
@Deprecated
public class RedisUtils {
@Autowired
private RedisTemplate redisTemplate;


Loading…
Cancel
Save