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

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ù)庫索引(默認(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ù)庫進(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ù)庫的方法

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ù)庫中的數(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)文章

  • Java多線程中Callable和Future的解讀

    Java多線程中Callable和Future的解讀

    這篇文章主要介紹了Java多線程中Callable和Future的解讀,Callable接口類似于Runnable,從名字就可以看出來了,但是Runnable不會(huì)返回結(jié)果,并且無法拋出返回結(jié)果的異常,而Callable功能更強(qiáng)大一些,被線程執(zhí)行后,可以返回值,這個(gè)返回值可以被Future拿到,需要的朋友可以參考下
    2023-09-09
  • java利用pdfbox+poi往pdf插入數(shù)據(jù)

    java利用pdfbox+poi往pdf插入數(shù)據(jù)

    這篇文章主要給大家介紹了關(guān)于java利用pdfbox+poi如何往pdf插入數(shù)據(jù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-02-02
  • Java多線程之生產(chǎn)者消費(fèi)者模式詳解

    Java多線程之生產(chǎn)者消費(fèi)者模式詳解

    這篇文章主要為大家詳細(xì)介紹了Java多線程之生產(chǎn)者消費(fèi)者模式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • MyBatis-Plus Generator配置詳解

    MyBatis-Plus Generator配置詳解

    這篇文章主要介紹了MyBatis-Plus Generator配置詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Java線程生命周期的終止與復(fù)位

    Java線程生命周期的終止與復(fù)位

    這篇文章主要介紹了Java線程生命周期的終止與復(fù)位,Java的線程狀態(tài)描述放在Thread類里面的枚舉類State中.總共包含了6中狀態(tài),具體詳情需要的小伙伴可以參考一下文章描述
    2022-07-07
  • SpringBoot中properties,yml,yaml的區(qū)別及使用說明

    SpringBoot中properties,yml,yaml的區(qū)別及使用說明

    這篇文章主要介紹了SpringBoot中properties,yml,yaml的區(qū)別及使用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Spring?Boot存在路徑遍歷漏洞CVE-2021-22118的問題解析

    Spring?Boot存在路徑遍歷漏洞CVE-2021-22118的問題解析

    CVE-2021-22118?是一個(gè)在?Spring?Boot?中發(fā)現(xiàn)的漏洞,該漏洞關(guān)系到?Spring?Boot?的開發(fā)者工具(Devtools)中的遠(yuǎn)程更新(Remote?Update)功能,這篇文章主要介紹了Spring?Boot存在路徑遍歷漏洞CVE-2021-22118,需要的朋友可以參考下
    2023-09-09
  • SpringBoot接口接收json參數(shù)解析

    SpringBoot接口接收json參數(shù)解析

    這篇文章主要介紹了SpringBoot接口接收json參數(shù)解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Mybatis不支持batchInsertOrUpdate返顯id問題

    Mybatis不支持batchInsertOrUpdate返顯id問題

    這篇文章主要介紹了Mybatis不支持batchInsertOrUpdate返顯id問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • 詳解Java繼承中屬性、方法和對(duì)象的關(guān)系

    詳解Java繼承中屬性、方法和對(duì)象的關(guān)系

    這篇文章主要幫助大家詳細(xì)介紹了Java繼承中屬性、方法和對(duì)象的關(guān)系,感興趣的朋友可以參考一下
    2016-03-03

最新評(píng)論

夏津县| 平阳县| 海盐县| 紫云| 奉贤区| 梨树县| 汽车| 大丰市| 新巴尔虎右旗| 雷波县| 雷州市| 浦北县| 武山县| 福泉市| 上高县| 麻阳| 抚顺县| 侯马市| 兴海县| 惠水县| 太仓市| 陕西省| 乐至县| 香河县| 科技| 大姚县| 金溪县| 资中县| 吉林省| 光山县| 桐庐县| 德保县| 肥东县| 奉节县| 滦平县| 象州县| 永康市| 肥城市| 宽甸| 侯马市| 岳西县|