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

SpringBoot整合Redis實(shí)現(xiàn)熱點(diǎn)數(shù)據(jù)緩存的示例代碼

 更新時(shí)間:2023年03月13日 14:43:28   作者:菜鳥是大神  
這篇文章主要介紹了SpringBoot中整合Redis實(shí)現(xiàn)熱點(diǎn)數(shù)據(jù)緩存,本文以IDEA?+?SpringBoot作為?Java中整合Redis的使用?的測(cè)試環(huán)境,結(jié)合實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下

我們以IDEA + SpringBoot作為 Java中整合Redis的使用 的測(cè)試環(huán)境

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

<!-- Redis的maven依賴包 -->
		<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
    # 連接超時(shí)時(shí)間(毫秒)
    timeout: 10s
    jedis:
      pool:
        # 連接池中的最大空閑連接數(shù)
        max-idle: 8
        # 連接池中的最小空閑連接數(shù)
        min-idle: 0
        # 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)
        max-active: 8
        # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制)
        max-wait: -1

對(duì) redis 做自定義配置

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
 
@Configuration
public class RedisConfigurer extends CachingConfigurerSupport {
 
    /**
     * redisTemplate 序列化使用的jdkSerializeable, 存儲(chǔ)二進(jìn)制字節(jié)碼, 所以自定義序列化類
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        // 配置redisTemplate
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        // 設(shè)置序列化
        Jackson2JsonRedisSerializer<Object> redisSerializer = getRedisSerializer();
        // key序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // value序列化
        redisTemplate.setValueSerializer(redisSerializer);
        // Hash key序列化
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        // Hash value序列化
        redisTemplate.setHashValueSerializer(redisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
 
    /**
     * 設(shè)置Jackson序列化
     */
    private Jackson2JsonRedisSerializer<Object> getRedisSerializer() {
        Jackson2JsonRedisSerializer<Object> redisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        redisSerializer.setObjectMapper(om);
        return redisSerializer;
    }
}

然后,我們需要?jiǎng)?chuàng)建一個(gè)RedisUtil來對(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 
 * @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);
    }
 
    /**
     * 寫入數(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è)試來檢測(cè)我們?cè)赗edisUtil中寫的操作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客戶端(redis-desktop-manager)來查看Redis數(shù)據(jù)庫(kù)中的數(shù)據(jù)
  • 至此,我們?cè)谌粘m?xiàng)目中整合Redis的基本使用操作就完成了,但在實(shí)際項(xiàng)目中,可能會(huì)涉及到更復(fù)雜的用法,可以根據(jù)你的業(yè)務(wù)需求調(diào)整Redis的使用即可。

到此這篇關(guān)于SpringBoot中整合Redis實(shí)現(xiàn)熱點(diǎn)數(shù)據(jù)緩存的文章就介紹到這了,更多相關(guān)SpringBoot熱點(diǎn)數(shù)據(jù)緩存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 完美解決Spring聲明式事務(wù)不回滾的問題

    完美解決Spring聲明式事務(wù)不回滾的問題

    下面小編就為大家?guī)硪黄昝澜鉀QSpring聲明式事務(wù)不回滾的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • redisson 實(shí)現(xiàn)分布式鎖的源碼解析

    redisson 實(shí)現(xiàn)分布式鎖的源碼解析

    這篇文章主要介紹了redisson 實(shí)現(xiàn)分布式鎖的源碼解析,通過模擬一個(gè)商品秒殺的場(chǎng)景結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • 如何基于java向mysql數(shù)據(jù)庫(kù)中存取圖片

    如何基于java向mysql數(shù)據(jù)庫(kù)中存取圖片

    這篇文章主要介紹了如何基于java向mysql數(shù)據(jù)庫(kù)中存取圖片,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • SpringBoot集成FTP與SFTP連接池流程

    SpringBoot集成FTP與SFTP連接池流程

    在項(xiàng)目開發(fā)中,一般文件存儲(chǔ)很少再使用SFTP服務(wù),但是也不排除合作伙伴使用SFTP來存儲(chǔ)項(xiàng)目中的文件或者通過SFTP來實(shí)現(xiàn)文件數(shù)據(jù)的交互,這篇文章主要介紹了SpringBoot集成FTP與SFTP連接池
    2022-12-12
  • 詳解Java 微服務(wù)架構(gòu)

    詳解Java 微服務(wù)架構(gòu)

    這篇文章主要介紹了Java 微服務(wù)架構(gòu)的相關(guān)資料,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2021-02-02
  • Spring實(shí)戰(zhàn)之ServletContextResource訪問資源文件示例

    Spring實(shí)戰(zhàn)之ServletContextResource訪問資源文件示例

    這篇文章主要介紹了Spring實(shí)戰(zhàn)之ServletContextResource訪問資源文件,結(jié)合實(shí)例形式分析了spring使用ServletContextResource讀取與遍歷資源文件相關(guān)操作技巧,需要的朋友可以參考下
    2019-12-12
  • Java?Spring?boot?配置JDK和MAVEN開發(fā)環(huán)境的過程

    Java?Spring?boot?配置JDK和MAVEN開發(fā)環(huán)境的過程

    本文詳細(xì)介紹了如何配置JDK和Maven環(huán)境,包括JDK的安裝與環(huán)境變量設(shè)置,Maven的下載、配置環(huán)境變量和設(shè)置阿里云倉(cāng)庫(kù),最后簡(jiǎn)述了在IntelliJ?IDEA中配置JDK和Maven的步驟,本教程適合Java開發(fā)新手進(jìn)行開發(fā)環(huán)境的搭建,確保順利進(jìn)行Java項(xiàng)目的開發(fā)
    2024-11-11
  • 詳解log4j.properties的簡(jiǎn)單配置和使用

    詳解log4j.properties的簡(jiǎn)單配置和使用

    本篇文章主要介紹了詳解log4j.properties的簡(jiǎn)單配置和使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • Java發(fā)送https請(qǐng)求并跳過ssl證書驗(yàn)證方法

    Java發(fā)送https請(qǐng)求并跳過ssl證書驗(yàn)證方法

    最近在負(fù)責(zé)一個(gè)對(duì)接第三方服務(wù)的事情,在對(duì)接期間因?yàn)榈谌椒?wù)為https的請(qǐng)求,這篇文章主要給大家介紹了關(guān)于Java發(fā)送https請(qǐng)求并跳過ssl證書驗(yàn)證的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • IDEA如何開啟并配置services窗口

    IDEA如何開啟并配置services窗口

    在使用IntelliJ IDEA時(shí),可能會(huì)遇到Services窗口不自動(dòng)彈出的情況,本文介紹了如何手動(dòng)開啟Services窗口的簡(jiǎn)單步驟,首先,通過點(diǎn)擊菜單欄中的“視圖”->“工具窗口”->“服務(wù)”,或使用快捷鍵Alt+F8(注意快捷鍵可能存在沖突)來打開Services窗口
    2024-10-10

最新評(píng)論