Class CachedFeatureStore

java.lang.Object
cloud.opencode.base.feature.store.CachedFeatureStore
All Implemented Interfaces:
FeatureStore

public final class CachedFeatureStore extends Object implements FeatureStore
Cached Feature Store with Optional Cache Module Delegation 支持可选缓存模块委托的缓存功能存储

Wraps another FeatureStore with caching for improved performance. If the Cache module (opencode-base-cache) is available, it delegates to OpenCache for high-performance caching. Otherwise, falls back to LruFeatureStore as cache.

用缓存包装另一个 FeatureStore 以提高性能。 如果缓存模块可用,则委托给 OpenCache 进行高性能缓存。 否则降级到使用 LruFeatureStore 作为缓存。

Usage Examples | 使用示例:

// Wrap a remote store with caching
FeatureStore remoteStore = new RedisFeatureStore(redisClient);
FeatureStore cachedStore = CachedFeatureStore.wrap(remoteStore);

// With custom TTL
FeatureStore cachedStore = CachedFeatureStore.wrap(remoteStore, Duration.ofMinutes(5));

// Check if cache module is available
boolean hasCache = CachedFeatureStore.isCacheModuleAvailable();

Features | 主要功能:

  • TTL-based caching layer over any FeatureStore - 基于TTL的任意FeatureStore缓存层
  • Automatic cache expiration and refresh - 自动缓存过期和刷新

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
  • Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)
Since:
JDK 25, opencode-base-feature V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • isCacheModuleAvailable

      public static boolean isCacheModuleAvailable()
      Checks if the Cache module is available. 检查缓存模块是否可用
      Returns:
      true if Cache module is available | 如果缓存模块可用返回 true
    • wrap

      public static CachedFeatureStore wrap(FeatureStore delegate)
      Wraps a feature store with caching. 用缓存包装功能存储
      Parameters:
      delegate - the underlying store | 底层存储
      Returns:
      cached feature store | 缓存的功能存储
    • wrap

      public static CachedFeatureStore wrap(FeatureStore delegate, Duration ttl)
      Wraps a feature store with caching and custom TTL. 用缓存和自定义 TTL 包装功能存储
      Parameters:
      delegate - the underlying store | 底层存储
      ttl - the cache TTL | 缓存 TTL
      Returns:
      cached feature store | 缓存的功能存储
    • wrap

      public static CachedFeatureStore wrap(FeatureStore delegate, Duration ttl, int maxSize)
      Wraps a feature store with caching and custom settings. 用缓存和自定义设置包装功能存储
      Parameters:
      delegate - the underlying store | 底层存储
      ttl - the cache TTL | 缓存 TTL
      maxSize - the maximum cache size | 最大缓存大小
      Returns:
      cached feature store | 缓存的功能存储
    • save

      public void save(Feature feature)
      Description copied from interface: FeatureStore
      Save a feature 保存功能
      Specified by:
      save in interface FeatureStore
      Parameters:
      feature - the feature to save | 要保存的功能
    • find

      public Optional<Feature> find(String key)
      Description copied from interface: FeatureStore
      Find a feature by key 根据键查找功能
      Specified by:
      find in interface FeatureStore
      Parameters:
      key - the feature key | 功能键
      Returns:
      optional containing feature if found | 如果找到则包含功能的Optional
    • findAll

      public List<Feature> findAll()
      Description copied from interface: FeatureStore
      Find all features 查找所有功能
      Specified by:
      findAll in interface FeatureStore
      Returns:
      list of all features | 所有功能的列表
    • delete

      public boolean delete(String key)
      Description copied from interface: FeatureStore
      Delete a feature 删除功能
      Specified by:
      delete in interface FeatureStore
      Parameters:
      key - the feature key | 功能键
      Returns:
      true if deleted | 如果删除成功返回true
    • exists

      public boolean exists(String key)
      Description copied from interface: FeatureStore
      Check if a feature exists 检查功能是否存在
      Specified by:
      exists in interface FeatureStore
      Parameters:
      key - the feature key | 功能键
      Returns:
      true if exists | 如果存在返回true
    • count

      public int count()
      Description copied from interface: FeatureStore
      Get the count of features 获取功能数量
      Specified by:
      count in interface FeatureStore
      Returns:
      feature count | 功能数量
    • clear

      public void clear()
      Description copied from interface: FeatureStore
      Clear all features 清除所有功能
      Specified by:
      clear in interface FeatureStore
    • isUsingOpenCache

      public boolean isUsingOpenCache()
      Checks if this store is using OpenCache. 检查此存储是否使用 OpenCache
      Returns:
      true if using OpenCache | 如果使用 OpenCache 返回 true
    • invalidate

      public void invalidate(String key)
      Invalidates cache entry for a key. 使键的缓存条目失效
      Parameters:
      key - the key to invalidate | 要使其失效的键
    • invalidateAll

      public void invalidateAll()
      Invalidates all cache entries. 使所有缓存条目失效
    • getDelegate

      public FeatureStore getDelegate()
      Gets the underlying delegate store. 获取底层委托存储
      Returns:
      the delegate store | 委托存储