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

Spring中RedisTemplate的基本使用淺析

 更新時間:2023年02月21日 16:39:19   作者:尚少  
Spring Boot Data(數(shù)據(jù)) Redis中提供了RedisTemplate和StringRedisTemplate,其中StringRedisTemplate是RedisTemplate的子類,兩個方法基本一致。本文介紹了Spring操作Redis的方法,需要的可以參考一下

spring-data-redis項目

  spring-data-redis提供了在Spring應(yīng)用中通過簡單的配置訪問redis服務(wù),封裝了 RedisTemplate 對象來對Redis進行各種操作、異常處理及序列化,支持發(fā)布訂閱。RedisTemplate對應(yīng)于Redis五大數(shù)據(jù)類型的api:

Api返回值類型說明
redisTemplate.opsForValue()ValueOperations操作 String 類型數(shù)據(jù)
redisTemplate.opsForHash()HashOperations操作 Hash 類型數(shù)據(jù)
redisTemplate.opsForList()ListOperations操作 List 類型數(shù)據(jù)
redisTemplate.opsForSet()SetOperations操作 Set 類型數(shù)據(jù)
redisTemplate.opsForZSet()ZSetOperations操作 SortedSet 類型數(shù)據(jù)

使用步驟

  前提條件:運行著的Redis(有windows版本)

  引入依賴:

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-redis</artifactId>
  <version>2.7.3</version>
</dependency>
<!--SpringBoot項目,可以引入這個依賴,這個依賴也是會依賴spring-data-redis的-->
<!--<dependency>-->
<!--    <groupId>org.springframework.boot</groupId>-->
<!--    <artifactId>spring-boot-starter-data-redis</artifactId>-->
<!--    <version>2.7.4</version>-->
<!--</dependency>-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.7.4</version>
</dependency>

  application.yml配置Redis的信息:

spring:
  redis:
    host: 127.0.0.1
    port: 6379
    # 沒有密碼可以注釋掉
    password: 123456
    database: 0
    lettuce:
      pool:
        # 最大連接
        max-active: 8
        # 最大空閑連接
        max-idle: 8
        # 最小空閑連接
        min-idle: 0
        # 連接等待時間
        max-wait: 100ms

  demo使用:

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.DefaultTypedTuple;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@SpringBootTest
public class RedisDemoApplicationTest {
    // 注入RedisTemplate
    @Autowired
    private RedisTemplate redisTemplate;
    // String類型
    @Test
    void testString () {
        redisTemplate.opsForValue().set("name", "javaCoder");
        Object name = redisTemplate.opsForValue().get("name");
        System.out.println(name);
    }
    // Hash類型
    @Test
    public void testHash () {
        redisTemplate.opsForHash().put("hash", "name", "abc");
        redisTemplate.opsForHash().put("hash", "age", 18);
        Map map = redisTemplate.opsForHash().entries("hash");
        System.out.println(map);
    }
    // List類型
    @Test
    public void testList () {
        redisTemplate.opsForList().leftPushAll("list", "zhangsan", "li", 
        "wanger");
        List<String> names = redisTemplate.opsForList().range("list", 0, 
        -1);
        System.out.println(names);
    }
    // Set類型
    @Test
    public void testSet () {
        redisTemplate.opsForSet().add("set", "cat", "dog", "wolf", "pig", 
        "sheep");
        Set<String> set = redisTemplate.opsForSet().members("set");
        System.out.println(set);
    }
    // SortedSet類型
    @Test
    public void testSortedSet () {
        redisTemplate.opsForZSet().add("zset", "cat", 30);
        redisTemplate.opsForZSet().add("zset", "dog", 20);
        redisTemplate.opsForZSet().add("zset", "wolf", 80);
        redisTemplate.opsForZSet().add("zset", "pig", 40);
        Set<String> aClass = redisTemplate.opsForZSet().range("zset", 
        0, -1);
        System.out.println(aClass);
        //使用下面這套寫法,也行
        //Set<ZSetOperations.TypedTuple<String>> set = new HashSet<>();
        //set.add(new DefaultTypedTuple<>("cat", 30.0));
        //set.add(new DefaultTypedTuple<>("dog", 20.0));
        //set.add(new DefaultTypedTuple<>("wolf", 80.0));
        //set.add(new DefaultTypedTuple<>("pig", 40.0));
        //redisTemplate.opsForZSet().add("zset", set);
        //Set<String> aClass1 = redisTemplate.opsForZSet().range("zset", 
        //0, -1);
        //System.out.println(aClass1);
    }
}

  此時代碼運行起來沒問題,但用redis-cli客戶端,用相關(guān)命令去查看時,發(fā)現(xiàn)數(shù)據(jù)不存在(get name、hgetall hash、lrange list 0 -1、smembers set、zrange zset 0 -1),用如下解決方法:

  a.設(shè)置RedisTemplate對象的key的序列化方式

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.RedisSerializer;
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(
    RedisConnectionFactory connectionFactory) {
        // 創(chuàng)建 RedisTemplate 對象
        RedisTemplate<String, Object> redisTemplate = 
        new RedisTemplate<>();
        // 設(shè)置連接工廠
        redisTemplate.setConnectionFactory(connectionFactory);
        // 設(shè)置Key的序列化 - String 序列化 RedisSerializer.string() => 
        // StringRedisSerializer.UTF_8
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        // 返回
        return redisTemplate;
    }
}

  b.注入的RestTemplate對象,指定泛型類型

