diff --git a/mallinkAdmin/src/main/java/com/iformall/config/RedisConfig.java b/mallinkAdmin/src/main/java/com/iformall/config/RedisConfig.java index db0c20110..1508e5108 100644 --- a/mallinkAdmin/src/main/java/com/iformall/config/RedisConfig.java +++ b/mallinkAdmin/src/main/java/com/iformall/config/RedisConfig.java @@ -1,14 +1,25 @@ package com.iformall.config; +import com.iformall.domain.po.PushLimit; +import com.iformall.domain.po.WxScoreRules; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import redis.clients.jedis.JedisPool; -import redis.clients.jedis.JedisPoolConfig; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.cache.RedisCacheManager; +import org.springframework.data.redis.cache.RedisCacheWriter; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +import java.time.Duration; +import java.util.HashMap; +import java.util.Map; /** * Created by yangqj on 2017/4/30. @@ -19,36 +30,60 @@ public class RedisConfig extends CachingConfigurerSupport { private final Logger logger = LoggerFactory.getLogger(this.getClass()); - @Value("${spring.redis.host}") - private String host; + //缓存管理器 + @Bean + public CacheManager cacheManager(RedisConnectionFactory connectionFactory) { + //user信息缓存配置 + RedisCacheConfiguration userCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(10)).disableCachingNullValues().prefixKeysWith("user"); + Map redisCacheConfigurationMap = new HashMap<>(); + redisCacheConfigurationMap.put("user", userCacheConfiguration); + //初始化一个RedisCacheWriter + RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory); + // 设置CacheManager的值序列化方式为JdkSerializationRedisSerializer,但其实RedisCacheConfiguration默认就是使用StringRedisSerializer序列化key,JdkSerializationRedisSerializer序列化value,所以以下注释代码为默认实现 + // ClassLoader loader = this.getClass().getClassLoader(); + // JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer(loader); + // RedisSerializationContext.SerializationPair pair = RedisSerializationContext.SerializationPair.fromSerializer(jdkSerializer); + // RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair); + RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig(); + //设置默认超过期时间是30秒 + defaultCacheConfig.entryTtl(Duration.ofSeconds(30)); + //初始化RedisCacheManager + RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig, redisCacheConfigurationMap); + return cacheManager; + } - @Value("${spring.redis.port}") - private int port; + @Bean("pushLimitRedisTemplate") + public RedisTemplate getPushLimitRedisTemplate(RedisConnectionFactory connectionFactory) { + RedisTemplate template = new RedisTemplate(); - @Value("${spring.redis.timeout}") - private int timeout; + Jackson2JsonRedisSerializer j = new Jackson2JsonRedisSerializer(PushLimit.class); + // value值的序列化 + template.setValueSerializer(j); + template.setHashKeySerializer(j); - @Value("${spring.redis.pool.max-idle}") - private int maxIdle; + // key的序列化 + template.setKeySerializer(new StringRedisSerializer()); + template.setHashKeySerializer(new StringRedisSerializer()); - @Value("${spring.redis.pool.max-wait}") - private long maxWaitMillis; - - @Value("${spring.redis.password}") - private String password; + template.setConnectionFactory(connectionFactory); + return template; + } - @Bean - public JedisPool redisPoolFactory() { - logger.info("JedisPool注入成功!!"); - logger.info("redis地址:" + host + ":" + port); - JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); - jedisPoolConfig.setMaxIdle(maxIdle); - jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); + @Bean("scoreRuleRedisTemplate") + public RedisTemplate getScoreRuleRedisTemplate(RedisConnectionFactory connectionFactory) { + RedisTemplate template = new RedisTemplate(); - JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout,password); + Jackson2JsonRedisSerializer j = new Jackson2JsonRedisSerializer(WxScoreRules.class); + // value值的序列化 + template.setValueSerializer(j); + template.setHashKeySerializer(j); - return jedisPool; - } + // key的序列化 + template.setKeySerializer(new StringRedisSerializer()); + template.setHashKeySerializer(new StringRedisSerializer()); + template.setConnectionFactory(connectionFactory); + return template; + } } diff --git a/mallinkAdmin/src/main/resources/application-dev.yml b/mallinkAdmin/src/main/resources/application-dev.yml index e39fdf00c..2c6543034 100644 --- a/mallinkAdmin/src/main/resources/application-dev.yml +++ b/mallinkAdmin/src/main/resources/application-dev.yml @@ -1,7 +1,7 @@ spring: # JDBC datasource: - url: jdbc:mysql://202.165.179.86:3306/mallink?characterEncoding=UTF-8&useSSL=false + url: jdbc:mysql://202.165.179.86:3306/mallink?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useUnicode=true&useSSL=false username: ENC(dZ8fmrtuBMQYaRytKQgTqg==) password: ENC(OZXsE6/Tj+V1Yu2wdjnIGbNOaBVQzi9W) type: com.alibaba.druid.pool.DruidDataSource @@ -28,13 +28,15 @@ spring: host: 202.165.179.86 port: 6789 password: ENC(YOLO4buIPjiYfosG+Akk3XZ9HYrbCFco) - timeout: 0 + timeout: 2000 expire: 1800 #30分钟 - pool: - max-active: 8 - max-idle: 8 - max-wait: -1 - min-idle: 0 + database: 1 + jedis: + pool: + max-active: 8 + max-idle: 8 + max-wait: -1 + min-idle: 0 logging: level: diff --git a/mallinkAdmin/src/main/resources/application-prod.yml b/mallinkAdmin/src/main/resources/application-prod.yml index bd0f1a701..00098be5f 100644 --- a/mallinkAdmin/src/main/resources/application-prod.yml +++ b/mallinkAdmin/src/main/resources/application-prod.yml @@ -25,13 +25,15 @@ spring: host: 202.165.179.86 port: 6789 password: ENC(YOLO4buIPjiYfosG+Akk3XZ9HYrbCFco) - timeout: 0 + timeout: 2000 expire: 1800 #30分钟 - pool: - max-active: 8 - max-idle: 8 - max-wait: -1 - min-idle: 0 + database: 1 + jedis: + pool: + max-active: 8 + max-idle: 8 + max-wait: -1 + min-idle: 0 logging: level: diff --git a/mallinkService/pom.xml b/mallinkService/pom.xml index 5f7a6419c..ba3d76989 100644 --- a/mallinkService/pom.xml +++ b/mallinkService/pom.xml @@ -10,4 +10,8 @@ 4.0.0 mallinkService + + + ${project.artifactId} + \ No newline at end of file diff --git a/mallinkService/src/main/java/com/iformall/service/impl/PushLimitServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/PushLimitServiceImpl.java index a99dffed5..a00e2cd5e 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/PushLimitServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/PushLimitServiceImpl.java @@ -1,21 +1,18 @@ package com.iformall.service.impl; -import com.github.pagehelper.PageHelper; -import com.github.pagehelper.PageInfo; import com.iformall.common.ErrorCode; import com.iformall.common.IdWorker; import com.iformall.domain.po.PushLimit; -import com.iformall.domain.po.WxCUser; -import com.iformall.domain.po.WxCoupon; import com.iformall.exception.MallinkException; import com.iformall.mapper.PushLimitMapper; import com.iformall.mapper.WxCouponActionLogMapper; import com.iformall.service.PushLimitService; import com.iformall.utils.DateUtils; -import com.iformall.utils.RedisUtils; 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.stereotype.Service; import java.util.Date; @@ -35,7 +32,8 @@ public class PushLimitServiceImpl implements PushLimitService { WxCouponActionLogMapper wxCouponActionLogMapper; @Autowired - RedisUtils redisUtils; + @Qualifier("pushLimitRedisTemplate") + private RedisTemplate pushLimitRedisTemplate; @Override public PushLimit getPushLimit(String tenantId) { @@ -43,9 +41,9 @@ public class PushLimitServiceImpl implements PushLimitService { String key = keyPrev + tenantId; // 缓存存在 - boolean hasKey = redisUtils.hasKey(key); + boolean hasKey = pushLimitRedisTemplate.hasKey(key); if (hasKey) { - PushLimit pushLimit = redisUtils.get(key, PushLimit.class); + PushLimit pushLimit = pushLimitRedisTemplate.opsForValue().get(key); logger.info("PushLimitServiceImpl.getPushLimit() : 从缓存中获取了疲劳度 >> " + pushLimit.toString()); return pushLimit; } @@ -71,7 +69,7 @@ public class PushLimitServiceImpl implements PushLimitService { } // 插入缓存 - redisUtils.set(key, pushLimit); + pushLimitRedisTemplate.opsForValue().set(key, pushLimit); logger.info("PushLimitServiceImpl.getPushLimit() : 疲劳度插入缓存 >> " + pushLimit.toString()); return pushLimit; } @@ -82,9 +80,9 @@ public class PushLimitServiceImpl implements PushLimitService { // 缓存存在,删除缓存 String key = keyPrev + record.getTenantId(); - boolean hasKey = redisUtils.hasKey(key); + boolean hasKey = pushLimitRedisTemplate.hasKey(key); if (hasKey) { - redisUtils.delete(key); + pushLimitRedisTemplate.delete(key); logger.info("PushLimitServiceImpl.saveOrUpdate() : 从缓存中删除疲劳度 >> " + record.toString()); } } diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxLevelConfigServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxLevelConfigServiceImpl.java index 46210068a..9402a099c 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxLevelConfigServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxLevelConfigServiceImpl.java @@ -20,7 +20,6 @@ public class WxLevelConfigServiceImpl implements WxLevelConfigService { @Autowired WxLevelConfigMapper wxLevelConfigMapper; - @Override public PageInfo listAsPage(WxLevelConfig record, Integer pageIndex, Integer pageSize) { return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxLevelConfigMapper.findList(record)); diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxScoreRulesServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxScoreRulesServiceImpl.java index d20b85a57..982ec41ac 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxScoreRulesServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxScoreRulesServiceImpl.java @@ -12,6 +12,8 @@ import com.iformall.utils.RedisUtils; 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.stereotype.Service; import java.util.Date; @@ -33,7 +35,8 @@ public class WxScoreRulesServiceImpl implements WxScoreRulesService { WxCUserBasicInfoService wxCUserBasicInfoService; @Autowired - RedisUtils redisUtils; + @Qualifier("scoreRuleRedisTemplate") + RedisTemplate scoreRulesRedisTemplate; @Override public WxScoreRules getScoreRules(String tenantId) { @@ -41,8 +44,8 @@ public class WxScoreRulesServiceImpl implements WxScoreRulesService { String key = keyPrev + tenantId; // 缓存存在 - if (redisUtils.hasKey(key)) { - WxScoreRules wScoreRules = redisUtils.get(key, WxScoreRules.class); + if (scoreRulesRedisTemplate.hasKey(key)) { + WxScoreRules wScoreRules = scoreRulesRedisTemplate.opsForValue().get(key); logger.info("WxScoreRulesServiceImpl.getScoreRules() : 从缓存中获取了成长值设置 >> " + wScoreRules.toString()); return wScoreRules; } @@ -70,7 +73,7 @@ public class WxScoreRulesServiceImpl implements WxScoreRulesService { } // 插入缓存 - redisUtils.set(key, scoreRules); + scoreRulesRedisTemplate.opsForValue().set(key, scoreRules); logger.info("WxScoreRulesServiceImpl.getScoreRules() : 成长值设置插入缓存 >> " + scoreRules.toString()); return scoreRules; @@ -91,9 +94,9 @@ public class WxScoreRulesServiceImpl implements WxScoreRulesService { // 缓存存在,删除缓存 String key = keyPrev + record.getTenantId(); - boolean hasKey = redisUtils.hasKey(key); + boolean hasKey = scoreRulesRedisTemplate.hasKey(key); if (hasKey) { - redisUtils.delete(key); + scoreRulesRedisTemplate.delete(key); logger.info("WxScoreRulesServiceImpl.saveOrUpdate() : 从缓存中删除成长值设置 >> " + record.toString()); } } diff --git a/pom.xml b/pom.xml index 7a8c6e71b..378b91a6e 100644 --- a/pom.xml +++ b/pom.xml @@ -56,10 +56,6 @@ org.springframework.boot spring-boot-starter-data-redis - - org.springframework.boot - spring-boot-starter-freemarker - org.springframework.boot spring-boot-configuration-processor