由于版本原因,SpringBoot2.0整合Redis和低版本的SpringBoot不太一样,经测试,本文这套整合方案可以使用。
更多关于SpringBoot的总结请点击:SpringBoot使用总结
//redis clienet
compile("redis.clients:jedis:2.9.0")
//commons pool
compile("org.apache.commons:commons-pool2:2.6.0")
//redis starter
compile("org.springframework.boot:spring-boot-starter-redis:2.0.4.RELEASE")
//redis data
compile("org.springframework.data:spring-data-redis:2.0.5.RELEASE")
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=1000
注意,如果使用的SpringBoot版本是1.5,那么spring.redis.jedis.pool.max-idl写成spring.redis.pool.max-idl
@Autowired
private StringRedisTemplate stringRedisTemplate;
@RequestMapping(value="/redis")
@ResponseBody
public String redis(){
System.out.println("hello");
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
String hello = ops.get("hello");
ops.set("redisTest","hello Redis",10000);
return hello;
}
经测试,以上配置全部可用
@Slf4j
@Component
public class RedisUtil {
@Autowired
private StringRedisTemplate redisTemplate;
/**
* 写入redis缓存(不设置expire存活时间)
* @param key
* @param value
* @return
*/
public boolean set(final String key, String value){
boolean result = false;
try {
ValueOperations operations = redisTemplate.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception e) {
log.error("写入redis缓存失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* 写入redis缓存(设置expire存活时间)
* @param key
* @param value
* @param expire 毫秒为单位
* @return
*/
public boolean set(final String key, String value, Long expire){
boolean result = false;
try {
ValueOperations operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expire, TimeUnit.MILLISECONDS);
result = true;
} catch (Exception e) {
log.error("写入redis缓存(设置expire存活时间)失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* 读取redis缓存
* @param key
* @return
*/
public Object get(final String key){
Object result = null;
try {
ValueOperations operations = redisTemplate.opsForValue();
result = operations.get(key);
} catch (Exception e) {
log.error("读取redis缓存失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* 判断redis缓存中是否有对应的key
* @param key
* @return
*/
public boolean exists(final String key){
boolean result = false;
try {
result = redisTemplate.hasKey(key);
} catch (Exception e) {
log.error("判断redis缓存中是否有对应的key失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* redis根据key删除对应的value
* @param key
* @return
*/
public boolean remove(final String key){
boolean result = false;
try {
if(exists(key)){
redisTemplate.delete(key);
}
result = true;
} catch (Exception e) {
log.error("redis根据key删除对应的value失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* redis根据keys批量删除对应的value
* @param keys
* @return
*/
public void remove(final List<String> keys){
for(String key : keys){
remove(key);
}
}
/**
* hash类型写入redis缓存(针对需要持久保存的对象)
* @param key
* @param value
* @return
*/
public boolean hset(String hkey,String key, String value){
boolean result = false;
try {
HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
hashOperations.put(hkey,key,value);
result = true;
} catch (Exception e) {
log.error("写入redis缓存失败!错误信息为:" + e.getMessage());
}
return result;
}
/**
* 获取hash数据
* @param key
* @param value
* @return
*/
public Object hget(String hkey,String key){
Object result = null;
try {
HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
Boolean aBoolean = hashOperations.hasKey(hkey, key);
if (!aBoolean) {
return null;
}
Object response = hashOperations.get(hkey, key);
result = response;
} catch (Exception e) {
log.error("写入redis缓存失败!错误信息为:" + e.getMessage());
}
return result;
}
}