Record Class CacheEvent<K,V>

java.lang.Object
java.lang.Record
cloud.opencode.base.cache.event.CacheEvent<K,V>
Type Parameters:
K - key type | 键类型
V - value type | 值类型

Features | 主要功能:

  • Type-safe event representation - 类型安全事件表示
  • Multiple event types (PUT, GET, REMOVE, EXPIRE, EVICT, LOAD, CLEAR) - 多种事件类型
  • Timestamp and metadata support - 时间戳和元数据支持
  • Immutable record - 不可变记录

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
  • Null-safe: Partial (key required, value optional) - 空值安全: 部分(键必需,值可选)
Record Components:
type - the event type | 事件类型
cacheName - the cache name | 缓存名称
key - the cache key | 缓存键
value - the current value | 当前值
oldValue - the previous value | 旧值
removalCause - the removal cause, if applicable | 移除原因(如适用)
isHit - whether the access was a cache hit | 是否命中缓存
timestamp - the event timestamp | 事件时间戳

public record CacheEvent<K,V>(CacheEvent.EventType type, String cacheName, K key, V value, V oldValue, RemovalCause removalCause, boolean isHit, Instant timestamp) extends Record
Cache Event - Represents events that occur in a cache 缓存事件 - 表示缓存中发生的事件

Provides a type-safe representation of cache events including put, get, remove, expire, evict, load, and clear operations.

提供缓存事件的类型安全表示,包括放入、获取、移除、过期、淘汰、加载和清除操作。

Event Types | 事件类型:

  • PUT - Entry added or updated | 条目添加或更新
  • GET - Entry accessed (hit or miss) | 条目访问(命中或未命中)
  • REMOVE - Entry explicitly removed | 条目显式移除
  • EXPIRE - Entry expired | 条目过期
  • EVICT - Entry evicted due to size limit | 条目因容量限制被淘汰
  • LOAD - Entry loaded from loader | 条目从加载器加载
  • CLEAR - Cache cleared | 缓存清除

Usage Examples | 使用示例:

// Create put event - 创建放入事件
CacheEvent<String, User> event = CacheEvent.put("users", "user1", user);

// Create eviction event - 创建淘汰事件
CacheEvent<String, User> event = CacheEvent.evict("users", "user2", oldUser, RemovalCause.SIZE);

// Pattern matching - 模式匹配
switch (event.type()) {
    case PUT -> handlePut(event);
    case EVICT -> handleEviction(event);
    default -> log(event);
}
Since:
JDK 25, opencode-base-cache V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • CacheEvent

      public CacheEvent(CacheEvent.EventType type, String cacheName, K key, V value, V oldValue, RemovalCause removalCause, boolean isHit, Instant timestamp)
      Creates an instance of a CacheEvent record class.
      Parameters:
      type - the value for the type record component
      cacheName - the value for the cacheName record component
      key - the value for the key record component
      value - the value for the value record component
      oldValue - the value for the oldValue record component
      removalCause - the value for the removalCause record component
      isHit - the value for the isHit record component
      timestamp - the value for the timestamp record component
  • Method Details

    • put

      public static <K,V> CacheEvent<K,V> put(String cacheName, K key, V value)
      Create PUT event 创建 PUT 事件
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      cacheName - cache name | 缓存名称
      key - the key | 键
      value - the new value | 新值
      Returns:
      event | 事件
    • put

      public static <K,V> CacheEvent<K,V> put(String cacheName, K key, V value, V oldValue)
      Create PUT event with old value 创建带旧值的 PUT 事件
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      cacheName - cache name | 缓存名称
      key - the key | 键
      value - the new value | 新值
      oldValue - the old value | 旧值
      Returns:
      event | 事件
    • getHit

      public static <K,V> CacheEvent<K,V> getHit(String cacheName, K key, V value)
      Create GET event (hit) 创建 GET 事件(命中)
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      cacheName - cache name | 缓存名称
      key - the key | 键
      value - the value | 值
      Returns:
      event | 事件
    • getMiss

      public static <K,V> CacheEvent<K,V> getMiss(String cacheName, K key)
      Create GET event (miss) 创建 GET 事件(未命中)
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      cacheName - cache name | 缓存名称
      key - the key | 键
      Returns:
      event | 事件
    • remove

      public static <K,V> CacheEvent<K,V> remove(String cacheName, K key, V oldValue)
      Create REMOVE event 创建 REMOVE 事件
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      cacheName - cache name | 缓存名称
      key - the key | 键
      oldValue - the removed value | 被移除的值
      Returns:
      event | 事件
    • expire

      public static <K,V> CacheEvent<K,V> expire(String cacheName, K key, V oldValue)
      Create EXPIRE event 创建 EXPIRE 事件
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      cacheName - cache name | 缓存名称
      key - the key | 键
      oldValue - the expired value | 过期的值
      Returns:
      event | 事件
    • evict

      public static <K,V> CacheEvent<K,V> evict(String cacheName, K key, V oldValue, RemovalCause cause)
      Create EVICT event 创建 EVICT 事件
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      cacheName - cache name | 缓存名称
      key - the key | 键
      oldValue - the evicted value | 被淘汰的值
      cause - removal cause | 移除原因
      Returns:
      event | 事件
    • load

      public static <K,V> CacheEvent<K,V> load(String cacheName, K key, V value)
      Create LOAD event 创建 LOAD 事件
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      cacheName - cache name | 缓存名称
      key - the key | 键
      value - the loaded value | 加载的值
      Returns:
      event | 事件
    • clear

      public static <K,V> CacheEvent<K,V> clear(String cacheName)
      Create CLEAR event 创建 CLEAR 事件
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      cacheName - cache name | 缓存名称
      Returns:
      event | 事件
    • optionalOldValue

      public Optional<V> optionalOldValue()
      Get optional old value 获取可选的旧值
      Returns:
      optional old value | 可选的旧值
    • optionalValue

      public Optional<V> optionalValue()
      Get optional value 获取可选的值
      Returns:
      optional value | 可选的值
    • optionalRemovalCause

      public Optional<RemovalCause> optionalRemovalCause()
      Get optional removal cause 获取可选的移除原因
      Returns:
      optional removal cause | 可选的移除原因
    • isWriteEvent

      public boolean isWriteEvent()
      Check if this is a write event (PUT, REMOVE, CLEAR) 检查是否为写事件 (PUT, REMOVE, CLEAR)
      Returns:
      true if write event | 如果是写事件返回 true
    • isRemovalEvent

      public boolean isRemovalEvent()
      Check if this is a removal event (REMOVE, EXPIRE, EVICT) 检查是否为移除事件 (REMOVE, EXPIRE, EVICT)
      Returns:
      true if removal event | 如果是移除事件返回 true
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • type

      public CacheEvent.EventType type()
      Returns the value of the type record component.
      Returns:
      the value of the type record component
    • cacheName

      public String cacheName()
      Returns the value of the cacheName record component.
      Returns:
      the value of the cacheName record component
    • key

      public K key()
      Returns the value of the key record component.
      Returns:
      the value of the key record component
    • value

      public V value()
      Returns the value of the value record component.
      Returns:
      the value of the value record component
    • oldValue

      public V oldValue()
      Returns the value of the oldValue record component.
      Returns:
      the value of the oldValue record component
    • removalCause

      public RemovalCause removalCause()
      Returns the value of the removalCause record component.
      Returns:
      the value of the removalCause record component
    • isHit

      public boolean isHit()
      Returns the value of the isHit record component.
      Returns:
      the value of the isHit record component
    • timestamp

      public Instant timestamp()
      Returns the value of the timestamp record component.
      Returns:
      the value of the timestamp record component