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

使用SpringBoot中整合Redis

 更新時(shí)間:2022年06月07日 10:38:26   作者:Asurplus  
這篇文章主要介紹了使用SpringBoot中整合Redis,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

SpringBoot中整合Redis

本次,我們以IDEA + SpringBoot作為 Java中整合Redis的使用 的測(cè)試環(huán)境,如果對(duì)創(chuàng)建SpringBoot項(xiàng)目有不清楚的地方,可以參考這篇文章:使用Idea創(chuàng)建我的第一個(gè)SpringBoot項(xiàng)目

首先,我們需要導(dǎo)入Redis的maven依賴(lài)

<!-- Redis的maven依賴(lài)包 -->
?? ??? ?<dependency>
?? ??? ??? ?<groupId>org.springframework.boot</groupId>
?? ??? ??? ?<artifactId>spring-boot-starter-data-redis</artifactId>
?? ??? ?</dependency>

其次,我們需要在配置文件中配置你的Redis配置信息,我使用的是 .yml文件格式 

# redis配置
spring:
  redis:
    # r服務(wù)器地址
    host: 127.0.0.1
    # 服務(wù)器端口
    port: 6379
    # 數(shù)據(jù)庫(kù)索引(默認(rèn)0)
    database: 0
    jedis:
      pool:
        # 連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制)
        max-active: 50
        # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制)
        max-wait: 3000ms
        # 連接池中的最大空閑連接數(shù)
        max-idle: 20
        # 連接池中的最小空閑連接數(shù)
        min-idle: 2
    # 連接超時(shí)時(shí)間(毫秒)
    timeout: 5000ms

然后,我們需要?jiǎng)?chuàng)建一個(gè)RedisUtil來(lái)對(duì)Redis數(shù)據(jù)庫(kù)進(jìn)行操作

package com.zyxx.test.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @ClassName RedisUtil
 * @Author Lizhou
 * @Date 2019-08-03 17:29:29
 * @Version 1.0
 **/
