benym的知识笔记 benym的知识笔记
🦮首页
  • Java

    • Java-基础
    • Java-集合
    • Java-多线程与并发
    • Java-JVM
    • Java-IO
  • Python

    • Python-基础
    • Python-机器学习
  • Kafka
  • Redis
  • MySQL
  • 分布式事务
  • Spring

    • SpringIOC
    • SpringAOP
🦌设计模式
  • 剑指Offer
  • LeetCode
  • 排序算法
🐧实践
  • Rpamis

    • Utils
    • Exception
    • Security
  • 归档
  • 标签
  • 目录
🦉里程碑
🐷关于
GitHub (opens new window)

benym

惟其艰难,才更显勇毅🍂惟其笃行,才弥足珍贵
🦮首页
  • Java

    • Java-基础
    • Java-集合
    • Java-多线程与并发
    • Java-JVM
    • Java-IO
  • Python

    • Python-基础
    • Python-机器学习
  • Kafka
  • Redis
  • MySQL
  • 分布式事务
  • Spring

    • SpringIOC
    • SpringAOP
🦌设计模式
  • 剑指Offer
  • LeetCode
  • 排序算法
🐧实践
  • Rpamis

    • Utils
    • Exception
    • Security
  • 归档
  • 标签
  • 目录
🦉里程碑
🐷关于
GitHub (opens new window)
  • 站点优化

    • 将hexo自定义域名升级https
    • hexo到Typecho的迁移日志
  • 思考与方案

    • 海量数据TopK问题
    • 关于DO,VO,DTO,QueryParam的思考
    • 异步消息通知—异步改造
    • 二叉搜索树及AVL树详解
    • 简单高效的代码优化-事务后异步处理
    • 接口管理平台Yapi-最佳实践
    • Yapi私有化部署方案
    • Sentinel-Dashboard持久化生产环境解决方案
    • 单测覆盖率工具在多模块项目中的集成
    • DSTransactional与Transactional事务混用死锁场景分析
  • AI人工智能

    • 基于Docker如何快速部署自己的ChatGPT
  • 实用代码

    • 编程式事务工具类
    • EasyExcel工具类
    • 本地锁工具类
    • Jackson基本配置类
    • Mybatis-plus基本配置类
    • RestTemplate基本配置类
    • 线程池基本配置类
    • RedisTemplate基本配置类
    • SpringData-Mongo基本配置类
    • SpringCache基本配置类
      • 起步依赖
      • SpringCacheConfig
  • 实践
  • 实用代码
benym
2024-05-16
目录

SpringCache基本配置类

# 起步依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
1
2
3
4
1
2
3
4

# SpringCacheConfig

同时配置带过期时间的CacheManager

自定义Json序列化方式存储

import cn.hutool.core.util.RandomUtil;
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.SerializationFeature;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.cache.CacheManager;
import org.springframework.cache.support.NoOpCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

/**
 * spring cache配置
 */
@Configuration
public class SpringCacheConfig {

    @Value("${cache.enabled}")
    private Boolean cacheEnabled;

    /**
     * 随机数上界-1天时间内随机数
     */
    private static final long CACHE_RANDOM_LIMIT = 24 * 60 * 60 * 1000L;

    /**
     * 获取缓存时间
     * 缓存时间=sourceCacheTtl+random limit
     *
     * @param sourceCacheTtl sourceCacheTtl
     * @param limit          limit
     * @return long
     */
    private long cacheTtlWithRandom(long sourceCacheTtl, long limit) {
        long random = RandomUtil.randomLong(limit);
        return sourceCacheTtl + random;
    }

    /**
     * 同时随机过期时间解决缓存雪崩问题
     *
     * @return CacheManager
     */
    @Bean("testCache")
    public CacheManager cacheManager1Day(RedisConnectionFactory connectionFactory) {
        if (!cacheEnabled) {
            // 禁用缓存,不会存储
            return new NoOpCacheManager();
        }
        // 3天过期时间,转换成毫秒
        long threeDaysInMillis = 3 * 24 * 60 * 60 * 1000L;
        // 过期时间和最大空闲时间都设为3天+随机时间
        long trueTtl = cacheTtlWithRandom(threeDaysInMillis, CACHE_RANDOM_LIMIT);
        RedisCacheConfiguration config = instanceConfig(trueTtl);
        return RedisCacheManager.builder(connectionFactory)
                .cacheDefaults(config)
                .transactionAware()
                .build();
    }

    /**
     * 初始化config
     *
     * @param ttl 超时时间
     * @return RedisCacheConfiguration
     */
    private RedisCacheConfiguration instanceConfig(Long ttl) {
        // value存储为JSON格式
        // 使用Jackson2JsonRedisSerialize 替换默认序列化
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        // 指定objectMapper带输入类型的序列化,如果不指定redis中则存储纯json,序列化返回后解析默认为LinkedHashMap
        // 需要自己转换类型,指定序列化类型后无需再进行转化
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(LaissezFaireSubTypeValidator.instance,
                ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_ARRAY);
        // 处理LocalDateTime
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        objectMapper.registerModule(new JavaTimeModule());
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        return RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(ttl))
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));

    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

带过期时间的CacheManager使用方法如下

@Cacheable(cacheNames = "TestName",
            key = "'123456'",
            cacheManager = "testCache",
            sync = true)
1
2
3
4
1
2
3
4

其中存储到Redis中的key的规则为

  • 当没有配置key时:key=cacheNames+::+SimpleKey [],如TestName::SimpleKey []
  • 当配置了key时:key=cacheNames+::+key,如TestName::123456
编辑 (opens new window)
#SpringCache
上次更新: 2024/05/20, 17:55:40
SpringData-Mongo基本配置类

← SpringData-Mongo基本配置类

最近更新
01
DSTransactional与Transactional事务混用死锁场景分析
03-04
02
Rpamis-security-原理解析
12-13
03
Rpamis-security-技术背景
11-29
更多文章>
Theme by Vdoing | Copyright © 2018-2024 benym | MIT License
 |   |   | 
渝ICP备18012574号 | 渝公网安备50010902502537号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式