主頁 > 資料庫 > springboot中操作redis

springboot中操作redis

2023-06-19 08:25:16 資料庫

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) {
        // 創建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 {
        // 創建一個物件
        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"));
    }
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/555524.html

標籤:NoSQL

上一篇:深入淺出MySQL - 架構與執行

下一篇:返回列表

標籤雲
其他(161254) Python(38240) JavaScript(25505) Java(18246) C(15237) 區塊鏈(8271) C#(7972) AI(7469) 爪哇(7425) MySQL(7258) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5875) 数组(5741) R(5409) Linux(5347) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4603) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2436) ASP.NET(2404) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) .NET技术(1984) HtmlCss(1968) 功能(1967) Web開發(1951) C++(1941) python-3.x(1918) 弹簧靴(1913) xml(1889) PostgreSQL(1881) .NETCore(1863) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • GPU虛擬機創建時間深度優化

    **?桔妹導讀:**GPU虛擬機實體創建速度慢是公有云面臨的普遍問題,由于通常情況下創建虛擬機屬于低頻操作而未引起業界的重視,實際生產中還是存在對GPU實體創建時間有苛刻要求的業務場景。本文將介紹滴滴云在解決該問題時的思路、方法、并展示最終的優化成果。 從公有云服務商那里購買過虛擬主機的資深用戶,一 ......

    uj5u.com 2020-09-10 06:09:13 more
  • 可編程網卡芯片在滴滴云網路的應用實踐

    **?桔妹導讀:**隨著云規模不斷擴大以及業務層面對延遲、帶寬的要求越來越高,采用DPDK 加速網路報文處理的方式在橫向縱向擴展都出現了局限性。可編程芯片成為業界熱點。本文主要講述了可編程網卡芯片在滴滴云網路中的應用實踐,遇到的問題、帶來的收益以及開源社區貢獻。 #1. 資料中心面臨的問題 隨著滴滴 ......

    uj5u.com 2020-09-10 06:10:21 more
  • 滴滴資料通道服務演進之路

    **?桔妹導讀:**滴滴資料通道引擎承載著全公司的資料同步,為下游實時和離線場景提供了必不可少的源資料。隨著任務量的不斷增加,資料通道的整體架構也隨之發生改變。本文介紹了滴滴資料通道的發展歷程,遇到的問題以及今后的規劃。 #1. 背景 資料,對于任何一家互聯網公司來說都是非常重要的資產,公司的大資料 ......

    uj5u.com 2020-09-10 06:11:05 more
  • 滴滴AI Labs斬獲國際機器翻譯大賽中譯英方向世界第三

    **桔妹導讀:**深耕人工智能領域,致力于探索AI讓出行更美好的滴滴AI Labs再次斬獲國際大獎,這次獲獎的專案是什么呢?一起來看看詳細報道吧! 近日,由國際計算語言學協會ACL(The Association for Computational Linguistics)舉辦的世界最具影響力的機器 ......

    uj5u.com 2020-09-10 06:11:29 more
  • MPP (Massively Parallel Processing)大規模并行處理

    1、什么是mpp? MPP (Massively Parallel Processing),即大規模并行處理,在資料庫非共享集群中,每個節點都有獨立的磁盤存盤系統和記憶體系統,業務資料根據資料庫模型和應用特點劃分到各個節點上,每臺資料節點通過專用網路或者商業通用網路互相連接,彼此協同計算,作為整體提供 ......

    uj5u.com 2020-09-10 06:11:41 more
  • 滴滴資料倉庫指標體系建設實踐

    **桔妹導讀:**指標體系是什么?如何使用OSM模型和AARRR模型搭建指標體系?如何統一流程、規范化、工具化管理指標體系?本文會對建設的方法論結合滴滴資料指標體系建設實踐進行解答分析。 #1. 什么是指標體系 ##1.1 指標體系定義 指標體系是將零散單點的具有相互聯系的指標,系統化的組織起來,通 ......

    uj5u.com 2020-09-10 06:12:52 more
  • 單表千萬行資料庫 LIKE 搜索優化手記

    我們經常在資料庫中使用 LIKE 運算子來完成對資料的模糊搜索,LIKE 運算子用于在 WHERE 子句中搜索列中的指定模式。 如果需要查找客戶表中所有姓氏是“張”的資料,可以使用下面的 SQL 陳述句: SELECT * FROM Customer WHERE Name LIKE '張%' 如果需要 ......

    uj5u.com 2020-09-10 06:13:25 more
  • 滴滴Ceph分布式存盤系統優化之鎖優化

    **桔妹導讀:**Ceph是國際知名的開源分布式存盤系統,在工業界和學術界都有著重要的影響。Ceph的架構和演算法設計發表在國際系統領域頂級會議OSDI、SOSP、SC等上。Ceph社區得到Red Hat、SUSE、Intel等大公司的大力支持。Ceph是國際云計算領域應用最廣泛的開源分布式存盤系統, ......

    uj5u.com 2020-09-10 06:14:51 more
  • es~通過ElasticsearchTemplate進行聚合~嵌套聚合

    之前寫過《es~通過ElasticsearchTemplate進行聚合操作》的文章,這一次主要寫一個嵌套的聚合,例如先對sex集合,再對desc聚合,最后再對age求和,共三層嵌套。 Aggregations的部分特性類似于SQL語言中的group by,avg,sum等函式,Aggregation ......

    uj5u.com 2020-09-10 06:14:59 more
  • 爬蟲日志監控 -- Elastc Stack(ELK)部署

    傻瓜式部署,只需替換IP與用戶 導讀: 現ELK四大組件分別為:Elasticsearch(核心)、logstash(處理)、filebeat(采集)、kibana(可視化) 下載均在https://www.elastic.co/cn/downloads/下tar包,各組件版本最好一致,配合fdm會 ......

    uj5u.com 2020-09-10 06:15:05 more
