SpringBoot集成Redis的思路詳解
SpringBoot集成Redis
1、概述
Redis是什么?
Redis(Remote Dictionary Server ),即遠程字典服務。
是一個開源的使用ANSI C語言編寫、支持網(wǎng)絡、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,并提供多種語言的API。
與memcached一樣,為了保證效率,數(shù)據(jù)都是緩存在內(nèi)存中。區(qū)別的是redis會周期性的把更新的數(shù)據(jù)寫入磁盤或者把修改操作寫入追加的記錄文件,并且在此基礎上實現(xiàn)了master-slave(主從)同步。
Redis能該干什么?
內(nèi)存存儲、持久化,內(nèi)存是斷電即失的,所以需要持久化(RDB、AOF)高效率、用于高速緩沖發(fā)布訂閱系統(tǒng)地圖信息分析計時器、計數(shù)器(eg:瀏覽量)… …
特性
多樣的數(shù)據(jù)類型
持久化
集群
事務
…
2、測試Redis
SpringBoot操作數(shù)據(jù),Spring-Data、 jbdc、redis… …
SpringData與SpringBoot齊名的項目!
說明:在SpringBoot2.x之后,原來使用的jedis被替換為lettuce
jedis:采用的直連,多個線程操作的話,是不安全的,如果想要避免不安全的,需使用jedis pool連接池!像BIO模式
lettuce:采用netty,實例可以再多個線程中進行共享,不存在線程不安全的情況!可以減少線程數(shù)據(jù),更像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"));
}
}

3、自定義redisTemplate
首先先建一個實體類,測試
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
//在企業(yè)中,我們所有的pojo都會序列化
public class User implements Serializable {
private String name;
private int age;
}
測試:
@Test
public void test() throws JsonProcessingException {
//真實的開發(fā)一般都使用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類中實現(xiàn)序列化,它會報錯==
到此這篇關于SpringBoot集成Redis的文章就介紹到這了,更多相關SpringBoot集成Redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用Redis命令操作數(shù)據(jù)庫的常見錯誤及解決方法
由于Redis是內(nèi)存數(shù)據(jù)庫,因此可能會存在一些安全問題,下面這篇文章主要給大家介紹了關于使用Redis命令操作數(shù)據(jù)庫的常見錯誤及解決方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-02-02
詳解如何發(fā)現(xiàn)并解決Redis熱點Key問題
Redis 熱點 Key 是指在某一時間段內(nèi),被大量的讀寫操作命中的 Key,這種情況可能會導致性能瓶頸,數(shù)據(jù)一致性問題,緩存擊穿等問題,所以本文給大家介紹了如何發(fā)現(xiàn)并解決Redis熱點Key問題,需要的朋友可以參考下2024-05-05
redis.clients.jedis.exceptions.JedisBusyException無法處理異常的解決方法
redis.clients.jedis.exceptions.JedisBusyException異常通常不是 Jedis客戶端直接拋出的標準異常,本文就來介紹一下異常的解決方法,感興趣的可以了解一下2024-05-05
簡介Lua腳本與Redis數(shù)據(jù)庫的結合使用
這篇文章主要介紹了簡介Lua腳本與Redis數(shù)據(jù)庫的結合使用,Redis是基于主存的高性能數(shù)據(jù)庫,需要的朋友可以參考下2015-06-06
nestjs使用redis實現(xiàn)ip限流的步驟詳解
如果使用nestjs開發(fā)接口并部署之后,我們通常需要考慮到接口是否會被惡意盜刷消耗過多的資源,一個簡單的方式就是限制在單位時間內(nèi)的訪問次數(shù),所以本文給大家介紹了nestjs使用redis實現(xiàn)ip限流的步驟,需要的朋友可以參考下2025-01-01
詳解redis腳本命令執(zhí)行問題(redis.call)
這篇文章主要介紹了redis腳本命令執(zhí)行問題(redis.call),分別介紹了redis-cli命令行中執(zhí)行及l(fā)inux命令行中執(zhí)行問題,本文給大家介紹的非常詳細,需要的朋友參考下吧2022-03-03

