1. 程式人生 > 其它 >springboot使用cache整合redis(自定義每個key過期時間)

springboot使用cache整合redis(自定義每個key過期時間)

技術標籤:redisredisehcachespring boot

@Cacheable是SpringCache的註解

當初見到這個註解以為是redis的,後來研究發現是個spring的全家桶,快取器可以使用 ehCache,Redis,ConcurrentHashMap(jvm),ehCache研究的不多,好像和jvm記憶體一樣做不到分散式。本文就redis做自定義key過期設定

支援配置的key過期時間

採用springboot 2.4.0 版本,發現這個版本對配置檔案做了優化;比如:
配置了dev之後,dev.yml 和 dev.properties都會生效
在這裡插入圖片描述

配置檔案

# cacheName:
過期時間;逗號分隔 xcode.redis.cache.initialCacheNamesAndDuration=testCacheMap:15m

code

package com.jgma.xcode.redis.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.
slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.CacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.cache.RedisCacheWriter; 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; import org.springframework.lang.Nullable; import java.io.Serializable; import java.time.Duration; import java.util.LinkedHashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @Author: admin * @GitHub https://github.com/JGMa-java/magic-code.git */ @Slf4j @Configuration public class RedisConfig { @Value("${xcode.redis.cache.initialCacheNamesAndDuration}") private String[] initialCacheNames; private Pattern pattern = Pattern.compile("(?:([-+]?[0-9]+)M)|(?:([-+]?[0-9]+)H)|(?:([-+]?[0-9]+)S)", Pattern.CASE_INSENSITIVE); private String regStr = "m|M|s|S|h|H"; /** * 配置redisTemplate序列化方式,使用預設的序列化manager檢視會顯示亂碼 */ @Bean public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory redisConnectionFactory) { RedisTemplate<String, Serializable> template = new RedisTemplate<>(); //使用StringRedisSerializer來序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); // 使用Jackson2JsonRedisSerialize 替換預設的jdkSerializeable序列化redis的value值 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); template.setValueSerializer(jackson2JsonRedisSerializer); template.setConnectionFactory(redisConnectionFactory); return template; } @Bean public CacheManager cacheManager(LettuceConnectionFactory connectionFactory) { RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory); Map<String, RedisCacheConfiguration> initialCacheConfigurations = new LinkedHashMap(); for (String initialCacheName : initialCacheNames) { String[] split = initialCacheName.split(":"); String cacheName = split[0]; String durationStr = split[1]; Duration duration = null; try { duration = parseDuration(durationStr); } catch (Exception e) { log.error(e.getMessage()); continue; } RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(duration); initialCacheConfigurations.put(cacheName, cacheConfiguration); } RedisCacheManager redisCacheManager = RedisCacheManager.builder(redisCacheWriter).withInitialCacheConfigurations(initialCacheConfigurations).build(); return redisCacheManager; } /** * 過期時間配置轉Duration,僅支援時分秒格式 * * @param duration 需要匹配的配置時間;例 - 2s * @return * @throws RuntimeException 不支援的時間格式 */ private Duration parseDuration(@Nullable String duration) throws RuntimeException { Matcher matcher = pattern.matcher(duration); if (!matcher.matches()) { throw new RuntimeException("[過期時間僅支援h,s,m格式]當前引數:{" + duration + "}"); } if (duration.contains("m") || duration.contains("M")) { return Duration.ofMinutes(Long.valueOf(duration.replaceAll(regStr, ""))); } else if (duration.contains("s") || duration.contains("S")) { return Duration.ofSeconds(Long.valueOf(duration.replaceAll(regStr, ""))); } else { return Duration.ofHours(Long.valueOf(duration.replaceAll(regStr, ""))); } } }

業務層

package com.jgma.xcode.redis.service;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: admin
 */
@Service
public class RedisService {
    private static Map<String, String> result = new HashMap();

    static {
        result.put("1", "字串1");
        result.put("2", "字串2");
    }

    @Cacheable(key = "#key",value = "testCacheMap",cacheManager = "cacheManager")
    public Object get(String key) {
        System.out.println("我是測試cache,service返回的。。。");
        return result.get(key);
    }
}

個人感覺程式碼不是太優美,有沒有好的優化方式???