java org.springframework.boot 對redis操作方法
java org.springframework.boot 對redis操作
在Spring Boot項目中操作Redis,你可以使用Spring Data Redis。Spring Data Redis是Spring提供的一個用于簡化Redis數(shù)據(jù)訪問的模塊,它提供了一個易于使用的編程模型來與Redis交互。
1. 添加依賴
首先,你需要在你的pom.xml
文件中添加Spring Boot的Redis starter依賴:
<dependencies> <!-- Spring Boot Redis Starter --> <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> </dependencies>
2. 配置Redis
在application.properties
或application.yml
中配置Redis的連接信息:
# application.properties 示例 spring.redis.host=localhost spring.redis.port=6379 spring.redis.password=yourpassword # 如果你的Redis設(shè)置了密碼
或者使用YAML格式:
# application.yml 示例 spring: redis: host: localhost port: 6379 password: yourpassword # 如果你的Redis設(shè)置了密碼
3. 使用RedisTemplate操作Redis
RedisTemplate
是Spring Data Redis中用于操作Redis的一個模板類,它提供了豐富的API來操作Redis。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; @Service public class RedisService { @Autowired private StringRedisTemplate stringRedisTemplate; // 用于操作String類型的數(shù)據(jù) @Autowired private RedisTemplate<Object, Object> redisTemplate; // 用于操作Object類型的數(shù)據(jù),可以存儲任何類型的數(shù)據(jù),需要手動轉(zhuǎn)換類型等操作。 public void setKeyValue(String key, String value) { ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue(); opsForValue.set(key, value); } public String getKeyValue(String key) { return stringRedisTemplate.opsForValue().get(key); } }
增刪改查操作
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; @Service public class RedisService { @Autowired private RedisTemplate<String, Object> redisTemplate; // 存儲數(shù)據(jù) public void set(String key, Object value) { redisTemplate.opsForValue().set(key, value); } // 存儲數(shù)據(jù)并設(shè)置過期時間 public void setWithExpire(String key, Object value, long timeout, TimeUnit unit) { redisTemplate.opsForValue().set(key, value, timeout, unit); } // 獲取數(shù)據(jù) public Object get(String key) { return redisTemplate.opsForValue().get(key); } // 刪除數(shù)據(jù) public void delete(String key) { redisTemplate.delete(key); } }
創(chuàng)建臨時數(shù)據(jù)并自動刪除,通過設(shè)置過期時間,可以創(chuàng)建臨時數(shù)據(jù)。當數(shù)據(jù)過期時,Redis 會自動刪除它們。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; @Service public class RedisService { @Autowired private RedisTemplate<String, Object> redisTemplate; // 創(chuàng)建臨時數(shù)據(jù)并設(shè)置過期時間 public void createTemporaryData(String key, Object value, long timeout, TimeUnit unit) { redisTemplate.opsForValue().set(key, value, timeout, unit); } }
例如
redisTemplate.opsForValue().set("exampleKey", "exampleValue", 10, TimeUnit.SECONDS);
在這個示例中,“exampleKey"將被設(shè)置為"exampleValue”,并且這個鍵值對將在10秒后過期。
4. 使用Jackson2JsonRedisSerializer(可選)自定義序列化方式(存儲對象)
如果你需要存儲Java對象而不是簡單的字符串,你可以使用Jackson2JsonRedisSerializer
來序列化和反序列化對象。
import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(com.fasterxml.jackson.annotation.PropertyAccessor.ALL, com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY); // 所有字段全部可見,包括私有字段等。根據(jù)實際情況調(diào)整。 serializer.setObjectMapper(om); // 設(shè)置序列化工具類。也可以自定義序列化工具類。例如:new CustomObjectMapper()。 具體根據(jù)需要選擇合適的序列化工具類。例如:new CustomObjectMapper()。具體根據(jù)需要選擇合適的序列化工具類。例如:new CustomObjectMapper()。具體根據(jù)需要選擇合適的序列化工具類。例如:new CustomObjectMapper()。具體根據(jù)需要選擇合適的序列化工具類。例如:new CustomObjectMapper()。具體根據(jù)需要選擇合適的序列化工具類。例如:new CustomObjectMapper()。具體根據(jù)需要選擇合適的序列化工具類。例如:new CustomObjectMapper()。具體根據(jù)需要選擇合適的序列化工具類
到此這篇關(guān)于java org.springframework.boot 對redis操作方法的文章就介紹到這了,更多相關(guān)java org.springframework.boot redis操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java:無法訪問org.springframework.boot.SpringApplication
- java:無法訪問org.springframework.boot.SpringApplication問題
- Java報錯:Error:java:?程序包org.springframework.boot不存在解決辦法
- java:程序包org.springframework.boot不存在的完美解決方法
- java:無法訪問org.springframework.boot.SpringApplication的解決方法
- Java操作redis設(shè)置第二天凌晨過期的解決方案
- 詳解java操作Redis數(shù)據(jù)庫的redis工具(RedisUtil,jedis工具JedisUtil,JedisPoolUtil)
- IDEA版使用Java操作Redis數(shù)據(jù)庫的方法
相關(guān)文章
java?JVM方法分派模型靜態(tài)分派動態(tài)分派全面講解
這篇文章主要為大家介紹了java?JVM方法分派模型靜態(tài)分派動態(tài)分派全面講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06Java二維數(shù)組實現(xiàn)數(shù)字拼圖效果
這篇文章主要為大家詳細介紹了Java二維數(shù)組實現(xiàn)數(shù)字拼圖效果,控制臺可以對空格進行移動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07如何通過java將doc文件轉(zhuǎn)換為docx文件詳解
在數(shù)字化時代文檔處理成為了我們?nèi)粘9ぷ骱蛯W習中不可或缺的一部分,其中doc和docx作為兩種常見的文檔格式,各自具有不同的特點和優(yōu)勢,這篇文章主要給大家介紹了關(guān)于如何通過java將doc文件轉(zhuǎn)換為docx文件的相關(guān)資料,需要的朋友可以參考下2024-07-07Java LinkedList的實現(xiàn)原理圖文詳解
今天小編就為大家分享一篇關(guān)于Java LinkedList的實現(xiàn)原理圖文詳解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01SpringBoot整合Sharding-JDBC實現(xiàn)MySQL8讀寫分離
本文是一個基于SpringBoot整合Sharding-JDBC實現(xiàn)讀寫分離的極簡教程,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的可以了解一下2021-07-07