配置redis信息
spring:
redis:
host:
port: 6379
password:
database: 15
jedis:
pool:
#最大连接数
max-active: 100
#最大阻塞等待时间(负数表示没限制)
max-wait: 2000
#最大空闲
max-idle: 500
#最小空闲
min-idle: 8
#连接超时时间
timeout: 5000
配置redis连接池
package com.cloud.models.doctor.redis;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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 lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
-
RedisConfig
-
功能描述:
-
@author liuqiang
-
@date 2019年12月20日
*/
@Slf4j
@Configuration
@EnableCaching
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig extends CachingConfigurerSupport {@Value("${spring.redis.host}")
private String host;@Value("${spring.redis.port}")
private int port;@Value("${spring.redis.timeout}")
private int timeout;@Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle;@Value("${spring.redis.jedis.pool.max-wait}")
private long maxWaitMillis;@Value("${spring.redis.password}")
private String password;/**
- 配置 redis 连接池
- @return
*/
@Bean
public JedisPool redisPoolFactory(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
return new JedisPool(jedisPoolConfig, host, port, timeout, password);
}
}
编写redis应用
package com.cloud.models.doctor.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.cloud.models.common.dto.ResCommonDto;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Slf4j
@Service
public class DoctorRedisServiceImpl implements DoctorRedisService {
@Autowired
JedisPool pool;
@Value("${spring.redis.database}")
private int database;
@Override
public ResCommonDto findByKey(String key) {
Jedis jedis = null;
ResCommonDto resCommonDto = new ResCommonDto();
try{
jedis = pool.getResource();
jedis.select(database);
jedis.get(key);
resCommonDto.setMsg(jedis.get(key));
}finally {
if(null != jedis){
jedis.close(); // 释放资源还给连接池
}
}
return resCommonDto;
}
@Override
public void addKey(String key,String value,String time) {
log.error("addKey 接口调用 : key "+ key +" value "+ value);
Jedis jedis = null;
try{
jedis = pool.getResource();
jedis.select(database);
if("".equals(time) || time == null) {
jedis.set(key,value);
}else {
jedis.setex(key, Integer.valueOf(time), value);
}
}finally{
if(null != jedis){
jedis.close(); // 释放资源还给连接池
}
}
}
@Override
public void removeByKey(String key) {
Jedis jedis = null;
try{
jedis = pool.getResource();
jedis.select(database);
jedis.del(key);
}finally{
if(null != jedis){
jedis.close(); // 释放资源还给连接池
}
}
}
}
446

被折叠的 条评论
为什么被折叠?



