前言:此整合爲非註解形式,使用工具類進行自主數據控制redis
專有名詞解析:Jedis是Redis官方推薦的面向Java的操做Redis的客戶端,而RedisTemplate是SpringDataRedis中對JedisApi的高度封裝spring
環境: IDEA版本2017.3.1 x64, JDK1.8, SpringBoot2.1.1數據庫
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
複製代碼
spring:
redis:
host: yourIpAddress
password: yourPassword
port: 6379
jedis:
pool:
#最大鏈接數據庫鏈接數,設 0 爲沒有限制
max-active: 8
#最大等待鏈接中的數量,設 0 爲沒有限制
max-idle: 8
#最大創建鏈接等待時間。若是超過此時間將接到異常。設爲-1表示無限制。
max-wait: -1ms
#最小等待鏈接中的數量,設 0 爲沒有限制
min-idle: 0
複製代碼
/**
* @create 2018-12-10 23:10
* redis工具類
*/
@Component
public final class RedisUtil {
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
/**
* 指定緩存失效時間
*
* @param key 鍵
* @param time 時間(秒)
* @return
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根據key 獲取過時時間
*
* @param key 鍵 不能爲null
* @return 時間(秒) 返回0表明爲永久有效
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 判斷key是否存在
*
* @param key 鍵
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 刪除緩存
*
* @param key 能夠傳一個值 或多個
*/
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
// ============================String=============================
/**
* 普通緩存獲取
*
* @param key 鍵
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通緩存放入
*
* @param key 鍵
* @param value 值
* @return true成功 false失敗
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 普通緩存放入並設置時間
*
* @param key 鍵
* @param value 值
* @param time 時間(秒) time要大於0 若是time小於等於0 將設置無限期
* @return true成功 false 失敗
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 遞增
*
* @param key 鍵
* @param delta 要增長几(大於0)
* @return
*/
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("遞增因子必須大於0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 遞減
*
* @param key 鍵
* @param delta 要減小几(小於0)
* @return
*/
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("遞減因子必須大於0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
}
複製代碼
@Autowired
RedisUtil redisUtil;
@Test
public void testRedis(){
Employee employee = employeeMapper.getEmpById(1);
//往redis中存入對象
redisUtil.set("emp",employee);
//從redis中取出對象
Employee emp = (Employee) redisUtil.get("emp");
System.out.println(emp);
}
複製代碼
查看緩存:緩存
更多Spring Boot整合可瀏覽此博客:malizhi.cnbash