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

使用SpringCache加Redis做緩存

 更新時間:2021年12月31日 11:01:39   作者:愛敲代碼的Jerry  
這篇文章主要介紹了使用SpringCache加Redis做緩存方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Redis + SpringCache

1. 添加依賴

<!-- redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring2.X集成redis所需common-pool2-->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.6.0</version>
</dependency>

2. 使用配置類注入相關(guān)組件

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setConnectionFactory(factory);
        //key序列化方式
        template.setKeySerializer(redisSerializer);
        //value序列化
        template.setValueSerializer(jackson2JsonRedisSerializer);
        //value hashmap序列化
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        return template;
    }
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        //解決查詢緩存轉(zhuǎn)換異常的問題
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        // 配置序列化(解決亂碼的問題),過期時間600秒
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(600))
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
                .disableCachingNullValues();
        RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
                .cacheDefaults(config)
                .build();
        return cacheManager;
    }
}

3. 使用以下注解

  • @Cacheable:根據(jù)方法對返回結(jié)果進(jìn)行緩存,下次請求時,如果緩存存在,則直接讀取緩存數(shù)據(jù)返回,如果緩存不存在,則執(zhí)行方法,并把返回結(jié)果存入緩存,一般用在查詢方法上
  • @CachePut:每次都會執(zhí)行方法,并將結(jié)果存入緩存,其他方法可以直接讀取,一般用在新增方法上
  • @CacheEvict:會清除指定的緩存,一般用在更新或刪除方法上

注解1和2的屬性

注解屬性 作用
value 必填,緩存名,指定命名空間
cacheNames 和value類似
key 可選,可以用spEL自定義key

注解3多出兩個

注解屬性 作用
allEntries 如果指定為true,方法調(diào)用將清空所有緩存
beforeInvocation 是否在方法執(zhí)前清空,默認(rèn)為false

4. 配置

spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.database= 0
spring.redis.timeout=1800000

5. 存儲格式

value + "::" + key 的格式

例如:

@Cacheable(value = "space", key = "'key'")

緩存的鍵就是:

space::key

6. 注意點

key中如果是字符串需要加單引號,如果是spEL則不需要

Redis做緩存和SpringCache緩存

記錄一下Redis做緩存和SpringCache緩存的區(qū)別

1.Redis做緩存的話,相當(dāng)于是一個第三方緩存,所以項目重啟之后緩存數(shù)據(jù)還是存在的

2.SpringCache作緩存的話是建立在JVM上的,所以項目啟動之后緩存是自動消失的。

業(yè)務(wù):在郵件驗證碼、短信驗證碼情況下需要設(shè)置過期時間。

如何使用:用Redis和SpringCache兩種使用方法。

過期時間:如果需要設(shè)置過期時間的話,需要使用到redis。如果不需要設(shè)置過期時間,則Redis和SpringCache都可以。

SpringCache 使用方法

1.在啟動類使用@EnableCaching

@SpringBootApplication
@EnableCaching //啟動SpringCache緩存
public class GatheringApplication {}

2.是需要添加緩存的時候使用@Cacheable

//存入緩存
@Cacheable(value = "gathering",key = "#id")
public Gathering findById(String id){}3.在需要刪除緩存的時候使用@CacheEvict
//刪除
@CacheEvict(value = "gathering",key = "#gathering.id")
public void update(Gathering gathering){}

下面是SpringDataRedis的部分使用方法:

/**
   * 參數(shù)1:key 參數(shù)2:value 參數(shù)3:時間 參數(shù)4:時間單位
   */
redisTemplate.opsForValue().set(REDIS_ARTICLE_KEY+"_"+id,article,10, TimeUnit.SECONDS); 
stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);//向redis里存入數(shù)據(jù)和設(shè)置緩存時間 
stringRedisTemplate.opsForValue().get("test")//根據(jù)key獲取緩存中的val
stringRedisTemplate.boundValueOps("test").increment(-1);//val做-1操作
stringRedisTemplate.boundValueOps("test").increment(1);//val +1
stringRedisTemplate.getExpire("test")//根據(jù)key獲取過期時間
stringRedisTemplate.getExpire("test",TimeUnit.SECONDS)//根據(jù)key獲取過期時間并換算成指定單位
stringRedisTemplate.delete("test");//根據(jù)key刪除緩存
stringRedisTemplate.hasKey("546545");//檢查key是否存在,返回boolean值
stringRedisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS);//設(shè)置過期時間
stringRedisTemplate.opsForSet().add("red_123", "1","2","3");//向指定key中存放set集合
stringRedisTemplate.opsForSet().isMember("red_123", "1")//根據(jù)key查看集合中是否存在指定數(shù)據(jù)
stringRedisTemplate.opsForSet().members("red_123");//根據(jù)key獲取set集合

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

相關(guān)文章

  • Java split函數(shù)拆分后變成null問題解決方案

    Java split函數(shù)拆分后變成null問題解決方案

    這篇文章主要介紹了Java split函數(shù)拆分后變成null問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • Java數(shù)據(jù)結(jié)構(gòu)之隊列(動力節(jié)點Java學(xué)院整理)

    Java數(shù)據(jù)結(jié)構(gòu)之隊列(動力節(jié)點Java學(xué)院整理)

    隊列(Queue)是只允許在一端進(jìn)行插入,而在另一端進(jìn)行刪除的運算受限的線性表。 這篇文章詳細(xì)給大家介紹了java數(shù)據(jù)結(jié)構(gòu)之隊列,感興趣的朋友跟隨小編一起學(xué)習(xí)吧
    2017-04-04
  • 詳解JAVA里面獲取map的key和value的方法

    詳解JAVA里面獲取map的key和value的方法

    這篇文章主要介紹了詳解JAVA里面獲取map的key和value的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • java  interface 接口的使用好處分析

    java interface 接口的使用好處分析

    這篇文章主要介紹了java interface 接口的使用好處,結(jié)合實例形式分析了java interface接口的功能、基本使用方法及多態(tài)性的使用優(yōu)點,需要的朋友可以參考下
    2019-11-11
  • idea如何關(guān)閉右側(cè)類顯示方法

    idea如何關(guān)閉右側(cè)類顯示方法

    這篇文章主要介紹了idea如何關(guān)閉右側(cè)類顯示方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 淺談Spring Boot 異常處理篇

    淺談Spring Boot 異常處理篇

    本篇文章主要介紹了淺談Spring Boot 異常處理篇,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • 聊聊java中一些減少if else 的編碼習(xí)慣的方法

    聊聊java中一些減少if else 的編碼習(xí)慣的方法

    這篇文章主要介紹了聊聊java中一些減少if else 的編碼習(xí)慣的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • mybatisPlus填坑之邏輯刪除的實現(xiàn)

    mybatisPlus填坑之邏輯刪除的實現(xiàn)

    本文主要介紹了mybatisPlus填坑之邏輯刪除的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Java使用Semaphore對單接口進(jìn)行限流

    Java使用Semaphore對單接口進(jìn)行限流

    本篇主要講如何使用Semaphore對單接口進(jìn)行限流,主要有三種方式,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • 解決mybatis三表連接查詢數(shù)據(jù)重復(fù)的問題

    解決mybatis三表連接查詢數(shù)據(jù)重復(fù)的問題

    這篇文章主要介紹了解決mybatis三表連接查詢數(shù)據(jù)重復(fù)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01

最新評論