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

新建一個項(xiàng)目


注意:

查看底層

源碼分析:
@Bean
@ConditionalOnMissingBean( //如果未注入組件條件,我們自己可以定義一個redisTemplate來替換這個默認(rèn)的
name = {"redisTemplate"}
)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
//默認(rèn)的 RedisTemplate 沒有過多的設(shè)置 redis 都是需要序列化的 !
//兩個泛型都是 Object Object的類型,我們往后使用需要強(qiáng)制轉(zhuǎn)換<String,String>
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean //由于String 是redis 中最常用的類型 所有說單獨(dú)提出來一個bean!
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
1、導(dǎo)入依賴
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 比如事務(wù)和基本的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
首先先建一個實(shí)體類,測試
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 {
//真實(shí)的開發(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類中實(shí)現(xiàn)序列化,它會報(bào)錯==
到此這篇關(guān)于Java SpringBoot 集成 Redis詳解的文章就介紹到這了,更多相關(guān)Java 集成 Redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一篇文章帶你了解java Object根類中關(guān)于toString,equals的方法
這篇文章主要介紹了Object類toString()和equals()方法使用解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-09-09
SpringBoot緩存預(yù)熱實(shí)戰(zhàn)
緩存預(yù)熱是一種常見的優(yōu)化策略,旨在提高系統(tǒng)的響應(yīng)速度和性能,本文主要介紹了SpringBoot緩存預(yù)熱實(shí)戰(zhàn)指南,具有一定的參考價(jià)值,感興趣的可以了解一下2025-01-01
Java中使用LocalDate根據(jù)日期來計(jì)算年齡的實(shí)現(xiàn)方法
這篇文章主要介紹了Java中使用LocalDate根據(jù)日期來計(jì)算年齡的實(shí)現(xiàn)方法,需要的朋友可以參考下2018-01-01
Springboot?中的?Filter?實(shí)現(xiàn)超大響應(yīng)?JSON?數(shù)據(jù)壓縮的方法
這篇文章主要介紹了Springboot?中的?Filter?實(shí)現(xiàn)超大響應(yīng)?JSON?數(shù)據(jù)壓縮,定義GzipFilter對輸出進(jìn)行攔截,定義 Controller該 Controller 非常簡單,主要讀取一個大文本文件,作為輸出的內(nèi)容,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10
SpringBoot操作spark處理hdfs文件的操作方法
本文介紹了如何使用Spring Boot操作Spark處理HDFS文件,包括導(dǎo)入依賴、配置Spark信息、編寫Controller和Service處理地鐵數(shù)據(jù)、運(yùn)行項(xiàng)目以及觀察Spark和HDFS的狀態(tài),感興趣的朋友跟隨小編一起看看吧2025-01-01
Java中5種方式實(shí)現(xiàn)String反轉(zhuǎn)
下面小編就為大家?guī)硪黄狫ava中5種方式實(shí)現(xiàn)String反轉(zhuǎn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。2016-06-06
SpringBoot根據(jù)各地區(qū)時(shí)間設(shè)置接口有效時(shí)間的實(shí)現(xiàn)方式
這篇文章給大家介紹了SpringBoot根據(jù)各地區(qū)時(shí)間設(shè)置接口有效時(shí)間的實(shí)現(xiàn)方式,文中通過代碼示例給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01

