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

SpringBoot使用@Cacheable時設(shè)置部分緩存的過期時間方式

 更新時間:2021年12月23日 09:43:24   作者:Qiwany  
這篇文章主要介紹了SpringBoot使用@Cacheable時設(shè)置部分緩存的過期時間方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

使用@Cacheable時設(shè)置部分緩存的過期時間

業(yè)務(wù)場景

Spring Boot項目中有一些查詢數(shù)據(jù)需要緩存到Redis中,其中有一些緩存是固定數(shù)據(jù)不會改變,那么就沒必要設(shè)置過期時間。還有一些緩存需要每隔幾分鐘就更新一次,這時就需要設(shè)置過期時間。

Service層部分代碼如下:

@Override
@Cacheable(cacheNames = {"distributor"}, key = "#root.methodName")
public List<CityVO> findCities() {
 return distributorMapper.selectCities();
}
@Override
@Cacheable(cacheNames = {"distributor"}, key = "#root.methodName.concat('#cityId').concat(#cityId)")
public List<DistributorVO> findDistributorsByCityId(String cityId) {
 return distributorMapper.selectByCityId(cityId);
}
@Override
@Cacheable(cacheNames = {"car"}, key = "#root.methodName.concat('#cityId').concat(#cityId)")
public String carList(String cityId) {
 RequestData data = new RequestData();
 data.setCityId(cityId);
 
 CarListParam param = new CarListParam();
 param.setRequestData(data);
 
 String jsonParam = JSON.toJSONString(param);
 return HttpClientUtil.sendPostWithJson(ApiUrlConst.MULE_APP, jsonParam);
}

在使用@Cacheable注解對查詢數(shù)據(jù)進行緩存時,使用cacheNames屬性指定了緩存名稱。下面我們就針對不同的cacheNames來設(shè)置失效時間。

添加Redis配置類RedisConfig.java

代碼如下:

@Slf4j
@Configuration
@EnableCaching //啟用緩存
public class RedisConfig {
 
 /**
  * 自定義緩存管理器
  */
 @Bean
 public RedisCacheManager cacheManager(RedisConnectionFactory factory) {
  RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
  Set<String> cacheNames = new HashSet<>();
  cacheNames.add("car");
  cacheNames.add("distributor");
  ConcurrentHashMap<String, RedisCacheConfiguration> configMap = new ConcurrentHashMap<>();
  configMap.put("car", config.entryTtl(Duration.ofMinutes(6L)));
  configMap.put("distributor", config);
  
  //需要先初始化緩存名稱,再初始化其它的配置。
  RedisCacheManager cacheManager = RedisCacheManager.builder(factory).initialCacheNames(cacheNames).withInitialCacheConfigurations(configMap).build();
  return cacheManager;
 }
}

上面代碼,在configMap中指定了cacheNames為car的緩存過期時間為6分鐘。

@Cacheable自定義緩存過期時間

pom

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

yml

#  redis配置
spring:
    redis:
      database: 0
      host: 127.0.0.1
      password: 123456
      port: 6379
      timeout:  5000
      lettuce:
        pool:
          max-active: 300
          max-wait: -1
          max-idle: 20
          min-idle: 10

RedisConfig

  • RedisCacheManager:緩存默認不過期,所以這里返回自定RedisCacheManager
//return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
return new CustomRedisCacheManager(redisCacheWriter, redisCacheConfiguration);
@Configuration
public class RedisConfig {
    
    /*
     * @description redis序列化方式
     * @author xianping
     * @date 2020/9/25
     * @param redisConnectionFactory
     * @return RedisTemplate
     **/
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
    /*
     * @description Redis緩存的序列化方式使用redisTemplate.getValueSerializer(),不在使用JDK默認的序列化方式
     * @author xianping
     * @date 2020/9/25
     * @param redisTemplate
     * @return RedisCacheManager
     **/
    @Bean
    public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory());
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()               
        .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()));
        //return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
        return new CustomRedisCacheManager(redisCacheWriter, redisCacheConfiguration);
    }
}

CustomRedisCacheManager

自定義RedisCacheManager:若@Cacheable value中包含'#‘號,則'#'后為緩存生存時間,不存在則表示緩存不進行生存時間設(shè)置

