SpringBoot Cache 系列(一):概要

在 Spring Boot 中,缓存是提升应用性能的重要手段,常见的缓存方案可以分为 **本地缓存** 和 **分布式缓存** 两类。

在 Spring Boot 中,缓存是提升应用性能的重要手段,常见的缓存方案可以分为 本地缓存分布式缓存 两类。以下是几种典型方案及其实现方式:


一、Spring Cache 抽象层

Spring 提供了统一的缓存抽象(org.springframework.cache),通过注解简化缓存操作,支持多种缓存实现无缝切换。

核心注解:

  • @Cacheable:查询缓存,不存在则执行方法并缓存结果。

  • @CachePut:更新缓存,始终执行方法并刷新缓存。

  • @CacheEvict:删除缓存。

  • @Caching:组合多个缓存操作。

  • @CacheConfig:类级别的共享缓存配置。

示例代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
@Service
@CacheConfig(cacheNames = "userCache")
public class UserService {
    @Cacheable(key = "#id")
    public User getUserById(Long id) {
        // 模拟数据库查询
        return userRepository.findById(id);
    }

    @CacheEvict(key = "#user.id")
    public void updateUser(User user) {
        userRepository.update(user);
    }
}

二、本地缓存方案

1. Caffeine(推荐)

  • 特点:高性能本地缓存,支持异步刷新、过期策略。

  • 依赖

    1
    2
    3
    4
    
    <dependency>
        <groupId>com.github.ben-manes.caffeine</groupId>
        <artifactId>caffeine</artifactId>
    </dependency>
    
  • 配置

    1
    2
    3
    4
    5
    
    spring:
      cache:
        cache-names: userCache,orderCache
        caffeine:
          spec: maximumSize=500, expireAfterWrite=10m
    

2. Ehcache

  • 特点:成熟稳定,支持磁盘持久化、多级缓存。

  • 依赖

    1
    2
    3
    4
    
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
    </dependency>
    
  • 配置ehcache.xml):

    1
    2
    3
    
    <ehcache>
        <cache name="userCache" maxEntriesLocalHeap="1000" timeToLiveSeconds="600"/>
    </ehcache>
    

3. Guava Cache(旧版项目)

  • 特点:轻量级,适合简单场景(新项目建议用 Caffeine)。

  • 配置

    1
    2
    3
    4
    5
    6
    
    @Bean
    public CacheManager cacheManager() {
        GuavaCacheManager cacheManager = new GuavaCacheManager();
        cacheManager.setCacheBuilder(CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES));
        return cacheManager;
    }
    

三、分布式缓存方案

1. Redis

  • 特点:分布式、高可用,支持复杂数据结构。

  • 依赖

    1
    2
    3
    4
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    
  • 配置

    1
    2
    3
    4
    5
    
    spring:
      redis:
        host: localhost
        port: 6379
        password: 123456
    
  • 使用:通过 RedisTemplate 或直接使用 @Cacheable 注解(需配置 RedisCacheManager)。

2. Memcached

  • 特点:简单高效的分布式内存缓存。

  • 依赖(需手动集成,如 XMemcached):

    1
    2
    3
    4
    5
    
    <dependency>
        <groupId>com.googlecode.xmemcached</groupId>
        <artifactId>xmemcached</artifactId>
        <version>2.4.7</version>
    </dependency>
    
  • 配置:需手动编写客户端配置类。


四、多级缓存(混合方案)

结合本地缓存和分布式缓存,例如 Caffeine + Redis

  1. 本地缓存(Caffeine)处理高频读取,减少网络开销。

  2. Redis 作为二级缓存,保证分布式一致性。

  3. 通过消息队列(如 RabbitMQ)同步各节点的本地缓存。


五、选型建议

  • 单机应用:优先选择 CaffeineEhcache

  • 分布式系统:使用 RedisMemcached 保证数据一致性。

  • 高性能场景:考虑 Caffeine + Redis 多级缓存。


通过 Spring Cache 抽象层,可以轻松切换底层缓存实现,而无需修改业务代码。根据具体场景选择合适的缓存策略,能显著提升系统响应速度和并发能力。

Comments
  • Latest
  • Oldest
  • Hottest
No comment yet.
Powered by Waline v2.15.8
Gear(夕照)的博客。记录开发、生活,以及一些不足为道的思考……