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

新建一個(gè)項(xiàng)目


注意:

查看底層

源碼分析:
@Bean
@ConditionalOnMissingBean( //如果未注入組件條件,我們自己可以定義一個(gè)redisTemplate來(lái)替換這個(gè)默認(rèn)的
name = {"redisTemplate"}
)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
//默認(rèn)的 RedisTemplate 沒(méi)有過(guò)多的設(shè)置 redis 都是需要序列化的 !
//兩個(gè)泛型都是 Object Object的類(lèi)型,我們往后使用需要強(qiáng)制轉(zhuǎn)換<String,String>
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean //由于String 是redis 中最常用的類(lèi)型 所有說(shuō)單獨(dú)提出來(lái)一個(gè)bean!
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
1、導(dǎo)入依賴(lài)
2、配置連接
# SpringBoot 所有的配置類(lèi) 都有一個(gè)自動(dòng)配置類(lèi) RedisAutoConfiguration # 自動(dòng)配置類(lèi)都會(huì)綁定一個(gè) properties 配置文件 RedisProperties #配置 redis spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis
3、測(cè)試!
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 操作字符串的 類(lèi)似String
opsForList 操作List 類(lèi)似List
opsForSet
opsForHash
opsForZSet
opsForGeo
opsForHyperLogLog
除了基本的操作 ,我們常用的方法都可以直接通過(guò)redisTemplate 比如事務(wù)和基本的CRUD
*/
//獲取redis的連接對(duì)象
// RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
// connection.flushDb();
// connection.flushAll();
redisTemplate.opsForValue().set("kk1","kk2");
System.out.println(redisTemplate.opsForValue().get("kk1"));
}
}

3、自定義redisTemplate
首先先建一個(gè)實(shí)體類(lèi),測(cè)試
User類(lèi)
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都會(huì)序列化
public class User implements Serializable {
private String name;
private int age;
}
測(cè)試:
@Test
public void test() throws JsonProcessingException {
//真實(shí)的開(kāi)發(fā)一般都使用json來(lái)傳遞對(duì)象
User user = new User("kk", 17);
String jsonUser = new ObjectMapper().writeValueAsString(user);//這樣就變成了一個(gè)json對(duì)象了
redisTemplate.opsForValue().set("user",jsonUser);
System.out.println(redisTemplate.opsForValue().get("user"));
}
r = new ObjectMapper().writeValueAsString(user);//這樣就變成了一個(gè)json對(duì)象了 redisTemplate.opsForValue().set(“user”,jsonUser); System.out.println(redisTemplate.opsForValue().get(“user”)); }
==注意:如果不在User類(lèi)中實(shí)現(xiàn)序列化,它會(huì)報(bào)錯(cuò)==
到此這篇關(guān)于SpringBoot集成Redis的文章就介紹到這了,更多相關(guān)SpringBoot集成Redis內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信