/**
* Created on 2019/3/31.
* Title: Simple
* Description:配置類
* Copyright: Copyright(c) 2019
* Company:
*
* @author wy
*/
@Configuration
@Slf4j
public class RedisConfig {
@Value("${redis.host}")
private String host;
@Value("${redis.port}")
private Integer port;
@Value("${redis.password}")
private String password;
@Value("${redis.timeout}")
private int timeout;
@Value("${redis.pool.max-total}")
private int maxTotal;
@PostConstruct
public void init()
{
log.info("ids redis host {}, port {}", this.host, this.port);
}
@Primary
@Bean(name="redisConnectionFactory")
public JedisConnectionFactory idsRedisConnectionFactory() {
if (StringUtil.isBlank(this.host)) {
log.info("No need to initialize ids redis.");
return null;
}
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName(this.host);
factory.setPort(this.port);
//factory.setDatabase(this.index);
factory.setTimeout(this.timeout);
factory.setUsePool(true);
if (StringUtil.isNotBlank(this.password)) {
factory.setPassword(this.password);
log.info("idsRedisConnectionFactory setPassword {}", this.password);
}
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(this.maxTotal);
factory.setPoolConfig(jedisPoolConfig);
return factory;
}
@Primary
@Bean(name="idsRedisTemplate")
public RedisTemplate<String, String> idsRedisTemplate(@Qualifier("redisConnectionFactory")
RedisConnectionFactory cacheRedisConnectionFactory) {
if (null == cacheRedisConnectionFactory) {
return null;
}
StringRedisTemplate template = new StringRedisTemplate(cacheRedisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.afterPropertiesSet();
log.warn("set ids redis {}:{}", this.host, this.port);
return template;
}
}
set和get方法看起来很简单,它可能就来自于同一个Redis连接池的不同的redis的连接。为了使得所有的操作都来自于同一个连接,这样就可以保证很多命令,一个一个去执行,redis性能是很好的,但是存在网络延时的问题,会浪费掉很多的时间,所以批处理很快的可以执行快速的读写,执行此操作,有两个接口,还有一个RedisSessionCallback接口,但是这个接口并不有好,比较底层的封装,所以现在都在实现sessioncallback接口,通过这个接口就可以把多个命令放入到同一个Redis连接中去执行,可以使用同一个连接进行批量执行
/**
* Created on 2019/3/31.
* Title: Simple
* Description:測試類
* Copyright: Copyright(c) 2018
* Company:
*
* @author wy
*/
@Service
public class TestRedis {
@Autowired
private RedisTemplate redisTemplate;
public void redis() {
Role role = new Role();
role.setId(10);
role.setRoleName("CEO");
SessionCallback sessionCallback = new SessionCallback<Role>() {
@Override
public Role execute(RedisOperations redisOperations) throws DataAccessException {
redisOperations.boundValueOps("role_1").set(role);
return (Role) redisOperations.boundValueOps("role_1").get();
}
};
Role role1 = (Role) redisTemplate.execute(sessionCallback);
System.out.println(role1.getRoleName());
}
}
配置中心:
#redis
redis:
host: 127.0.0.1
port: 6379
password:
timeout: 2000
pool:
max-active: 8
max-idle: 8
max-wait: -1
min-idle: 0
max-total: 10