Просмотр исходного кода

[redis优化成长值和等级][优化]:redis缓存优化成长值读取及等级读取

release_toaliyun_real
Stormeye.Wu 7 лет назад
Родитель
Сommit
4e3d1b31ed
8 измененных файлов: 100 добавлений и 61 удалений
  1. +61
    -26
      mallinkAdmin/src/main/java/com/iformall/config/RedisConfig.java
  2. +9
    -7
      mallinkAdmin/src/main/resources/application-dev.yml
  3. +8
    -6
      mallinkAdmin/src/main/resources/application-prod.yml
  4. +4
    -0
      mallinkService/pom.xml
  5. +9
    -11
      mallinkService/src/main/java/com/iformall/service/impl/PushLimitServiceImpl.java
  6. +0
    -1
      mallinkService/src/main/java/com/iformall/service/impl/WxLevelConfigServiceImpl.java
  7. +9
    -6
      mallinkService/src/main/java/com/iformall/service/impl/WxScoreRulesServiceImpl.java
  8. +0
    -4
      pom.xml

+ 61
- 26
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<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;
}

}

+ 9
- 7
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:


+ 8
- 6
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:


+ 4
- 0
mallinkService/pom.xml Просмотреть файл

@@ -10,4 +10,8 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>mallinkService</artifactId>

<build>
<finalName>${project.artifactId}</finalName>
</build>
</project>

+ 9
- 11
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<String, PushLimit> 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());
}
}


+ 0
- 1
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<WxLevelConfig> listAsPage(WxLevelConfig record, Integer pageIndex, Integer pageSize) {
return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxLevelConfigMapper.findList(record));


+ 9
- 6
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<String, WxScoreRules> 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());
}
}


+ 0
- 4
pom.xml Просмотреть файл

@@ -56,10 +56,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>


Загрузка…
Отмена
Сохранить