spring boot結(jié)合Redis實(shí)現(xiàn)工具類的方法示例
自己整理了 spring boot 結(jié)合 Redis 的工具類
引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
加入配置
# Redis數(shù)據(jù)庫索引(默認(rèn)為0) spring.redis.database=0 # Redis服務(wù)器地址 spring.redis.host=localhost # Redis服務(wù)器連接端口 spring.redis.port=6379
實(shí)現(xiàn)代碼
這里用到了 靜態(tà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;
/**
* 緩存操作類
*/
@Component
public class CacheUtils {
@Autowired
private RedisTemplate<String, String> redisTemplate;
// 維護(hù)一個本類的靜態(tài)變量
private static CacheUtils cacheUtils;
@PostConstruct
public void init() {
cacheUtils = this;
cacheUtils.redisTemplate = this.redisTemplate;
}
/**
* 將參數(shù)中的字符串值設(shè)置為鍵的值,不設(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è)置過期時間
* @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è)置某個鍵的過期時間
* @param key 鍵值
* @param ttl 過期秒數(shù)
*/
public static boolean expire(String key, Long ttl) {
return cacheUtils.redisTemplate.expire(key, ttl, TimeUnit.SECONDS);
}
/**
* 判斷某個鍵是否存在
* @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);
}
/**
* 獲取集合中的某個元素
* @param key
* @return 返回值為redis中鍵值為key的value的Set集合
*/
public static Set<String> sGetMembers(String key) {
return cacheUtils.redisTemplate.opsForSet().members(key);
}
/**
* 將給定分?jǐn)?shù)的指定成員添加到鍵中存儲的排序集合中
* @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);
}
/**
* 刪除多個鍵
* @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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用springboot 獲取控制器參數(shù)的幾種方法小結(jié)
這篇文章主要介紹了使用springboot 獲取控制器參數(shù)的幾種方法小結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
WeakHashMap?和?HashMap?區(qū)別及使用場景
這篇文章主要為大家介紹了WeakHashMap?和?HashMap?的區(qū)別是什么以及何時使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Java編寫簡單計(jì)算器的完整實(shí)現(xiàn)過程
這篇文章主要給大家介紹了關(guān)于Java編寫簡單計(jì)算器的完整實(shí)現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
mybatis+springboot發(fā)布postgresql數(shù)據(jù)的實(shí)現(xiàn)
本文主要介紹了mybatis+springboot發(fā)布postgresql數(shù)據(jù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
gradle配置國內(nèi)鏡像的實(shí)現(xiàn)
這篇文章主要介紹了gradle配置國內(nèi)鏡像的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Java中forEach使用lambda表達(dá)式,數(shù)組和集合的區(qū)別說明
這篇文章主要介紹了Java中forEach使用lambda表達(dá)式,數(shù)組和集合的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Java將json字符串轉(zhuǎn)換為數(shù)組的幾種方法
在Java開發(fā)中,經(jīng)常會遇到將json字符串轉(zhuǎn)換為數(shù)組的需求,本文主要介紹了Java將json字符串轉(zhuǎn)換為數(shù)組的幾種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
Java中實(shí)現(xiàn)String字符串用逗號隔開
這篇文章主要介紹了Java中實(shí)現(xiàn)String字符串用逗號隔開,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06

