spring boot結(jié)合Redis實(shí)現(xiàn)工具類(lèi)的方法示例
自己整理了 spring boot 結(jié)合 Redis 的工具類(lèi)
引入依賴(lài)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
加入配置
# Redis數(shù)據(jù)庫(kù)索引(默認(rèn)為0) spring.redis.database=0 # Redis服務(wù)器地址 spring.redis.host=localhost # Redis服務(wù)器連接端口 spring.redis.port=6379
實(shí)現(xiàn)代碼
這里用到了 靜態(tài)類(lèi)工具類(lèi)中 如何使用 @Autowired
package com.lmxdawn.api.common.utils;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
/**
* 緩存操作類(lèi)
*/
@Component
public class CacheUtils {
@Autowired
private RedisTemplate<String, String> redisTemplate;
// 維護(hù)一個(gè)本類(lèi)的靜態(tài)變量
private static CacheUtils cacheUtils;
@PostConstruct
public void init() {
cacheUtils = this;
cacheUtils.redisTemplate = this.redisTemplate;
}
/**
* 將參數(shù)中的字符串值設(shè)置為鍵的值,不設(shè)置過(guò)期時(shí)間
* @param key
* @param value 必須要實(shí)現(xiàn) Serializable 接口
*/
public static void set(String key, String value) {
cacheUtils.redisTemplate.opsForValue().set(key, value);
}
/**
* 將參數(shù)中的字符串值設(shè)置為鍵的值,設(shè)置過(guò)期時(shí)間
* @param key
* @param value 必須要實(shí)現(xiàn) Serializable 接口
* @param timeout
*/
public static void set(String key, String value, Long timeout) {
cacheUtils.redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
}
/**
* 獲取與指定鍵相關(guān)的值
* @param key
* @return
*/
public static Object get(String key) {
return cacheUtils.redisTemplate.opsForValue().get(key);
}
/**
* 設(shè)置某個(gè)鍵的過(guò)期時(shí)間
* @param key 鍵值
* @param ttl 過(guò)期秒數(shù)
*/
public static boolean expire(String key, Long ttl) {
return cacheUtils.redisTemplate.expire(key, ttl, TimeUnit.SECONDS);
}
/**
* 判斷某個(gè)鍵是否存在
* @param key 鍵值
*/
public static boolean hasKey(String key) {
return cacheUtils.redisTemplate.hasKey(key);
}
/**
* 向集合添加元素
* @param key
* @param value
* @return 返回值為設(shè)置成功的value數(shù)
*/
public static Long sAdd(String key, String... value) {
return cacheUtils.redisTemplate.opsForSet().add(key, value);
}
/**
* 獲取集合中的某個(gè)元素
* @param key
* @return 返回值為redis中鍵值為key的value的Set集合
*/
public static Set<String> sGetMembers(String key) {
return cacheUtils.redisTemplate.opsForSet().members(key);
}
/**
* 將給定分?jǐn)?shù)的指定成員添加到鍵中存儲(chǔ)的排序集合中
* @param key
* @param value
* @param score
* @return
*/
public static Boolean zAdd(String key, String value, double score) {
return cacheUtils.redisTemplate.opsForZSet().add(key, value, score);
}
/**
* 返回指定排序集中給定成員的分?jǐn)?shù)
* @param key
* @param value
* @return
*/
public static Double zScore(String key, String value) {
return cacheUtils.redisTemplate.opsForZSet().score(key, value);
}
/**
* 刪除指定的鍵
* @param key
* @return
*/
public static Boolean delete(String key) {
return cacheUtils.redisTemplate.delete(key);
}
/**
* 刪除多個(gè)鍵
* @param keys
* @return
*/
public static Long delete(Collection<String> keys) {
return cacheUtils.redisTemplate.delete(keys);
}
}
相關(guān)地址
GitHub 地址:https://github.com/lmxdawn/vue-admin-java
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解SpringBoot中添加@ResponseBody注解會(huì)發(fā)生什么
這篇文章主要介紹了詳解SpringBoot中添加@ResponseBody注解會(huì)發(fā)生什么,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
SpringBoot自定義注解之實(shí)現(xiàn)AOP切面日志詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot自定義注解之實(shí)現(xiàn)AOP切面統(tǒng)一打印出入?yún)⑷罩?,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
Java 無(wú)符號(hào)右移與右移運(yùn)算符的使用介紹
這篇文章主要介紹了Java 無(wú)符號(hào)右移與右移運(yùn)算符的使用介紹,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Spring?Cloud?Gateway編碼實(shí)現(xiàn)任意地址跳轉(zhuǎn)
這篇文章主要介紹了Spring?Cloud?Gateway編碼實(shí)現(xiàn)任意地址跳轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下2023-06-06
SpringBoot如何集成PageHelper分頁(yè)功能
這篇文章主要介紹了SpringBoot如何集成PageHelper分頁(yè)功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
2020最新eclipse安裝過(guò)程及細(xì)節(jié)
這篇文章主要介紹了2020最新eclipse安裝過(guò)程及細(xì)節(jié),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
java.lang.UnsupportedClassVersionError異常正確解決方法
java.lang.UnsupportedClassVersionError異常通常發(fā)生在嘗試在較低版本的Java虛擬機(jī)上運(yùn)行使用更高版本的Jav 編譯器編譯的類(lèi)文件時(shí),下面就來(lái)介紹一下解決方法,感興趣的可以了解一下2024-05-05

