此文僅爲初學java的同窗學習,大佬請勿噴,文末我會附上完整代碼包供你們參考
redis的搭建教程此處略過,你們自行百度,本文的教程開始:
1、先在pom.xml中添加相關依賴
<!--redis的依賴 start-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<!--redis的依賴 end-->
2、在application.yml中加入相關的配置
spring:
#redis配置(host和端口改爲你本身的)
redis:
host: 10.24.19.237
port: 6379
# 密碼,沒有就不配置
password: 123456
# 鏈接超時時間(毫秒)
timeout: 36000
# Redis默認狀況下有16個分片,這裏配置具體使用的分片,默認是0
database: 0
lettuce:
pool:
# 鏈接池最大鏈接數(使用負值表示沒有限制)默認 8
max-active: 8
# 鏈接池最大阻塞等待時間(使用負值表示沒有限制) 默認 -1
max-wait: -1ms
# 鏈接池中的最大空閒鏈接 默認 8
max-idle: 8
# 鏈接池中的最小空閒鏈接 默認 0
min-idle: 0
3、添加一個RedisTemplate的工具類RedisUtils.java方便咱們使用
package com.example.study.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* RedisTemplate 工具類
*
* @author 154594742@qq.com
* @date 2021/3/1 20:23
*/
@Component
public class RedisUtils {
@Autowired
private RedisTemplate redisTemplate;
//- - - - - - - - - - - - - - - - - - - - - 通用方法 - - - - - - - - - - - - - - - - - - - -
/**
* 給一個指定的 key 值附加過時時間
*
* @param key
* @param time
* @return
*/
public boolean expire(String key, long time) {
return redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
/**
* 根據key進行刪除
*
* @param key
* @return
*/
public boolean delete(String key) {
return redisTemplate.delete(key);
}
/**
* 根據批量key進行批量刪除
*
* @param keys
* @return 執行成功的數量
*/
public Long delete(Collection<String> keys) {
return redisTemplate.delete(keys);
}
/**
* 根據key 獲取過時時間
*
* @param key
* @return
*/
public long getTime(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 根據key 獲取過時時間
*
* @param key
* @return
*/
public boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
/**
* 移除指定key 的過時時間
*
* @param key
* @return
*/
public boolean persist(String key) {
return redisTemplate.boundValueOps(key).persist();
}
//- - - - - - - - - - - - - - - - - - - - - String類型 - - - - - - - - - - - - - - - - - - - -
/**
* 根據key獲取值
*
* @param key 鍵
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 將值放入緩存
*
* @param key 鍵
* @param value 值
* @return true成功 false 失敗
*/
public void set(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 將值放入緩存並設置時間
*
* @param key 鍵
* @param value 值
* @param time 時間(秒) 小於等於0爲無期限
* @return true成功 false 失敗
*/
public void set(String key, String value, long time) {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
this.set(key, value);
}
}
/**
* 批量添加 key (重複的鍵會覆蓋)
*
* @param keyAndValue
*/
public void batchSet(Map<String, String> keyAndValue) {
redisTemplate.opsForValue().multiSet(keyAndValue);
}
/**
* 批量添加 key-value 只有在鍵不存在時,才添加
* map 中只要有一個key存在,則所有不添加
*
* @param keyAndValue
*/
public void batchSetIfAbsent(Map<String, String> keyAndValue) {
redisTemplate.opsForValue().multiSetIfAbsent(keyAndValue);
}
/**
* 對一個 key-value 的值進行加減操做,
* 若是該 key 不存在 將建立一個key 並賦值該 number
* 若是 key 存在,但 value 不是長整型 ,將報錯
*
* @param key
* @param number
*/
public Long increment(String key, long number) {
return redisTemplate.opsForValue().increment(key, number);
}
/**
* 對一個 key-value 的值進行加減操做,
* 若是該 key 不存在 將建立一個key 並賦值該 number
* 若是 key 存在,但 value 不是 純數字 ,將報錯
*
* @param key
* @param number
*/
public Double increment(String key, double number) {
return redisTemplate.opsForValue().increment(key, number);
}
//- - - - - - - - - - - - - - - - - - - - - set類型 - - - - - - - - - - - - - - - - - - - -
/**
* 將數據放入set緩存
*
* @param key 鍵
* @return
*/
public void sSet(String key, String value) {
redisTemplate.opsForSet().add(key, value);
}
/**
* 獲取變量中的值
*
* @param key 鍵
* @return
*/
public Set<Object> members(String key) {
return redisTemplate.opsForSet().members(key);
}
/**
* 隨機獲取變量中指定個數的元素
*
* @param key 鍵
* @param count 值
* @return
*/
public void randomMembers(String key, long count) {
redisTemplate.opsForSet().randomMembers(key, count);
}
/**
* 隨機獲取變量中的元素
*
* @param key 鍵
* @return
*/
public Object randomMember(String key) {
return redisTemplate.opsForSet().randomMember(key);
}
/**
* 彈出變量中的元素
*
* @param key 鍵
* @return
*/
public Object pop(String key) {
return redisTemplate.opsForSet().pop(key);
}
/**
* 獲取變量中值的長度
*
* @param key 鍵
* @return
*/
public long size(String key) {
return redisTemplate.opsForSet().size(key);
}
/**
* 根據value從一個set中查詢,是否存在
*
* @param key 鍵
* @param value 值
* @return true 存在 false不存在
*/
public boolean sHasKey(String key, Object value) {
return redisTemplate.opsForSet().isMember(key, value);
}
/**
* 檢查給定的元素是否在變量中。
*
* @param key 鍵
* @param obj 元素對象
* @return
*/
public boolean isMember(String key, Object obj) {
return redisTemplate.opsForSet().isMember(key, obj);
}
/**
* 轉移變量的元素值到目的變量。
*
* @param key 鍵
* @param value 元素對象
* @param destKey 元素對象
* @return
*/
public boolean move(String key, String value, String destKey) {
return redisTemplate.opsForSet().move(key, value, destKey);
}
/**
* 批量移除set緩存中元素
*
* @param key 鍵
* @param values 值
* @return
*/
public void remove(String key, Object... values) {
redisTemplate.opsForSet().remove(key, values);
}
/**
* 經過給定的key求2個set變量的差值
*
* @param key 鍵
* @param destKey 鍵
* @return
*/
public Set<Set> difference(String key, String destKey) {
return redisTemplate.opsForSet().difference(key, destKey);
}
//- - - - - - - - - - - - - - - - - - - - - hash類型 - - - - - - - - - - - - - - - - - - - -
/**
* 加入緩存
*
* @param key 鍵
* @param map 鍵
* @return
*/
public void add(String key, Map<String, String> map) {
redisTemplate.opsForHash().putAll(key, map);
}
/**
* 獲取 key 下的 全部 hashkey 和 value
*
* @param key 鍵
* @return
*/
public Map<Object, Object> getHashEntries(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* 驗證指定 key 下 有沒有指定的 hashkey
*
* @param key
* @param hashKey
* @return
*/
public boolean hashKey(String key, String hashKey) {
return redisTemplate.opsForHash().hasKey(key, hashKey);
}
/**
* 獲取指定key的值string
*
* @param key 鍵
* @param key2 鍵
* @return
*/
public String getMapString(String key, String key2) {
return redisTemplate.opsForHash().get(key, key2).toString();
}
/**
* 獲取指定的值Int
*
* @param key 鍵
* @param key2 鍵
* @return
*/
public Integer getMapInt(String key, String key2) {
return (Integer) redisTemplate.opsForHash().get(key, key2);
}
/**
* 彈出元素並刪除
*
* @param key 鍵
* @return
*/
public String popValue(String key) {
return redisTemplate.opsForSet().pop(key).toString();
}
/**
* 刪除指定 hash 的 HashKey
*
* @param key
* @param hashKeys
* @return 刪除成功的 數量
*/
public Long delete(String key, String... hashKeys) {
return redisTemplate.opsForHash().delete(key, hashKeys);
}
/**
* 給指定 hash 的 hashkey 作增減操做
*
* @param key
* @param hashKey
* @param number
* @return
*/
public Long increment(String key, String hashKey, long number) {
return redisTemplate.opsForHash().increment(key, hashKey, number);
}
/**
* 給指定 hash 的 hashkey 作增減操做
*
* @param key
* @param hashKey
* @param number
* @return
*/
public Double increment(String key, String hashKey, Double number) {
return redisTemplate.opsForHash().increment(key, hashKey, number);
}
/**
* 獲取 key 下的 全部 hashkey 字段
*
* @param key
* @return
*/
public Set<Object> hashKeys(String key) {
return redisTemplate.opsForHash().keys(key);
}
/**
* 獲取指定 hash 下面的 鍵值對 數量
*
* @param key
* @return
*/
public Long hashSize(String key) {
return redisTemplate.opsForHash().size(key);
}
//- - - - - - - - - - - - - - - - - - - - - list類型 - - - - - - - - - - - - - - - - - - - -
/**
* 在變量左邊添加元素值
*
* @param key
* @param value
* @return
*/
public void leftPush(String key, Object value) {
redisTemplate.opsForList().leftPush(key, value);
}
/**
* 獲取集合指定位置的值。
*
* @param key
* @param index
* @return
*/
public Object index(String key, long index) {
return redisTemplate.opsForList().index(key, index);
}
/**
* 獲取指定區間的值。
*
* @param key
* @param start
* @param end
* @return
*/
public List<Object> range(String key, long start, long end) {
return redisTemplate.opsForList().range(key, start, end);
}
/**
* 把最後一個參數值放到指定集合的第一個出現中間參數的前面,
* 若是中間參數值存在的話。
*
* @param key
* @param pivot
* @param value
* @return
*/
public void leftPush(String key, String pivot, String value) {
redisTemplate.opsForList().leftPush(key, pivot, value);
}
/**
* 向左邊批量添加參數元素。
*
* @param key
* @param values
* @return
*/
public void leftPushAll(String key, String... values) {
redisTemplate.opsForList().leftPushAll(key, values);
}
/**
* 向集合最右邊添加元素。
*
* @param key
* @param value
* @return
*/
public void leftPushAll(String key, String value) {
redisTemplate.opsForList().rightPush(key, value);
}
/**
* 向左邊批量添加參數元素。
*
* @param key
* @param values
* @return
*/
public void rightPushAll(String key, String... values) {
redisTemplate.opsForList().rightPushAll(key, values);
}
/**
* 向已存在的集合中添加元素。
*
* @param key
* @param value
* @return
*/
public void rightPushIfPresent(String key, Object value) {
redisTemplate.opsForList().rightPushIfPresent(key, value);
}
/**
* 向已存在的集合中添加元素。
*
* @param key
* @return
*/
public long listLength(String key) {
return redisTemplate.opsForList().size(key);
}
/**
* 移除集合中的左邊第一個元素。
*
* @param key
* @return
*/
public void leftPop(String key) {
redisTemplate.opsForList().leftPop(key);
}
/**
* 移除集合中左邊的元素在等待的時間裏,若是超過等待的時間仍沒有元素則退出。
*
* @param key
* @return
*/
public void leftPop(String key, long timeout, TimeUnit unit) {
redisTemplate.opsForList().leftPop(key, timeout, unit);
}
/**
* 移除集合中右邊的元素。
*
* @param key
* @return
*/
public void rightPop(String key) {
redisTemplate.opsForList().rightPop(key);
}
/**
* 移除集合中右邊的元素在等待的時間裏,若是超過等待的時間仍沒有元素則退出。
*
* @param key
* @return
*/
public void rightPop(String key, long timeout, TimeUnit unit) {
redisTemplate.opsForList().rightPop(key, timeout, unit);
}
}
4、添加RedisController.java類來測試一下
package com.example.study.controller;
import com.example.study.model.vo.ResponseVo;
import com.example.study.util.BuildResponseUtils;
import com.example.study.util.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* redis控制器
*
* @author 154594742@qq.com
* @date 2021/3/1 19:50
*/
@RequestMapping("/redis")
@RestController
@Api(tags = "Redis控制器")
public class RedisController {
@Autowired
private RedisUtils redisUtils;
/**
* 設置緩存
*
* @param key key
* @param value value
* @return ResponseVo
*/
@ApiOperation("設置緩存")
@PostMapping("add")
public ResponseVo<?> add(String key, String value) {
redisUtils.set(key, value, 600);
return BuildResponseUtils.success();
}
/**
* 經過key查詢
*
* @param key key
* @return ResponseVo
*/
@ApiOperation("經過key查詢")
@GetMapping("{key}")
public ResponseVo<Object> getByKey(@PathVariable String key) {
String value = (String) redisUtils.get(key);
return BuildResponseUtils.buildResponse(value);
}
/**
* 經過key刪除
*
* @param key key
* @return ResponseVo
*/
@ApiOperation("經過key刪除")
@DeleteMapping("{key}")
public ResponseVo<?> delete(@PathVariable String key) {
return redisUtils.delete(key) ? BuildResponseUtils.success() : BuildResponseUtils.error();
}
}
設置緩存:
![](http://static.javashuo.com/static/loading.gif)
獲取緩存:
![](http://static.javashuo.com/static/loading.gif)
6、附上完整代碼包供你們學習參考,若是對你有幫助,請給個關注或者點個贊吧! 點擊下載完整代碼包