@Autowired
private RedisTemplate<String, String> redisTemplate;

  不過方法a又有問題啦,去看String類型的key:name的值,

  長長的一串,占用空間大,可讀性差。這是因為value的序列化方式默認是JdkSerializationRedisSerializer,把它改成json。在上方的RedisConfig類中,添加代碼:

redisTemplate.setValueSerializer(RedisSerializer.json());
// 針對于hash類型的value
redisTemplate.setHashValueSerializer(RedisSerializer.json());

  如果你的redisTemplate類型確定就是RedisTemplate<String, String>,那也可以用StringRedisTemplate,兩者效果一樣。

到此這篇關(guān)于Spring中RedisTemplate的基本使用淺析的文章就介紹到這了,更多相關(guān)Spring RedisTemplate內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 深入理解 CAS 算法原理已經(jīng)在jdk中的運用

    深入理解 CAS 算法原理已經(jīng)在jdk中的運用

    這篇文章主要介紹了深入理解 CAS 算法原理已經(jīng)在jdk中的運用,幫助大家更好的使用Java,感興趣的朋友可以了解下
    2020-12-12
  • Intellij IDEA菜單欄不見了(Main Menu as Separate Toolbar)恢復(fù)菜單欄顯示的解決辦法

    Intellij IDEA菜單欄不見了(Main Menu as Separat

    有人問博主,關(guān)于Intellij IDEA菜單欄找不到了,被不小心的操作給隱藏了,怎么辦?下面給大家分享解決方案,感興趣的朋友跟隨小編一起看看吧
    2024-06-06
  • spring boot配置ssl(多cer格式)超詳細教程

    spring boot配置ssl(多cer格式)超詳細教程

    這篇文章主要介紹了spring boot配置ssl(多cer格式)超詳細教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-11-11
  • Springcloud基于OpenFeign實現(xiàn)服務(wù)調(diào)用代碼實例

    Springcloud基于OpenFeign實現(xiàn)服務(wù)調(diào)用代碼實例

    這篇文章主要介紹了Springcloud基于OpenFeign實現(xiàn)服務(wù)調(diào)用代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • 解決復(fù)制springboot項目后,啟動日志無顏色的問題

    解決復(fù)制springboot項目后,啟動日志無顏色的問題

    這篇文章主要介紹了解決復(fù)制springboot項目后,啟動日志無顏色的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringCloud中使用Sentinel實現(xiàn)限流的實戰(zhàn)

    SpringCloud中使用Sentinel實現(xiàn)限流的實戰(zhàn)

    限流在很多地方都可以使用的到,本篇博客將介紹如何使用SpringCloud中使用Sentinel實現(xiàn)限流,從而達到服務(wù)降級的目的,感興趣的可以了解一下
    2022-01-01
  • 淺談maven 多環(huán)境打包發(fā)布的兩種方式

    淺談maven 多環(huán)境打包發(fā)布的兩種方式

    這篇文章主要介紹了淺談maven 多環(huán)境打包發(fā)布的兩種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • 快速入手IntelliJ IDEA基本配置

    快速入手IntelliJ IDEA基本配置

    IntelliJ IDEA是java編程語言開發(fā)的集成環(huán)境,本篇主要介紹了對它的安裝、配置maven倉庫、調(diào)試方法、常用的插件推薦、快捷鍵大全與常用快捷鍵說明,感興趣的朋友一起看看吧
    2021-10-10
  • 使用springboot每日推送早安問候語到用戶微信的全過程

    使用springboot每日推送早安問候語到用戶微信的全過程

    近期網(wǎng)上又出現(xiàn)一股給女朋友做微信公眾號推送的潮流,所以這篇文章主要給大家介紹了關(guān)于如何使用springboot每日推送早安問候語到用戶微信的相關(guān)資料,文中通過圖文以及實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-11-11
  • SpringCloud整合OpenFeign問題

    SpringCloud整合OpenFeign問題

    這篇文章主要介紹了SpringCloud整合OpenFeign問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05

最新評論