| @@ -1,14 +1,25 @@ | |||||
| package com.iformall.config; | package com.iformall.config; | ||||
| import com.iformall.domain.po.PushLimit; | |||||
| import com.iformall.domain.po.WxScoreRules; | |||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | 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.CachingConfigurerSupport; | ||||
| 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; | ||||
| 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. | * Created by yangqj on 2017/4/30. | ||||
| @@ -19,36 +30,60 @@ public class RedisConfig extends CachingConfigurerSupport { | |||||
| private final Logger logger = LoggerFactory.getLogger(this.getClass()); | 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<String, RedisCacheConfiguration> 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<Object> 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<String, PushLimit> getPushLimitRedisTemplate(RedisConnectionFactory connectionFactory) { | |||||
| RedisTemplate<String, PushLimit> template = new RedisTemplate<String, PushLimit>(); | |||||
| @Value("${spring.redis.timeout}") | |||||
| private int timeout; | |||||
| Jackson2JsonRedisSerializer<PushLimit> j = new Jackson2JsonRedisSerializer<PushLimit>(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<String, WxScoreRules> getScoreRuleRedisTemplate(RedisConnectionFactory connectionFactory) { | |||||
| RedisTemplate<String, WxScoreRules> template = new RedisTemplate<String, WxScoreRules>(); | |||||
| JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout,password); | |||||
| Jackson2JsonRedisSerializer<WxScoreRules> j = new Jackson2JsonRedisSerializer<WxScoreRules>(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; | |||||
| } | |||||
| } | } | ||||
| @@ -1,7 +1,7 @@ | |||||
| spring: | spring: | ||||
| # JDBC | # JDBC | ||||
| datasource: | 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==) | username: ENC(dZ8fmrtuBMQYaRytKQgTqg==) | ||||
| password: ENC(OZXsE6/Tj+V1Yu2wdjnIGbNOaBVQzi9W) | password: ENC(OZXsE6/Tj+V1Yu2wdjnIGbNOaBVQzi9W) | ||||
| type: com.alibaba.druid.pool.DruidDataSource | type: com.alibaba.druid.pool.DruidDataSource | ||||
| @@ -28,13 +28,15 @@ spring: | |||||
| host: 202.165.179.86 | host: 202.165.179.86 | ||||
| port: 6789 | port: 6789 | ||||
| password: ENC(YOLO4buIPjiYfosG+Akk3XZ9HYrbCFco) | password: ENC(YOLO4buIPjiYfosG+Akk3XZ9HYrbCFco) | ||||
| timeout: 0 | |||||
| timeout: 2000 | |||||
| expire: 1800 #30分钟 | 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: | logging: | ||||
| level: | level: | ||||
| @@ -25,13 +25,15 @@ spring: | |||||
| host: 202.165.179.86 | host: 202.165.179.86 | ||||
| port: 6789 | port: 6789 | ||||
| password: ENC(YOLO4buIPjiYfosG+Akk3XZ9HYrbCFco) | password: ENC(YOLO4buIPjiYfosG+Akk3XZ9HYrbCFco) | ||||
| timeout: 0 | |||||
| timeout: 2000 | |||||
| expire: 1800 #30分钟 | 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: | logging: | ||||
| level: | level: | ||||
| @@ -10,4 +10,8 @@ | |||||
| <modelVersion>4.0.0</modelVersion> | <modelVersion>4.0.0</modelVersion> | ||||
| <artifactId>mallinkService</artifactId> | <artifactId>mallinkService</artifactId> | ||||
| <build> | |||||
| <finalName>${project.artifactId}</finalName> | |||||
| </build> | |||||
| </project> | </project> | ||||
| @@ -1,21 +1,18 @@ | |||||
| package com.iformall.service.impl; | package com.iformall.service.impl; | ||||
| import com.github.pagehelper.PageHelper; | |||||
| import com.github.pagehelper.PageInfo; | |||||
| import com.iformall.common.ErrorCode; | import com.iformall.common.ErrorCode; | ||||
| import com.iformall.common.IdWorker; | import com.iformall.common.IdWorker; | ||||
| import com.iformall.domain.po.PushLimit; | 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.exception.MallinkException; | ||||
| import com.iformall.mapper.PushLimitMapper; | import com.iformall.mapper.PushLimitMapper; | ||||
| import com.iformall.mapper.WxCouponActionLogMapper; | import com.iformall.mapper.WxCouponActionLogMapper; | ||||
| import com.iformall.service.PushLimitService; | import com.iformall.service.PushLimitService; | ||||
| import com.iformall.utils.DateUtils; | import com.iformall.utils.DateUtils; | ||||
| import com.iformall.utils.RedisUtils; | |||||
| 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.beans.factory.annotation.Qualifier; | |||||
| import org.springframework.data.redis.core.RedisTemplate; | |||||
| import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||
| import java.util.Date; | import java.util.Date; | ||||
| @@ -35,7 +32,8 @@ public class PushLimitServiceImpl implements PushLimitService { | |||||
| WxCouponActionLogMapper wxCouponActionLogMapper; | WxCouponActionLogMapper wxCouponActionLogMapper; | ||||
| @Autowired | @Autowired | ||||
| RedisUtils redisUtils; | |||||
| @Qualifier("pushLimitRedisTemplate") | |||||
| private RedisTemplate<String, PushLimit> pushLimitRedisTemplate; | |||||
| @Override | @Override | ||||
| public PushLimit getPushLimit(String tenantId) { | public PushLimit getPushLimit(String tenantId) { | ||||
| @@ -43,9 +41,9 @@ public class PushLimitServiceImpl implements PushLimitService { | |||||
| String key = keyPrev + tenantId; | String key = keyPrev + tenantId; | ||||
| // 缓存存在 | // 缓存存在 | ||||
| boolean hasKey = redisUtils.hasKey(key); | |||||
| boolean hasKey = pushLimitRedisTemplate.hasKey(key); | |||||
| if (hasKey) { | if (hasKey) { | ||||
| PushLimit pushLimit = redisUtils.get(key, PushLimit.class); | |||||
| PushLimit pushLimit = pushLimitRedisTemplate.opsForValue().get(key); | |||||
| logger.info("PushLimitServiceImpl.getPushLimit() : 从缓存中获取了疲劳度 >> " + pushLimit.toString()); | logger.info("PushLimitServiceImpl.getPushLimit() : 从缓存中获取了疲劳度 >> " + pushLimit.toString()); | ||||
| return pushLimit; | 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()); | logger.info("PushLimitServiceImpl.getPushLimit() : 疲劳度插入缓存 >> " + pushLimit.toString()); | ||||
| return pushLimit; | return pushLimit; | ||||
| } | } | ||||
| @@ -82,9 +80,9 @@ public class PushLimitServiceImpl implements PushLimitService { | |||||
| // 缓存存在,删除缓存 | // 缓存存在,删除缓存 | ||||
| String key = keyPrev + record.getTenantId(); | String key = keyPrev + record.getTenantId(); | ||||
| boolean hasKey = redisUtils.hasKey(key); | |||||
| boolean hasKey = pushLimitRedisTemplate.hasKey(key); | |||||
| if (hasKey) { | if (hasKey) { | ||||
| redisUtils.delete(key); | |||||
| pushLimitRedisTemplate.delete(key); | |||||
| logger.info("PushLimitServiceImpl.saveOrUpdate() : 从缓存中删除疲劳度 >> " + record.toString()); | logger.info("PushLimitServiceImpl.saveOrUpdate() : 从缓存中删除疲劳度 >> " + record.toString()); | ||||
| } | } | ||||
| } | } | ||||
| @@ -20,7 +20,6 @@ public class WxLevelConfigServiceImpl implements WxLevelConfigService { | |||||
| @Autowired | @Autowired | ||||
| WxLevelConfigMapper wxLevelConfigMapper; | WxLevelConfigMapper wxLevelConfigMapper; | ||||
| @Override | @Override | ||||
| public PageInfo<WxLevelConfig> listAsPage(WxLevelConfig record, Integer pageIndex, Integer pageSize) { | public PageInfo<WxLevelConfig> listAsPage(WxLevelConfig record, Integer pageIndex, Integer pageSize) { | ||||
| return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxLevelConfigMapper.findList(record)); | return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxLevelConfigMapper.findList(record)); | ||||
| @@ -12,6 +12,8 @@ import com.iformall.utils.RedisUtils; | |||||
| 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.beans.factory.annotation.Qualifier; | |||||
| import org.springframework.data.redis.core.RedisTemplate; | |||||
| import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||
| import java.util.Date; | import java.util.Date; | ||||
| @@ -33,7 +35,8 @@ public class WxScoreRulesServiceImpl implements WxScoreRulesService { | |||||
| WxCUserBasicInfoService wxCUserBasicInfoService; | WxCUserBasicInfoService wxCUserBasicInfoService; | ||||
| @Autowired | @Autowired | ||||
| RedisUtils redisUtils; | |||||
| @Qualifier("scoreRuleRedisTemplate") | |||||
| RedisTemplate<String, WxScoreRules> scoreRulesRedisTemplate; | |||||
| @Override | @Override | ||||
| public WxScoreRules getScoreRules(String tenantId) { | public WxScoreRules getScoreRules(String tenantId) { | ||||
| @@ -41,8 +44,8 @@ public class WxScoreRulesServiceImpl implements WxScoreRulesService { | |||||
| String key = keyPrev + tenantId; | 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()); | logger.info("WxScoreRulesServiceImpl.getScoreRules() : 从缓存中获取了成长值设置 >> " + wScoreRules.toString()); | ||||
| return wScoreRules; | 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()); | logger.info("WxScoreRulesServiceImpl.getScoreRules() : 成长值设置插入缓存 >> " + scoreRules.toString()); | ||||
| return scoreRules; | return scoreRules; | ||||
| @@ -91,9 +94,9 @@ public class WxScoreRulesServiceImpl implements WxScoreRulesService { | |||||
| // 缓存存在,删除缓存 | // 缓存存在,删除缓存 | ||||
| String key = keyPrev + record.getTenantId(); | String key = keyPrev + record.getTenantId(); | ||||
| boolean hasKey = redisUtils.hasKey(key); | |||||
| boolean hasKey = scoreRulesRedisTemplate.hasKey(key); | |||||
| if (hasKey) { | if (hasKey) { | ||||
| redisUtils.delete(key); | |||||
| scoreRulesRedisTemplate.delete(key); | |||||
| logger.info("WxScoreRulesServiceImpl.saveOrUpdate() : 从缓存中删除成长值设置 >> " + record.toString()); | logger.info("WxScoreRulesServiceImpl.saveOrUpdate() : 从缓存中删除成长值设置 >> " + record.toString()); | ||||
| } | } | ||||
| } | } | ||||
| @@ -56,10 +56,6 @@ | |||||
| <groupId>org.springframework.boot</groupId> | <groupId>org.springframework.boot</groupId> | ||||
| <artifactId>spring-boot-starter-data-redis</artifactId> | <artifactId>spring-boot-starter-data-redis</artifactId> | ||||
| </dependency> | </dependency> | ||||
| <dependency> | |||||
| <groupId>org.springframework.boot</groupId> | |||||
| <artifactId>spring-boot-starter-freemarker</artifactId> | |||||
| </dependency> | |||||
| <dependency> | <dependency> | ||||
| <groupId>org.springframework.boot</groupId> | <groupId>org.springframework.boot</groupId> | ||||
| <artifactId>spring-boot-configuration-processor</artifactId> | <artifactId>spring-boot-configuration-processor</artifactId> | ||||