欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot中操作redis實例分享

 更新時間:2023年06月19日 08:55:58   作者:minqiliang  
本文介紹了如何在Spring?Boot應用中整合Redis緩存技術,包括配置Redis連接、定義Redis模板、實現(xiàn)Redis的基本操作以及使用Spring?Cache注解。這些內容可幫助開發(fā)者快速掌握Spring?Boot與Redis的集成,并提高應用性能。

1.maven引入相關依賴

<dependencies>
 	<!-- spring-boot-starter-data-redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!-- commons-pool2 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.11.1</version>
        </dependency>
        <!--jackjson-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--junit-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
</dependencies>

2.配置redis

application.yml

spring:
  # 配置redis
  redis:
    host: 192.168.***.***
    port: 6379
    password: ******
    lettuce:
      pool:
        max-active: 8 # 連接池最大連接數(使用負值表示沒有限制)
        max-idle: 8 # 連接池中的最大空閑連接
        max-wait: 100 # 連接池最大阻塞等待時間(使用負值表示沒有限制)
        min-idle: 0 # 連接池中的最小空閑連接
    database: 0 # redis數據庫索引(默認為0)

3.配置序列化

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * Redis  序列化方式配置
 *
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        // 創(chuàng)建RedisTemplate<String, Object>對象
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 配置連接工廠
        template.setConnectionFactory(factory);
        // json方式序列化對象
        GenericJackson2JsonRedisSerializer jsonRedisSerializer =
                new GenericJackson2JsonRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(RedisSerializer.string());
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(RedisSerializer.string());
        // value序列化方式采用jackson
        template.setValueSerializer(jsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jsonRedisSerializer);
        return template;
    }

}

4.測試類中進行測試

package com.example;

import com.example.entity.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
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.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

@SpringBootTest
class SpringDataRedisTestApplicationTests {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 測試redis的String類型
     */
    @Test
    void testString() {
        redisTemplate.opsForValue().set("name", "minqiliang");
        System.out.println(redisTemplate.opsForValue().get("name"));
    }

    /**
     * 使用StringRedisTemplate操作redis(需要手動進行序列化和反序列化)
     * @throws JsonProcessingException
     */
    @Test
    void testString2() throws JsonProcessingException {
        // 創(chuàng)建一個對象
        User user = new User("001", "minqiliang", 18);
        // 由于StringRedisTemplate默認使用的是String的序列化器,所以這里需要將對象轉換成json字符串
        String json = objectMapper.writeValueAsString(user);
        // 存入redis
        stringRedisTemplate.opsForValue().set("user:001", json);
        // 從redis中取出數據
        String s = stringRedisTemplate.opsForValue().get("user:001");
        // 將json字符串轉換成對象
        User u = objectMapper.readValue(s, User.class);
        System.out.println(u);
    }

    @Test
    void testHash() {
        // 存入redis
        redisTemplate.opsForHash().put("user:002", "id", "002");
        redisTemplate.opsForHash().put("user:002", "name", "張三");
        redisTemplate.opsForHash().put("user:002", "age", 18);
        // 獲取對應的key的所有值
        System.out.println(redisTemplate.opsForHash().entries("user:002"));
        System.out.println("====================================");
        // 獲取對應的key的某個值
        System.out.println(redisTemplate.opsForHash().get("user:002", "id"));
        System.out.println(redisTemplate.opsForHash().get("user:002", "name"));
        System.out.println(redisTemplate.opsForHash().get("user:002", "age"));
    }
}

到此這篇關于springboot中操作redis實例分享的文章就介紹到這了,更多相關springboot中操作redis內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Redis 數據遷移的項目實踐

    Redis 數據遷移的項目實踐

    本文主要介紹了Redis 數據遷移的項目實踐,通過Redis-shake的sync(同步)模式,可以將Redis的數據實時遷移至另一套Redis環(huán)境,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • 異步redis隊列實現(xiàn) 數據入庫的方法

    異步redis隊列實現(xiàn) 數據入庫的方法

    今天小編就為大家分享一篇異步redis隊列實現(xiàn) 數據入庫的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • Redis中的延遲雙刪

    Redis中的延遲雙刪

    這篇文章主要介紹了Redis中的延遲雙刪問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Redis和Lua實現(xiàn)分布式限流器的方法詳解

    Redis和Lua實現(xiàn)分布式限流器的方法詳解

    這篇文章主要給大家介紹了關于Redis和Lua實現(xiàn)分布式限流器的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Redis和Lua具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-06-06
  • 一文搞懂阿里云服務器部署Redis并整合Spring?Boot

    一文搞懂阿里云服務器部署Redis并整合Spring?Boot

    這篇文章主要介紹了一文搞懂阿里云服務器部署Redis并整合Spring?Boot,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • Redis分布式鎖方案設計之防止訂單重復提交或支付

    Redis分布式鎖方案設計之防止訂單重復提交或支付

    這篇文章主要為大家介紹了Redis分布式鎖之防止訂單重復提交或支付方案設計示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • 基于Redis實現(xiàn)分布式鎖以及任務隊列

    基于Redis實現(xiàn)分布式鎖以及任務隊列

    這篇文章主要介紹了基于Redis實現(xiàn)分布式鎖以及任務隊列,需要的朋友可以參考下
    2015-11-11
  • Redis如何解決BigKey

    Redis如何解決BigKey

    在Redis的使用過程中,我們經常會遇到BigKey, BigKey的大值會導致Redis內存中產生大量不連續(xù)的碎片,降低內存利用效率,本文主要介紹了Redis如何解決BigKey,具有一定的參考價值,感興趣的可以了解一下
    2024-01-01
  • RedisDesktopManager遠程連接redis的實現(xiàn)

    RedisDesktopManager遠程連接redis的實現(xiàn)

    本文主要介紹了RedisDesktopManager遠程連接redis的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-05-05
  • 深入了解Redis的看門狗機制

    深入了解Redis的看門狗機制

    Redis鎖的延期機制,通常被稱為看門狗機制,本文就拉介紹一下Redis的看門狗機制,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-12-12

最新評論