• 手把手教你使用 Spring Boot 3 开发上线一个前后端分离的生产级系统(六) - 本地缓存 Caffeine 和 分布式缓存 Redis 集成与配置


    本地缓存 Caffeine 集成和配置

    Caffeine 是 Java 8 对 Google Guava 缓存的重写,是一个提供了近乎最佳命中率的高性能的缓存库。我们按照如下步骤集成和配置:

    1. 添加 spring-boot-starter-cache 依赖

    使用 spring-boot-starter-cache “Starter” 可以快速添加基本缓存依赖项。 starter 引入了 spring-context-support。如果我们手动添加依赖项,则必须包含 spring-context-support 才能使用 JCache 或 Caffeine 支持。

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    1. 添加 caffeine 依赖
    <dependency>
    <groupId>com.github.ben-manes.caffeine</groupId>
    <artifactId>caffeine</artifactId>
    </dependency>
    1. 自定义缓存管理器
    /**
    * Caffeine 缓存管理器
    */
    @Bean
    public CacheManager caffeineCacheManager() {
    SimpleCacheManager cacheManager = new SimpleCacheManager();
    List<CaffeineCache> caches = new ArrayList<>(CacheConsts.CacheEnum.values().length);
    for (CacheConsts.CacheEnum c : CacheConsts.CacheEnum.values()) {
    if (c.isLocal()) {
    Caffeine<Object, Object> caffeine = Caffeine.newBuilder().recordStats().maximumSize(c.getMaxSize());
    if (c.getTtl() > 0) {
    caffeine.expireAfterWrite(Duration.ofSeconds(c.getTtl()));
    }
    caches.add(new CaffeineCache(c.getName(), caffeine.build()));
    }
    }
    cacheManager.setCaches(caches);
    return cacheManager;
    }
    1. 使用 @EnableCaching 注解开启缓存
    @SpringBootApplication
    @MapperScan("io.github.xxyopen.novel.dao.mapper")
    @EnableCaching
    @Slf4j
    public class NovelApplication {
    public static void main(String[] args) {
    SpringApplication.run(NovelApplication.class, args);
    }
    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext context){
    return args -> {
    Map<String, CacheManager> beans = context.getBeansOfType(CacheManager.class);
    log.info("加载了如下缓存管理器:");
    beans.forEach((k,v)->{
    log.info("{}:{}",k,v.getClass().getName());
    log.info("缓存:{}",v.getCacheNames());
    });
    };
    }
    }

    这样我们就可以使用 Spring Cache 的注解(例如 @Cacheable)开发了。

    分布式缓存 Redis 集成和配置

    本地缓存虽然有着访问速度快的优点,但无法进行大数据的存储。并且当我们集群部署多个服务节点,或者后期随着业务发展进行服务拆分后,没法共享缓存和保证缓存数据的一致性。
    本地缓存的数据还会随应用程序的重启而丢失,这样对于需要持久化的数据满足不了需求,还会导致重启后数据库瞬时压力过大。

    所以本地缓存一般适合于缓存只读数据,如统计类数据,或者每个部署节点独立的数据。其它情况就需要用到分布式缓存了。

    分布式缓存的集成步骤和本地缓存基本差不多,除了替换 caffeine 的依赖项为我们 redis 的依赖和配置上我们自定义的 redis 缓存管理器外,还要在配置文件中加入 redis 的连接配置:

    1. 加入依赖
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    1. 配置 redis 缓存管理器
    /**
    * 缓存配置类
    *
    * @author xiongxiaoyang
    * @date 2022/5/12
    */
    @Configuration
    public class CacheConfig {
    /**
    * Caffeine 缓存管理器
    */
    @Bean
    @Primary
    public CacheManager caffeineCacheManager() {
    SimpleCacheManager cacheManager = new SimpleCacheManager();
    List<CaffeineCache> caches = new ArrayList<>(CacheConsts.CacheEnum.values().length);
    for (CacheConsts.CacheEnum c : CacheConsts.CacheEnum.values()) {
    if (c.isLocal()) {
    Caffeine<Object, Object> caffeine = Caffeine.newBuilder().recordStats().maximumSize(c.getMaxSize());
    if (c.getTtl() > 0) {
    caffeine.expireAfterWrite(Duration.ofSeconds(c.getTtl()));
    }
    caches.add(new CaffeineCache(c.getName(), caffeine.build()));
    }
    }
    cacheManager.setCaches(caches);
    return cacheManager;
    }
    /**
    * Redis 缓存管理器
    */
    @Bean
    public CacheManager redisCacheManager(RedisConnectionFactory connectionFactory) {
    RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
    RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
    .disableCachingNullValues().prefixCacheNameWith(CacheConsts.REDIS_CACHE_PREFIX);
    Map<String, RedisCacheConfiguration> cacheMap = new LinkedHashMap<>(CacheConsts.CacheEnum.values().length);
    for (CacheConsts.CacheEnum c : CacheConsts.CacheEnum.values()) {
    if (c.isRemote()) {
    if (c.getTtl() > 0) {
    cacheMap.put(c.getName(), RedisCacheConfiguration.defaultCacheConfig().disableCachingNullValues()
    .prefixCacheNameWith(CacheConsts.REDIS_CACHE_PREFIX).entryTtl(Duration.ofSeconds(c.getTtl())));
    } else {
    cacheMap.put(c.getName(), RedisCacheConfiguration.defaultCacheConfig().disableCachingNullValues()
    .prefixCacheNameWith(CacheConsts.REDIS_CACHE_PREFIX));
    }
    }
    }
    RedisCacheManager redisCacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig, cacheMap);
    redisCacheManager.setTransactionAware(true);
    redisCacheManager.initializeCaches();
    return redisCacheManager;
    }
    }
    1. application.yml 中加入 redis 连接配置信息
    spring:
    redis:
    host: 127.0.0.1
    port: 6379
    password: 123456
  • 相关阅读:
    计算机毕业设计 SSM学生成绩测评系统 大学生毕业管理系统 学生课程信息管理系统Java Vue MySQL数据库 远程调试 代码讲解
    Python的比较运算符查询表
    git diff 生成patch合入代码
    SpringBoot 20 Shiro授权的实现和整合Thymeleaf
    el-form内容重置(解决点击保存关闭后再点击新增会有编辑携带的数据的问题)
    开源项目 DamiBus v0.54 发布,1000 万发事件/秒
    SpringMVC如何在web.xml中配置DispatcherServlet呢?
    【黑马-SpringCloud技术栈】【01】完整的微服务技术栈_微服务技术对比
    第十四届蓝桥杯模拟赛第二期部分题答案(C++代码)
    【24种设计模式】单例模式(Singleton Pattern)
  • 原文地址:https://www.cnblogs.com/xxyopen/p/16322775.html