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:
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionCacheEvent(CacheEvent.EventType type, String cacheName, K key, V value, V oldValue, RemovalCause removalCause, boolean isHit, Instant timestamp) Creates an instance of aCacheEventrecord class. -
Method Summary
Modifier and TypeMethodDescriptionReturns the value of thecacheNamerecord component.static <K,V> CacheEvent <K, V> Create CLEAR event 创建 CLEAR 事件final booleanIndicates whether some other object is "equal to" this one.static <K,V> CacheEvent <K, V> evict(String cacheName, K key, V oldValue, RemovalCause cause) Create EVICT event 创建 EVICT 事件static <K,V> CacheEvent <K, V> Create EXPIRE event 创建 EXPIRE 事件static <K,V> CacheEvent <K, V> Create GET event (hit) 创建 GET 事件(命中)static <K,V> CacheEvent <K, V> Create GET event (miss) 创建 GET 事件(未命中)final inthashCode()Returns a hash code value for this object.booleanisHit()Returns the value of theisHitrecord component.booleanCheck if this is a removal event (REMOVE, EXPIRE, EVICT) 检查是否为移除事件 (REMOVE, EXPIRE, EVICT)booleanCheck if this is a write event (PUT, REMOVE, CLEAR) 检查是否为写事件 (PUT, REMOVE, CLEAR)key()Returns the value of thekeyrecord component.static <K,V> CacheEvent <K, V> Create LOAD event 创建 LOAD 事件oldValue()Returns the value of theoldValuerecord component.Get optional old value 获取可选的旧值Get optional removal cause 获取可选的移除原因Get optional value 获取可选的值static <K,V> CacheEvent <K, V> Create PUT event 创建 PUT 事件static <K,V> CacheEvent <K, V> Create PUT event with old value 创建带旧值的 PUT 事件Returns the value of theremovalCauserecord component.static <K,V> CacheEvent <K, V> Create REMOVE event 创建 REMOVE 事件Returns the value of thetimestamprecord component.final StringtoString()Returns a string representation of this record class.type()Returns the value of thetyperecord component.value()Returns the value of thevaluerecord component.
-
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 aCacheEventrecord class.- Parameters:
type- the value for thetyperecord componentcacheName- the value for thecacheNamerecord componentkey- the value for thekeyrecord componentvalue- the value for thevaluerecord componentoldValue- the value for theoldValuerecord componentremovalCause- the value for theremovalCauserecord componentisHit- the value for theisHitrecord componenttimestamp- the value for thetimestamprecord component
-
-
Method Details
-
put
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
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
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
Create GET event (miss) 创建 GET 事件(未命中)- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
cacheName- cache name | 缓存名称key- the key | 键- Returns:
- event | 事件
-
remove
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
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
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
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
Create CLEAR event 创建 CLEAR 事件- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
cacheName- cache name | 缓存名称- Returns:
- event | 事件
-
optionalOldValue
-
optionalValue
-
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
-
hashCode
-
equals
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 withObjects::equals(Object,Object); primitive components are compared with thecomparemethod from their corresponding wrapper classes. -
type
-
cacheName
-
key
-
value
-
oldValue
-
removalCause
Returns the value of theremovalCauserecord component.- Returns:
- the value of the
removalCauserecord component
-
isHit
-
timestamp
-