Java SpringBoot 集成 Redis詳解
1、概述
Redis是什么?
Redis(Remote Dictionary Server ),即遠程字典服務(wù)。
是一個開源的使用ANSI C語言編寫、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,并提供多種語言的API。
與memcached一樣,為了保證效率,數(shù)據(jù)都是緩存在內(nèi)存中。區(qū)別的是redis會周期性的把更新的數(shù)據(jù)寫入磁盤或者把修改操作寫入追加的記錄文件,并且在此基礎(chǔ)上實現(xiàn)了master-slave(主從)同步。
Redis能該干什么?
- 內(nèi)存存儲、持久化,內(nèi)存是斷電即失的,所以需要持久化(RDB、AOF)
- 高效率、用于高速緩沖
- 發(fā)布訂閱系統(tǒng)
- 地圖信息分析
- 計時器、計數(shù)器(eg:瀏覽量)
- … …
特性
- 多樣的數(shù)據(jù)類型
- 持久化
- 集群
- 事務(wù)
…
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 沒有過多的設(shè)置 redis 都是需要序列化的 ! //兩個泛型都是 Object Object的類型,我們往后使用需要強制轉(zhuǎn)換<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、導(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
首先先建一個實體類,測試
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)序列化,它會報錯==
到此這篇關(guān)于Java SpringBoot 集成 Redis詳解的文章就介紹到這了,更多相關(guān)Java 集成 Redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java性能優(yōu)化之數(shù)據(jù)結(jié)構(gòu)實例代碼
這篇文章主要介紹了Java性能優(yōu)化之數(shù)據(jù)結(jié)構(gòu)實例代碼,具有一定借鑒價值,需要的朋友可以參考下2018-01-01SpringBoot中集成企業(yè)微信機器人實現(xiàn)運維報警的示例
本文主要介紹了SpringBoot中集成企業(yè)微信機器人實現(xiàn)運維報警,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05java如何獲取request中json數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于java如何獲取request中json數(shù)據(jù)的相關(guān)資料,文中通過代碼示例以及圖文將獲取的方法介紹的非常詳細,對大家學(xué)習(xí)或者使用java具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08