最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java SpringBoot 集成 Redis詳解

 更新時間:2021年10月14日 09:31:49   作者:七元K  
Redis 是一個由 Salvatore Sanfilippo 寫的 key-value 存儲系統(tǒng),是跨平臺的非關系型數(shù)據(jù)庫。Redis 是一個開源的使用 ANSI C 語言編寫、遵守 BSD 協(xié)議、支持網(wǎng)絡、可基于內(nèi)存、分布式、可選持久性的鍵值對(Key-Value)存儲數(shù)據(jù)庫,并提供多種語言的 API

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能該干什么?

  1. 內(nèi)存存儲、持久化,內(nèi)存是斷電即失的,所以需要持久化(RDB、AOF)
  2. 高效率、用于高速緩沖
  3. 發(fā)布訂閱系統(tǒng)
  4. 地圖信息分析
  5. 計時器、計數(shù)器(eg:瀏覽量)
  6. … …

特性

  1. 多樣的數(shù)據(jù)類型
  2. 持久化
  3. 集群
  4. 事務

2、測試Redis

SpringBoot操作數(shù)據(jù),Spring-Data、 jbdc、redis… …

SpringData與SpringBoot齊名的項目!

說明:在SpringBoot2.x之后,原來使用的jedis被替換為lettuce

jedis:采用的直連,多個線程操作的話,是不安全的,如果想要避免不安全的,需使用jedis pool連接池!像BIO模式

lettuce:采用netty,實例可以再多個線程中進行共享,不存在線程不安全的情況!可以減少線程數(shù)據(jù),更像NIO模式

img

新建一個項目

img

img

注意:

img

查看底層

img

源碼分析:

@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"));

    }

}

img

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)序列化,它會報錯==

到此這篇關于Java SpringBoot 集成 Redis詳解的文章就介紹到這了,更多相關Java 集成 Redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

沙坪坝区| 井研县| 观塘区| 保定市| 西和县| 蒙山县| 莱西市| 图木舒克市| 彭泽县| 中牟县| 博湖县| 湘潭县| 康定县| 夹江县| 郁南县| 通榆县| 稷山县| 忻州市| 八宿县| 特克斯县| 观塘区| 南投市| 栾城县| 英超| 普安县| 巫溪县| 永春县| 旅游| 大埔县| 波密县| 乌鲁木齐县| 登封市| 且末县| 闽侯县| 吕梁市| 平昌县| 额济纳旗| 苍溪县| 杭锦后旗| 汉川市| 古蔺县|