Class OpenCodeCacheManager

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

public class OpenCodeCacheManager extends Object
OpenCode Cache Manager for Spring Cache Abstraction OpenCode 缓存管理器 - 用于 Spring Cache 抽象

Implements Spring's CacheManager interface to integrate OpenCode Cache with Spring's @Cacheable, @CachePut, @CacheEvict annotations.

实现 Spring 的 CacheManager 接口,将 OpenCode Cache 与 Spring 的 @Cacheable@CachePut@CacheEvict 注解集成。

Usage Examples | 使用示例:

// Configuration class
@Configuration
@EnableCaching
public class CacheConfiguration {

    @Bean
    public CacheManager cacheManager() {
        return OpenCodeCacheManager.builder()
            .defaultConfig(config -> config
                .maximumSize(10000)
                .expireAfterWrite(Duration.ofMinutes(30))
                .recordStats())
            .cache("users", config -> config
                .maximumSize(5000)
                .expireAfterAccess(Duration.ofMinutes(10)))
            .cache("products", config -> config
                .maximumSize(50000)
                .expireAfterWrite(Duration.ofHours(1)))
            .build();
    }
}

// Service class
@Service
public class UserService {

    @Cacheable("users")
    public User findById(Long id) {
        return userRepository.findById(id);
    }

    @CachePut(value = "users", key = "#user.id")
    public User save(User user) {
        return userRepository.save(user);
    }

    @CacheEvict("users")
    public void delete(Long id) {
        userRepository.deleteById(id);
    }
}

Features | 主要功能:

  • Spring Cache abstraction integration - Spring Cache 抽象集成
  • Per-cache configuration - 每缓存配置
  • Default configuration support - 默认配置支持
  • Dynamic cache creation - 动态缓存创建

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

    • builder

      public static OpenCodeCacheManager.Builder builder()
      Create builder 创建构建器
      Returns:
      builder | 构建器
    • create

      public static OpenCodeCacheManager create()
      Create with default settings 使用默认设置创建
      Returns:
      cache manager | 缓存管理器
    • getCache

      public cloud.opencode.base.cache.spring.SpringCache getCache(String name)
    • getCacheNames

      public Collection<String> getCacheNames()
    • getDelegate

      public CacheManager getDelegate()
      Get underlying OpenCode CacheManager 获取底层 OpenCode CacheManager
      Returns:
      cache manager | 缓存管理器
    • getAllStats

      public Map<String, CacheStats> getAllStats()
      Get statistics for all managed caches 获取所有管理缓存的统计信息
      Returns:
      map of cache name to stats | 缓存名称到统计的映射
      Since:
      V2.0.1
    • getCacheStats

      public CacheStats getCacheStats(String name)
      Get statistics for a specific cache 获取特定缓存的统计信息
      Parameters:
      name - cache name | 缓存名称
      Returns:
      cache stats or empty stats if not found | 缓存统计,不存在返回空统计
      Since:
      V2.0.1
    • getCombinedStats

      public CacheStats getCombinedStats()
      Get combined statistics across all caches 获取所有缓存的合并统计信息
      Returns:
      combined stats | 合并的统计
      Since:
      V2.0.1
    • getCacheMetrics

      public CacheMetrics getCacheMetrics(String name)
      Get metrics for a specific cache 获取特定缓存的指标
      Parameters:
      name - cache name | 缓存名称
      Returns:
      cache metrics or null if cache not found | 缓存指标,缓存不存在返回 null
      Since:
      V2.0.1
    • invalidateAll

      public void invalidateAll()
      Invalidate all entries in all managed caches 使所有管理缓存中的所有条目失效
      Since:
      V2.0.1
    • cleanUpAll

      public void cleanUpAll()
      Clean up all managed caches 清理所有管理缓存
      Since:
      V2.0.1