spring boot結(jié)合Redis實現(xiàn)工具類的方法示例
自己整理了 spring boot 結(jié)合 Redis 的工具類
引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
加入配置
# Redis數(shù)據(jù)庫索引(默認為0) spring.redis.database=0 # Redis服務(wù)器地址 spring.redis.host=localhost # Redis服務(wù)器連接端口 spring.redis.port=6379
實現(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;
// 維護一個本類的靜態(tài)變量
private static CacheUtils cacheUtils;
@PostConstruct
public void init() {
cacheUtils = this;
cacheUtils.redisTemplate = this.redisTemplate;
}
/**
* 將參數(shù)中的字符串值設(shè)置為鍵的值,不設(shè)置過期時間
* @param key
* @param value 必須要實現(xiàn) Serializable 接口
*/
public static void set(String key, String value) {
cacheUtils.redisTemplate.opsForValue().set(key, value);
}
/**
* 將參數(shù)中的字符串值設(shè)置為鍵的值,設(shè)置過期時間
* @param key
* @param value 必須要實現(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);
}
/**
* 將給定分數(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);
}
/**
* 返回指定排序集中給定成員的分數(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解SpringBoot中添加@ResponseBody注解會發(fā)生什么
這篇文章主要介紹了詳解SpringBoot中添加@ResponseBody注解會發(fā)生什么,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11
SpringBoot自定義注解之實現(xiàn)AOP切面日志詳解
這篇文章主要為大家詳細介紹了SpringBoot自定義注解之實現(xiàn)AOP切面統(tǒng)一打印出入?yún)⑷罩?,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-09-09
Spring?Cloud?Gateway編碼實現(xiàn)任意地址跳轉(zhuǎn)
這篇文章主要介紹了Spring?Cloud?Gateway編碼實現(xiàn)任意地址跳轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下2023-06-06
java.lang.UnsupportedClassVersionError異常正確解決方法
java.lang.UnsupportedClassVersionError異常通常發(fā)生在嘗試在較低版本的Java虛擬機上運行使用更高版本的Jav 編譯器編譯的類文件時,下面就來介紹一下解決方法,感興趣的可以了解一下2024-05-05

