springboot中操作redis實(shí)例分享
1.maven引入相關(guān)依賴(lài)
<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ù)(使用負(fù)值表示沒(méi)有限制)
max-idle: 8 # 連接池中的最大空閑連接
max-wait: 100 # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制)
min-idle: 0 # 連接池中的最小空閑連接
database: 0 # redis數(shù)據(jù)庫(kù)索引(默認(rèn)為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>對(duì)象
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 配置連接工廠
template.setConnectionFactory(factory);
// json方式序列化對(duì)象
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.測(cè)試類(lèi)中進(jìn)行測(cè)試
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();
/**
* 測(cè)試redis的String類(lèi)型
*/
@Test
void testString() {
redisTemplate.opsForValue().set("name", "minqiliang");
System.out.println(redisTemplate.opsForValue().get("name"));
}
/**
* 使用StringRedisTemplate操作redis(需要手動(dòng)進(jìn)行序列化和反序列化)
* @throws JsonProcessingException
*/
@Test
void testString2() throws JsonProcessingException {
// 創(chuàng)建一個(gè)對(duì)象
User user = new User("001", "minqiliang", 18);
// 由于StringRedisTemplate默認(rèn)使用的是String的序列化器,所以這里需要將對(duì)象轉(zhuǎn)換成json字符串
String json = objectMapper.writeValueAsString(user);
// 存入redis
stringRedisTemplate.opsForValue().set("user:001", json);
// 從redis中取出數(shù)據(jù)
String s = stringRedisTemplate.opsForValue().get("user:001");
// 將json字符串轉(zhuǎn)換成對(duì)象
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);
// 獲取對(duì)應(yīng)的key的所有值
System.out.println(redisTemplate.opsForHash().entries("user:002"));
System.out.println("====================================");
// 獲取對(duì)應(yīng)的key的某個(gè)值
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"));
}
}到此這篇關(guān)于springboot中操作redis實(shí)例分享的文章就介紹到這了,更多相關(guān)springboot中操作redis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot2整合redis使用lettuce連接池的方法(解決lettuce連接池?zé)o效問(wèn)題)
- 詳解springboot配置多個(gè)redis連接
- Springboot2.X集成redis集群(Lettuce)連接的方法
- springboot整合redis進(jìn)行數(shù)據(jù)操作(推薦)
- springboot整合spring-data-redis遇到的坑
- SpringBoot集成Redisson實(shí)現(xiàn)分布式鎖的方法示例
- SpringBoot Redis配置Fastjson進(jìn)行序列化和反序列化實(shí)現(xiàn)
- SpringBoot項(xiàng)目中使用redis緩存的方法步驟
- springboot與redis的簡(jiǎn)單整合實(shí)例
- SpringBoot通過(guò)RedisTemplate執(zhí)行Lua腳本的方法步驟
相關(guān)文章
Redis 數(shù)據(jù)遷移的項(xiàng)目實(shí)踐
本文主要介紹了Redis 數(shù)據(jù)遷移的項(xiàng)目實(shí)踐,通過(guò)Redis-shake的sync(同步)模式,可以將Redis的數(shù)據(jù)實(shí)時(shí)遷移至另一套R(shí)edis環(huán)境,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
異步redis隊(duì)列實(shí)現(xiàn) 數(shù)據(jù)入庫(kù)的方法
今天小編就為大家分享一篇異步redis隊(duì)列實(shí)現(xiàn) 數(shù)據(jù)入庫(kù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
Redis和Lua實(shí)現(xiàn)分布式限流器的方法詳解
這篇文章主要給大家介紹了關(guān)于Redis和Lua實(shí)現(xiàn)分布式限流器的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Redis和Lua具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
一文搞懂阿里云服務(wù)器部署Redis并整合Spring?Boot
這篇文章主要介紹了一文搞懂阿里云服務(wù)器部署Redis并整合Spring?Boot,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
Redis分布式鎖方案設(shè)計(jì)之防止訂單重復(fù)提交或支付
這篇文章主要為大家介紹了Redis分布式鎖之防止訂單重復(fù)提交或支付方案設(shè)計(jì)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
基于Redis實(shí)現(xiàn)分布式鎖以及任務(wù)隊(duì)列
這篇文章主要介紹了基于Redis實(shí)現(xiàn)分布式鎖以及任務(wù)隊(duì)列,需要的朋友可以參考下2015-11-11
RedisDesktopManager遠(yuǎn)程連接redis的實(shí)現(xiàn)
本文主要介紹了RedisDesktopManager遠(yuǎn)程連接redis的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05

