Class AccessPatternAnalyzer<K>

java.lang.Object
cloud.opencode.base.cache.analysis.AccessPatternAnalyzer<K>
Type Parameters:
K - key type | 键类型

Security | 安全性:

  • Thread-safe: Yes (uses ConcurrentHashMap and atomic counters) - 线程安全: 是(使用 ConcurrentHashMap 和原子计数器)
  • Null-safe: Yes - 空值安全: 是

public class AccessPatternAnalyzer<K> extends Object
Access Pattern Analyzer - Analyzes cache access patterns 访问模式分析器 - 分析缓存访问模式

Tracks and analyzes cache access patterns to identify hot keys, cold data, and optimize cache performance.

跟踪和分析缓存访问模式以识别热点键、冷数据,并优化缓存性能。

Features | 主要功能:

  • Hot key detection - 热点键检测
  • Cold data identification - 冷数据识别
  • Access frequency tracking - 访问频率跟踪
  • Temporal pattern analysis - 时间模式分析
  • Top-K tracking - Top-K 跟踪

Usage Examples | 使用示例:

// Create analyzer
AccessPatternAnalyzer<String> analyzer = AccessPatternAnalyzer.<String>builder()
    .hotKeyThreshold(100)     // 100 accesses = hot
    .coldDataAge(Duration.ofHours(1))  // 1 hour = cold
    .topKSize(10)             // Track top 10
    .build();

// Record accesses
analyzer.recordAccess("user:1001");
analyzer.recordAccess("user:1002");

// Get hot keys
Set<String> hotKeys = analyzer.getHotKeys();

// Get cold keys
Set<String> coldKeys = analyzer.getColdKeys();

// Get analysis report
AccessPatternReport report = analyzer.analyze();
Since:
JDK 25, opencode-base-cache V2.0.5
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • builder

      public static <K> AccessPatternAnalyzer.Builder<K> builder()
      Create a builder 创建构建器
      Type Parameters:
      K - key type | 键类型
      Returns:
      builder | 构建器
    • create

      public static <K> AccessPatternAnalyzer<K> create()
      Create with default settings 使用默认设置创建
      Type Parameters:
      K - key type | 键类型
      Returns:
      analyzer | 分析器
    • recordAccess

      public void recordAccess(K key)
      Record an access to a key 记录键的访问
      Parameters:
      key - the key | 键
    • recordAccess

      public void recordAccess(K key, long count)
      Record multiple accesses to a key 记录键的多次访问
      Parameters:
      key - the key | 键
      count - access count | 访问次数
    • recordMiss

      public void recordMiss(K key)
      Record a miss (access but not found) 记录未命中(访问但未找到)
      Parameters:
      key - the key | 键
    • getHotKeys

      public Set<K> getHotKeys()
      Get hot keys (frequently accessed) 获取热点键(频繁访问)
      Returns:
      set of hot keys | 热点键集合
    • getColdKeys

      public Set<K> getColdKeys()
      Get cold keys (not accessed recently) 获取冷数据键(最近未访问)
      Returns:
      set of cold keys | 冷数据键集合
    • getTopK

      Get top K most accessed keys 获取访问最多的 Top K 键
      Returns:
      list of top K keys with counts | Top K 键及其计数列表
    • getTopK

      public List<AccessPatternAnalyzer.KeyAccessCount<K>> getTopK(int k)
      Get top K most accessed keys 获取访问最多的 Top K 键
      Parameters:
      k - number of keys to return | 要返回的键数量
      Returns:
      list of top K keys with counts | Top K 键及其计数列表
    • getBottomK

      public List<AccessPatternAnalyzer.KeyAccessCount<K>> getBottomK(int k)
      Get bottom K least accessed keys 获取访问最少的 Bottom K 键
      Parameters:
      k - number of keys to return | 要返回的键数量
      Returns:
      list of bottom K keys with counts | Bottom K 键及其计数列表
    • getAccessCount

      public long getAccessCount(K key)
      Get access count for a specific key 获取特定键的访问计数
      Parameters:
      key - the key | 键
      Returns:
      access count | 访问计数
    • getMissCount

      public long getMissCount(K key)
      Get miss count for a specific key 获取特定键的未命中计数
      Parameters:
      key - the key | 键
      Returns:
      miss count | 未命中计数
    • getLastAccess

      public Instant getLastAccess(K key)
      Get last access time for a key 获取键的最后访问时间
      Parameters:
      key - the key | 键
      Returns:
      last access instant or null | 最后访问时间或 null
    • analyze

      Generate full analysis report 生成完整分析报告
      Returns:
      analysis report | 分析报告
    • remove

      public void remove(K key)
      Remove tracking for a key 移除键的跟踪
      Parameters:
      key - the key | 键
    • clear

      public void clear()
      Clear all tracking data 清除所有跟踪数据
    • pruneCold

      public int pruneCold(Duration age)
      Prune cold entries older than specified age 修剪超过指定年龄的冷条目
      Parameters:
      age - maximum age | 最大年龄
      Returns:
      number of entries pruned | 修剪的条目数
    • size

      public int size()
      Get tracked key count 获取跟踪的键数量
      Returns:
      key count | 键数量