//cacheConfig.entryTtl 設(shè)置緩存過期時間
public RedisCacheConfiguration entryTtl(Duration ttl) {
    Assert.notNull(ttl, "TTL duration must not be null!");
    return new RedisCacheConfiguration(ttl, this.cacheNullValues, this.usePrefix,    this.keyPrefix, this.keySerializationPair, this.valueSerializationPair,     this.conversionService);
}
//Duration.ofMinutes 持續(xù)時間
//這里默認是以分鐘為單位,所以調(diào)用ofMinutes靜態(tài)方法進行轉(zhuǎn)換
public static Duration ofMinutes(long minutes) {
        return create(Math.multiplyExact(minutes, SECONDS_PER_MINUTE), 0);
}
public class CustomRedisCacheManager extends RedisCacheManager {
    /*
     * @description 提供默認構(gòu)造器
     * @author xianping
     * @date 2020/9/28 9:22
     * @param
     * @param cacheWriter
     * @param defaultCacheConfiguration
     * @return
     **/
    public CustomRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {
        super(cacheWriter, defaultCacheConfiguration);
    }
    /*
     * @description 重寫父類createRedisCache方法
     * @author xianping
     * @date 2020/9/28 9:22
     * @param
     * @param name @Cacheable中的value
     * @param cacheConfig
     * @return org.springframework.data.redis.cache.RedisCache
     **/
    @Override
    protected RedisCache createRedisCache(String name, RedisCacheConfiguration cacheConfig) {
        //名稱中存在#標記進行到期時間配置
        if (!name.isEmpty() && name.contains("#")) {
            String[] SPEL = name.split("#");
            if (StringUtils.isNumeric(SPEL[1])) {
                //配置緩存到期時間
                int cycle = Integer.parseInt(SPEL[1]);
                return super.createRedisCache(SPEL[0], cacheConfig.entryTtl(Duration.ofMinutes(cycle * 24 * 60)));
            }
        }
        return super.createRedisCache(name, cacheConfig);
    }
}

使用

生存時間1天

@Cacheable(value = "cacheTest#1")
public String cacheTest() {
    return "cacheTest";
}

緩存持久,無過期時間

@Cacheable(value = "cacheTest")
public String cacheTest() {
    return "cacheTest";
}

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

相關(guān)文章

  • Java使用JNDI連接數(shù)據(jù)庫的實現(xiàn)方法

    Java使用JNDI連接數(shù)據(jù)庫的實現(xiàn)方法

    本文主要介紹了Java使用JNDI連接數(shù)據(jù)庫的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • Java數(shù)據(jù)結(jié)構(gòu)之線段樹的原理與實現(xiàn)

    Java數(shù)據(jù)結(jié)構(gòu)之線段樹的原理與實現(xiàn)

    線段樹是一種二叉搜索樹,是用來維護區(qū)間信息的數(shù)據(jù)結(jié)構(gòu)。本文將利用示例詳細講講Java數(shù)據(jù)結(jié)構(gòu)中線段樹的原理與實現(xiàn),需要的可以參考一下
    2022-06-06
  • Java函數(shù)式編程(八):字符串及方法引用

    Java函數(shù)式編程(八):字符串及方法引用

    這篇文章主要介紹了Java函數(shù)式編程(八):字符串及方法引用,本文是系列文章的第8篇,其它文章請參閱本文底部的相關(guān)文章,需要的朋友可以參考下
    2014-09-09
  • JetBrains推出全新IDE之Fleet詳解

    JetBrains推出全新IDE之Fleet詳解

    當您第一次啟動 Fleet 時,它會作為一個功能齊全的編輯器啟動,具有語法高亮顯示、簡單的代碼補全以及您對一個編輯器期待的所有功能,本文學(xué)習下JetBrains推出全新IDE之Fleet相關(guān)知識,感興趣的朋友跟隨小編一起看看吧
    2021-12-12
  • 通過pipeline配置sonar自動化實現(xiàn)過程解析

    通過pipeline配置sonar自動化實現(xiàn)過程解析

    這篇文章主要介紹了通過pipeline配置sonar自動化實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下
    2020-11-11
  • java 中單例模式餓漢式與懶漢式的對比

    java 中單例模式餓漢式與懶漢式的對比

    這篇文章主要介紹了java 中單例模式餓漢式與懶漢式的對比的相關(guān)資料,這里對這兩種單例模式進行對比,希望大家能理解并應(yīng)用,需要的朋友可以參考下
    2017-08-08
  • Java實現(xiàn)局域網(wǎng)聊天小程序

    Java實現(xiàn)局域網(wǎng)聊天小程序

    這篇文章主要為大家詳細介紹了Java實現(xiàn)局域網(wǎng)聊天小程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Java中的LinkedHashSet集合解讀

    Java中的LinkedHashSet集合解讀

    這篇文章主要介紹了Java中的LinkedHashSet集合解讀,LInkedHashSet這個容器不知道大家在平時的工作用的多嗎,反正我基本上沒有用過,所以,本文主要對于它的特點、使用場景、實現(xiàn)原理,做一個講解,希望對大家平時的工作有所幫助,需要的朋友可以參考下
    2023-09-09
  • JAVA基于Slack實現(xiàn)異常日志報警詳解

    JAVA基于Slack實現(xiàn)異常日志報警詳解

    這篇文章主要為大家介紹了JAVA基于Slack實現(xiàn)異常日志報警詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • SpringBoot中的Spring Cloud Hystrix原理和用法詳解

    SpringBoot中的Spring Cloud Hystrix原理和用法詳解

    在Spring Cloud中,Hystrix是一個非常重要的組件,Hystrix可以幫助我們構(gòu)建具有韌性的分布式系統(tǒng),保證系統(tǒng)的可用性和穩(wěn)定性,在本文中,我們將介紹SpringBoot中的Hystrix,包括其原理和如何使用,需要的朋友可以參考下
    2023-07-07

最新評論