最新发布
  • springboot中操作redis

    ## 1.maven引入相關依賴 ~~~xml org.springframework.boot spring-boot-starter-data-redis org.apache.commons commons-pool2 2.11.1 com.fasterxml.jackson.core jac ......

    uj5u.com 2023-06-19 08:25:16 more
  • 深入淺出MySQL - 架構與執行

    MySQL作為一個流行的開源關系型資料庫管理系統,它可以運行在多種平臺上,支持多種存盤引擎,提供了靈活的資料操作和管理功能。 ......

    uj5u.com 2023-06-19 08:24:54 more
  • 【后端面經】MySQL主鍵、唯一索引、聯合索引的區別和作用

    [TOC](MySQL主鍵、唯一索引、聯合索引的區別和作用) # 0. 簡介 索引是一類特殊的`檔案`,用來存盤檢索資訊,使資料庫查找更加快速。 # 1. 主鍵 主鍵是一類特殊的唯一索引,選擇某一列元素作為主鍵,用來表示每一行元素的特殊性,其特點如下 - 在一個資料表中只有一個主鍵; - 主鍵不能為 ......

    uj5u.com 2023-06-19 08:24:43 more
  • 極限科技旗下軟體產品 INFINI Easysearch 通過統信 UOS 認證

    近日,極限資料 (北京) 科技有限公司(以下簡稱:極限科技)旗下的軟體 INFINI Easysearch 搜索引擎軟體 V1.0 通過統信 UOS 服務器作業系統 V20 認證。 此次兼容適配基于統信 UOS 服務器作業系統 V20,聯合國產 CPU:海光 5000、海光 7000、兆芯 KH-3 ......

    uj5u.com 2023-06-19 08:24:23 more
  • 大資料SQL資料傾斜與資料膨脹的優化與經驗總結

    本文主要基于團隊實際開發經驗與積累,并結合了業界對大資料SQL的使用與優化,嘗試給出相對系統性的解決方案。 ......

    uj5u.com 2023-06-19 08:24:12 more
  • 使用 Easysearch 還原 Elasticsearch 快照資料

    本文主要驗證 Elasticsearch 快照在 [Easysearch](http://www.infinilabs.com/docs/latest/easysearch/overview) 中進行資料恢復。 ## 準備測驗資料 ### 索引 ![](https://www.infinilabs. ......

    uj5u.com 2023-06-18 08:08:32 more
  • 使用 Easysearch 還原 Elasticsearch 快照資料

    本文主要驗證 Elasticsearch 快照在 [Easysearch](http://www.infinilabs.com/docs/latest/easysearch/overview) 中進行資料恢復。 ## 準備測驗資料 ### 索引 ![](https://www.infinilabs. ......

    uj5u.com 2023-06-18 08:07:05 more
  • 曝光!Apache SeaTunnel Catalog 功能設計為何能大大簡化用戶啟用

    ![file](https://img2023.cnblogs.com/other/3195851/202306/3195851-20230616175541667-1729991882.jpg) Catalog(目錄)提供了關于資料庫、表格和訪問資料所需的資訊的元資料,以及統一的 API 來管理元 ......

    uj5u.com 2023-06-17 08:15:34 more
  • Committer 迎新!這次是來自阿里云的同學

    ![file](https://img2023.cnblogs.com/other/2685289/202306/2685289-20230616173226789-2009691483.jpg) > 截至今天,Apache DolphinScheduler 專案在 GitHub 上的 Star 數 ......

    uj5u.com 2023-06-17 08:15:25 more
  • 和必應對話之mysql磁區分表

    > mysql單表資料量太大 導致查詢緩慢問題如何解決 您好,這是Bing。根據網路搜索結果,mysql單表資料量太大導致查詢緩慢的問題有以下幾種可能的解決方案: - 使用索引。索引是一種資料結構,可以幫助資料庫高效地獲取資料。索引可以根據WHERE和JOIN中出現的列創建,以提高查詢效率。索引的種 ......

    uj5u.com 2023-06-17 08:15:21 more