這篇文章給大家分享的是有關SpringBoot如何集成Redis的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
Redis是什么?
Redis(Remote Dictionary Server ),即遠程字典服務。
是一個開源的使用ANSI C語言編寫、支持網絡、可基于內存亦可持久化的日志型、Key-Value數據庫,并提供多種語言的API。
與memcached一樣,為了保證效率,數據都是緩存在內存中。區別的是redis會周期性的把更新的數據寫入磁盤或者把修改操作寫入追加的記錄文件,并且在此基礎上實現了master-slave(主從)同步。
Redis能該干什么?
內存存儲、持久化,內存是斷電即失的,所以需要持久化(RDB、AOF)高效率、用于高速緩沖發布訂閱系統地圖信息分析計時器、計數器(eg:瀏覽量)… …
特性
多樣的數據類型
持久化
集群
事務
…
SpringBoot操作數據,Spring-Data、 jbdc、redis… …
SpringData與SpringBoot齊名的項目!
說明:在SpringBoot2.x之后,原來使用的jedis被替換為lettuce
jedis:采用的直連,多個線程操作的話,是不安全的,如果想要避免不安全的,需使用jedis pool連接池!像BIO模式
lettuce:采用netty,實例可以再多個線程中進行共享,不存在線程不安全的情況!可以減少線程數據,更像NIO模式

新建一個項目


注意:

查看底層

源碼分析:
@Bean
@ConditionalOnMissingBean( //如果未注入組件條件,我們自己可以定義一個redisTemplate來替換這個默認的
name = {"redisTemplate"}
)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
//默認的 RedisTemplate 沒有過多的設置 redis 都是需要序列化的 !
//兩個泛型都是 Object Object的類型,我們往后使用需要強制轉換<String,String>
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean //由于String 是redis 中最常用的類型 所有說單獨提出來一個bean!
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}1、導入依賴
2、配置連接
# SpringBoot 所有的配置類 都有一個自動配置類 RedisAutoConfiguration # 自動配置類都會綁定一個 properties 配置文件 RedisProperties #配置 redis spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis
3、測試!
package com.kk;
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.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
class Redis01SpringbootApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
/*
redisTemplate
opsForValue 操作字符串的 類似String
opsForList 操作List 類似List
opsForSet
opsForHash
opsForZSet
opsForGeo
opsForHyperLogLog
除了基本的操作 ,我們常用的方法都可以直接通過redisTemplate 比如事務和基本的CRUD
*/
//獲取redis的連接對象
// RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
// connection.flushDb();
// connection.flushAll();
redisTemplate.opsForValue().set("kk1","kk2");
System.out.println(redisTemplate.opsForValue().get("kk1"));
}
}
首先先建一個實體類,測試
User類
package com.kk.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
import java.io.Serializable;
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
//在企業中,我們所有的pojo都會序列化
public class User implements Serializable {
private String name;
private int age;
}測試:
@Test
public void test() throws JsonProcessingException {
//真實的開發一般都使用json來傳遞對象
User user = new User("kk", 17);
String jsonUser = new ObjectMapper().writeValueAsString(user);//這樣就變成了一個json對象了
redisTemplate.opsForValue().set("user",jsonUser);
System.out.println(redisTemplate.opsForValue().get("user"));
}r = new ObjectMapper().writeValueAsString(user);//這樣就變成了一個json對象了 redisTemplate.opsForValue().set(“user”,jsonUser); System.out.println(redisTemplate.opsForValue().get(“user”)); }
==注意:如果不在User類中實現序列化,它會報錯==
感謝各位的閱讀!關于“SpringBoot如何集成Redis”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。