@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate<String, String> template;

    /**
     * 讀取數(shù)據(jù)
     *
     * @param key
     * @return
     */
    public String get(final String key) {
        return template.opsForValue().get(key);
    }

    /**
     * 寫(xiě)入數(shù)據(jù)
     */
    public boolean set(final String key, String value) {
        boolean res = false;
        try {
            template.opsForValue().set(key, value);
            res = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 根據(jù)key更新數(shù)據(jù)
     */
    public boolean update(final String key, String value) {
        boolean res = false;
        try {
            template.opsForValue().getAndSet(key, value);
            res = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 根據(jù)key刪除數(shù)據(jù)
     */
    public boolean del(final String key) {
        boolean res = false;
        try {
            template.delete(key);
            res = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }
    
	/**
     * 是否存在key
     */
    public boolean hasKey(final String key) {
        boolean res = false;
        try {
            res = template.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

	/**
     * 給指定的key設(shè)置存活時(shí)間
     * 默認(rèn)為-1,表示永久不失效
     */
    public boolean setExpire(final String key, long seconds) {
        boolean res = false;
        try {
            if (0 < seconds) {
                res = template.expire(key, seconds, TimeUnit.SECONDS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 獲取指定key的剩余存活時(shí)間
     * 默認(rèn)為-1,表示永久不失效,-2表示該key不存在
     */
    public long getExpire(final String key) {
        long res = 0;
        try {
            res = template.getExpire(key, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

	/**
     * 移除指定key的有效時(shí)間
     * 當(dāng)key的有效時(shí)間為-1即永久不失效和當(dāng)key不存在時(shí)返回false,否則返回true
     */
    public boolean persist(final String key) {
        boolean res = false;
        try {
            res = template.persist(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }    
}

最后,我們可以使用單元測(cè)試來(lái)檢測(cè)我們?cè)赗edisUtil中寫(xiě)的操作Redis數(shù)據(jù)庫(kù)的方法

package com.zyxx.test;
import com.zyxx.test.utils.RedisUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestApplicationTest {

    @Resource
    private RedisUtil redisUtil;

    @Test
    public void setRedis() {
        boolean res = redisUtil.set("jay", "周杰倫 - 《以父之名》");
        System.out.println(res);
    }

    @Test
    public void getRedis() {
        String res = redisUtil.get("jay");
        System.out.println(res);
    }

    @Test
    public void updateRedis() {
        boolean res = redisUtil.update("jay", "周杰倫 - 《夜的第七章》");
        System.out.println(res);
    }

    @Test
    public void delRedis() {
        boolean res = redisUtil.del("jay");
        System.out.println(res);
    }

	@Test
    public void hasKey() {
        boolean res = redisUtil.hasKey("jay");
        System.out.println(res);
    }

	@Test
    public void expire() {
        boolean res = redisUtil.setExpire("jay", 100);
        System.out.println(res);
    }

    @Test
    public void getExpire() {
        long res = redisUtil.getExpire("jay");
        System.out.println(res);
    }

	@Test
    public void persist() {
        boolean res = redisUtil.persist("jay");
        System.out.println(res);
    }    
}

推薦使用Redis客戶(hù)端(redis-desktop-manager)來(lái)查看Redis數(shù)據(jù)庫(kù)中的數(shù)據(jù)

至此,我們?cè)谌粘m?xiàng)目中整合Redis的基本使用操作就完成了,但在實(shí)際項(xiàng)目中,可能會(huì)涉及到更復(fù)雜的用法,可以根據(jù)你的業(yè)務(wù)需求調(diào)整Redis的使用即可。

SpringBoot整合Redis改不了database問(wèn)題

關(guān)于學(xué)習(xí)redis寫(xiě)的yaml文件,里面的

redis:
? database: 15
? host: 127.0.0.1
? port: 6379
? password:
? timeout: 3000ms # 連接超時(shí)時(shí)間(毫秒)

但是springboot自動(dòng)裝配一直是database為0

最后發(fā)現(xiàn)是沒(méi)有找到我配置的redis,默認(rèn)是用的自動(dòng)裝配的默認(rèn)地址(所以我一直以為我的yaml寫(xiě)對(duì)了,但是沒(méi)用寫(xiě)對(duì),因?yàn)橐恢睕](méi)有取到我的值)

自動(dòng)裝配的就是

database=0,port=6379;無(wú)密碼,host=localhost,

就是因?yàn)檫@個(gè)原因,我寫(xiě)的數(shù)據(jù)能進(jìn)入我的本地redis中,所以我一直沒(méi)找到問(wèn)題,因?yàn)槲乙彩褂玫谋镜氐膔edis測(cè)試的,一直找不到為什么改不了database。

最后發(fā)現(xiàn)是我的yaml文件的redis寫(xiě)錯(cuò)了,把redis寫(xiě)到log下面,并且歸屬于log。

最后該成歸屬于spring下面就可以了。

注意:一定要注意yaml的書(shū)寫(xiě)規(guī)范和上下級(jí)的關(guān)系。

深刻的教訓(xùn),我在網(wǎng)上查了很多,也沒(méi)找到對(duì)應(yīng)的辦法,幸好沒(méi)有,不然的話(huà),我的代碼會(huì)被改的亂七八糟的,花了一下午才發(fā)現(xiàn)是自己yaml寫(xiě)錯(cuò)了。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • IDEA之如何關(guān)閉/開(kāi)啟引用提示Usages

    IDEA之如何關(guān)閉/開(kāi)啟引用提示Usages

    這篇文章主要介紹了IDEA之如何關(guān)閉/開(kāi)啟引用提示Usages問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • WebUploader客戶(hù)端批量上傳圖片 后臺(tái)使用springMVC

    WebUploader客戶(hù)端批量上傳圖片 后臺(tái)使用springMVC

    這篇文章主要為大家詳細(xì)介紹了WebUploader客戶(hù)端批量上傳圖片,后臺(tái)使用springMVC接收實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • springcloud微服務(wù)之Eureka配置詳解

    springcloud微服務(wù)之Eureka配置詳解

    這篇文章主要介紹了springcloud微服務(wù)之Eureka配置詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • 實(shí)例分析java對(duì)象的序列化和反序列化

    實(shí)例分析java對(duì)象的序列化和反序列化

    序列化 (Serialization)是將對(duì)象的狀態(tài)信息轉(zhuǎn)換為可以存儲(chǔ)或傳輸?shù)男问降倪^(guò)程。一般將一個(gè)對(duì)象存儲(chǔ)至一個(gè)儲(chǔ)存媒介,例如檔案或是記億體緩沖等。在網(wǎng)絡(luò)傳輸過(guò)程中,可以是字節(jié)或是XML等格式。而字節(jié)的或XML編碼格式可以還原完全相等的對(duì)象。這個(gè)相反的過(guò)程又稱(chēng)為反序列化
    2018-09-09
  • Java 對(duì)HashMap進(jìn)行排序的三種常見(jiàn)方法

    Java 對(duì)HashMap進(jìn)行排序的三種常見(jiàn)方法

    這篇文章主要介紹了Java 對(duì)HashMap進(jìn)行排序的三種常見(jiàn)方法,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-10-10
  • 解析Linux系統(tǒng)中JVM內(nèi)存2GB上限的詳解

    解析Linux系統(tǒng)中JVM內(nèi)存2GB上限的詳解

    本篇文章是對(duì)Linux系統(tǒng)中JVM內(nèi)存2GB上限進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • Spring boot項(xiàng)目整合WebSocket方法

    Spring boot項(xiàng)目整合WebSocket方法

    這篇文章主要介紹了WebSocket使用Spring boot整合方法,需要繼承webSocketHandler類(lèi),重寫(xiě)幾個(gè)方法就可以了,具體實(shí)例代碼跟隨小編一起看看吧
    2021-09-09
  • Java框架Struts2實(shí)現(xiàn)圖片上傳功能

    Java框架Struts2實(shí)現(xiàn)圖片上傳功能

    這篇文章主要為大家詳細(xì)介紹了Java框架Struts2實(shí)現(xiàn)圖片上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • spring整合redis消息監(jiān)聽(tīng)通知使用的實(shí)現(xiàn)示例

    spring整合redis消息監(jiān)聽(tīng)通知使用的實(shí)現(xiàn)示例

    在電商系統(tǒng)中,秒殺,搶購(gòu),紅包優(yōu)惠卷等操作,一般都會(huì)設(shè)置時(shí)間限制,本文主要介紹了spring整合redis消息監(jiān)聽(tīng)通知使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-12-12
  • java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):單,雙向鏈表

    java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):單,雙向鏈表

    這篇文章主要介紹了Java的數(shù)據(jù)解構(gòu)基礎(chǔ),希望對(duì)廣大的程序愛(ài)好者有所幫助,同時(shí)祝大家有一個(gè)好成績(jī),需要的朋友可以參考下,希望能給你帶來(lái)幫助
    2021-07-07

最新評(píng)論

枝江市| 沙湾县| 镇雄县| 安顺市| 石门县| 巴林左旗| 措勤县| 普格县| 大港区| 江源县| 高碑店市| 许昌县| 肥乡县| 南雄市| 银川市| 渭源县| 石林| 云浮市| 永和县| 和田市| 成都市| 赤壁市| 蒲城县| 思南县| 瑞金市| 宝鸡市| 高安市| 三江| 屏东县| 舟山市| 荣昌县| 南汇区| 叙永县| 环江| 星子县| 瓮安县| 玉屏| 武义县| 东阿县| 内黄县| 莆田市|