springboot中操作redis實例分享
更新時間:2023年06月19日 08:55:58 作者:minqiliang
本文介紹了如何在Spring?Boot應用中整合Redis緩存技術,包括配置Redis連接、定義Redis模板、實現(xiàn)Redis的基本操作以及使用Spring?Cache注解。這些內(nèi)容可幫助開發(fā)者快速掌握Spring?Boot與Redis的集成,并提高應用性能。
1.maven引入相關依賴
<dependencies>
<!-- spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.11.1</version>
</dependency>
<!--jackjson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--junit-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.配置redis
application.yml
spring:
# 配置redis
redis:
host: 192.168.***.***
port: 6379
password: ******
lettuce:
pool:
max-active: 8 # 連接池最大連接數(shù)(使用負值表示沒有限制)
max-idle: 8 # 連接池中的最大空閑連接
max-wait: 100 # 連接池最大阻塞等待時間(使用負值表示沒有限制)
min-idle: 0 # 連接池中的最小空閑連接
database: 0 # redis數(shù)據(jù)庫索引(默認為0)3.配置序列化
package com.example.config;
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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
/**
* Redis 序列化方式配置
*
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
// 創(chuàng)建RedisTemplate<String, Object>對象
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 配置連接工廠
template.setConnectionFactory(factory);
// json方式序列化對象
GenericJackson2JsonRedisSerializer jsonRedisSerializer =
new GenericJackson2JsonRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(RedisSerializer.string());
// hash的key也采用String的序列化方式
template.setHashKeySerializer(RedisSerializer.string());
// value序列化方式采用jackson
template.setValueSerializer(jsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jsonRedisSerializer);
return template;
}
}
4.測試類中進行測試
package com.example;
import com.example.entity.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
@SpringBootTest
class SpringDataRedisTestApplicationTests {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
private static final ObjectMapper objectMapper = new ObjectMapper();
/**
* 測試redis的String類型
*/
@Test
void testString() {
redisTemplate.opsForValue().set("name", "minqiliang");
System.out.println(redisTemplate.opsForValue().get("name"));
}
/**
* 使用StringRedisTemplate操作redis(需要手動進行序列化和反序列化)
* @throws JsonProcessingException
*/
@Test
void testString2() throws JsonProcessingException {
// 創(chuàng)建一個對象
User user = new User("001", "minqiliang", 18);
// 由于StringRedisTemplate默認使用的是String的序列化器,所以這里需要將對象轉換成json字符串
String json = objectMapper.writeValueAsString(user);
// 存入redis
stringRedisTemplate.opsForValue().set("user:001", json);
// 從redis中取出數(shù)據(jù)
String s = stringRedisTemplate.opsForValue().get("user:001");
// 將json字符串轉換成對象
User u = objectMapper.readValue(s, User.class);
System.out.println(u);
}
@Test
void testHash() {
// 存入redis
redisTemplate.opsForHash().put("user:002", "id", "002");
redisTemplate.opsForHash().put("user:002", "name", "張三");
redisTemplate.opsForHash().put("user:002", "age", 18);
// 獲取對應的key的所有值
System.out.println(redisTemplate.opsForHash().entries("user:002"));
System.out.println("====================================");
// 獲取對應的key的某個值
System.out.println(redisTemplate.opsForHash().get("user:002", "id"));
System.out.println(redisTemplate.opsForHash().get("user:002", "name"));
System.out.println(redisTemplate.opsForHash().get("user:002", "age"));
}
}到此這篇關于springboot中操作redis實例分享的文章就介紹到這了,更多相關springboot中操作redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- springboot2整合redis使用lettuce連接池的方法(解決lettuce連接池無效問題)
- 詳解springboot配置多個redis連接
- Springboot2.X集成redis集群(Lettuce)連接的方法
- springboot整合redis進行數(shù)據(jù)操作(推薦)
- springboot整合spring-data-redis遇到的坑
- SpringBoot集成Redisson實現(xiàn)分布式鎖的方法示例
- SpringBoot Redis配置Fastjson進行序列化和反序列化實現(xiàn)
- SpringBoot項目中使用redis緩存的方法步驟
- springboot與redis的簡單整合實例
- SpringBoot通過RedisTemplate執(zhí)行Lua腳本的方法步驟
相關文章
異步redis隊列實現(xiàn) 數(shù)據(jù)入庫的方法
今天小編就為大家分享一篇異步redis隊列實現(xiàn) 數(shù)據(jù)入庫的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
一文搞懂阿里云服務器部署Redis并整合Spring?Boot
這篇文章主要介紹了一文搞懂阿里云服務器部署Redis并整合Spring?Boot,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09
RedisDesktopManager遠程連接redis的實現(xiàn)
本文主要介紹了RedisDesktopManager遠程連接redis的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05

