Class CacheAutoConfiguration

java.lang.Object
cloud.opencode.base.cache.spring.CacheAutoConfiguration

public final class CacheAutoConfiguration extends Object
Cache Auto-Configuration for Spring Boot 缓存自动配置类 - 用于 Spring Boot 自动配置

This class provides Spring Boot auto-configuration support without requiring Spring as a compile-time dependency. It uses reflection to interact with Spring.

此类提供 Spring Boot 自动配置支持,无需 Spring 作为编译时依赖。它使用反射与 Spring 交互。

Configuration Properties | 配置属性:

# application.yml
opencode:
  cache:
    enabled: true
    allow-null-values: true
    default:
      maximum-size: 10000
      expire-after-write: 30m
      expire-after-access: 10m
      record-stats: true
    caches:
      users:
        maximum-size: 5000
        expire-after-write: 1h
      products:
        maximum-size: 50000
        expire-after-write: 24h

Usage with Spring Boot | Spring Boot 使用方式:

// 1. Add dependency in pom.xml
<dependency>
    <groupId>cloud.opencode.base</groupId>
    <artifactId>opencode-base-cache</artifactId>
</dependency>

// 2. Enable caching in configuration class
@Configuration
@EnableCaching
public class CacheConfiguration {
    // Auto-configuration will provide CacheManager bean
}

// 3. Use Spring Cache annotations
@Service
public class UserService {
    @Cacheable("users")
    public User findById(Long id) {
        return userRepository.findById(id);
    }
}

Features | 主要功能:

  • Auto-detection of Spring Boot environment - 自动检测 Spring Boot 环境
  • YAML/properties configuration binding - YAML/属性配置绑定
  • Per-cache and default configuration - 每缓存和默认配置
  • Conditional bean registration - 条件化 Bean 注册

Usage Examples | 使用示例:

// Auto-configured by Spring Boot when on classpath
// No explicit usage needed - Spring detects and configures automatically

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-cache V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • createCacheManager

      public static OpenCodeCacheManager createCacheManager(CacheProperties properties)
      Create OpenCodeCacheManager from CacheProperties 从 CacheProperties 创建 OpenCodeCacheManager
      Parameters:
      properties - the cache properties | 缓存属性
      Returns:
      configured cache manager | 配置的缓存管理器
    • createCacheManager

      public static OpenCodeCacheManager createCacheManager(CacheProperties properties, boolean validate)
      Create OpenCodeCacheManager from CacheProperties with optional validation 从 CacheProperties 创建 OpenCodeCacheManager,可选验证
      Parameters:
      properties - the cache properties | 缓存属性
      validate - whether to validate configuration | 是否验证配置
      Returns:
      configured cache manager | 配置的缓存管理器
      Throws:
      IllegalArgumentException - if validation is enabled and configuration is invalid
      Since:
      V2.0.1
    • getAutoConfigurationClassName

      public static String getAutoConfigurationClassName()
      Get the auto-configuration class name for META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 获取自动配置类名,用于 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

      Create this file in resources:

      # META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
      cloud.opencode.base.cache.spring.CacheAutoConfiguration
      
      Returns:
      fully qualified class name | 完全限定类名
    • getPropertiesClassName

      public static String getPropertiesClassName()
      Get the properties class name for @ConfigurationProperties 获取属性类名,用于 @ConfigurationProperties
      Returns:
      fully qualified class name | 完全限定类名
    • getCacheManagerClassName

      public static String getCacheManagerClassName()
      Get the cache manager class name for bean registration 获取缓存管理器类名,用于 bean 注册
      Returns:
      fully qualified class name | 完全限定类名