Record Class CacheEntry<K,V>

java.lang.Object
java.lang.Record
cloud.opencode.base.cache.model.CacheEntry<K,V>
Type Parameters:
K - the type of key | 键类型
V - the type of value | 值类型
Record Components:
key - the cache key | 缓存键
value - the cache value | 缓存值
createTime - creation time in milliseconds | 创建时间(毫秒)
lastAccessTime - last access time in milliseconds | 最后访问时间(毫秒)
accessCount - number of times accessed | 访问次数
weight - weight for capacity calculation | 容量计算权重

public record CacheEntry<K,V>(K key, V value, long createTime, long lastAccessTime, long accessCount, long weight) extends Record
Cache Entry - Represents a single cache entry with metadata 缓存条目 - 表示带有元数据的单个缓存条目

Immutable record containing cache entry data and access statistics.

不可变记录,包含缓存条目数据和访问统计信息。

Features | 主要功能:

  • Key-value storage - 键值存储
  • Creation and access time tracking - 创建和访问时间跟踪
  • Access count statistics - 访问次数统计
  • Weight for capacity calculation - 容量计算权重
  • Age and idle time calculation - 存活和空闲时间计算

Usage Examples | 使用示例:

CacheEntry<String, User> entry = new CacheEntry<>(
    "user:1001", user, System.currentTimeMillis(),
    System.currentTimeMillis(), 0, 1);

// Get age - 获取存活时间
long age = entry.age();

// Get idle time - 获取空闲时间
long idle = entry.idleTime();

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
  • Null-safe: Partial (key cannot be null) - 空值安全: 部分(键不能为 null)
Since:
JDK 25, opencode-base-cache V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    CacheEntry(K key, V value)
    Create entry with default weight of 1 创建默认权重为 1 的条目
    CacheEntry(K key, V value, long weight)
    Create entry with specified weight 创建指定权重的条目
    CacheEntry(K key, V value, long createTime, long lastAccessTime, long accessCount, long weight)
    Creates an instance of a CacheEntry record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Returns the value of the accessCount record component.
    long
    age()
    Get age in milliseconds (time since creation) 获取存活时间(毫秒,自创建以来)
    long
    Returns the value of the createTime record component.
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    long
    Get idle time in milliseconds (time since last access) 获取空闲时间(毫秒,自最后访问以来)
    key()
    Returns the value of the key record component.
    long
    Returns the value of the lastAccessTime record component.
    Create new entry with updated access time and count 创建更新访问时间和次数的新条目
    final String
    Returns a string representation of this record class.
    Returns the value of the value record component.
    long
    Returns the value of the weight record component.
    withValue(V newValue)
    Create new entry with updated value 创建更新值的新条目
    withWeight(long newWeight)
    Create new entry with updated weight 创建更新权重的新条目

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • CacheEntry

      public CacheEntry(K key, V value)
      Create entry with default weight of 1 创建默认权重为 1 的条目
      Parameters:
      key - the key | 键
      value - the value | 值
    • CacheEntry

      public CacheEntry(K key, V value, long weight)
      Create entry with specified weight 创建指定权重的条目
      Parameters:
      key - the key | 键
      value - the value | 值
      weight - the weight | 权重
    • CacheEntry

      public CacheEntry(K key, V value, long createTime, long lastAccessTime, long accessCount, long weight)
      Creates an instance of a CacheEntry record class.
      Parameters:
      key - the value for the key record component
      value - the value for the value record component
      createTime - the value for the createTime record component
      lastAccessTime - the value for the lastAccessTime record component
      accessCount - the value for the accessCount record component
      weight - the value for the weight record component
  • Method Details

    • age

      public long age()
      Get age in milliseconds (time since creation) 获取存活时间(毫秒,自创建以来)
      Returns:
      age in milliseconds | 存活时间(毫秒)
    • idleTime

      public long idleTime()
      Get idle time in milliseconds (time since last access) 获取空闲时间(毫秒,自最后访问以来)
      Returns:
      idle time in milliseconds | 空闲时间(毫秒)
    • recordAccess

      public CacheEntry<K,V> recordAccess()
      Create new entry with updated access time and count 创建更新访问时间和次数的新条目
      Returns:
      new entry with updated access info | 更新后的新条目
    • withValue

      public CacheEntry<K,V> withValue(V newValue)
      Create new entry with updated value 创建更新值的新条目
      Parameters:
      newValue - the new value | 新值
      Returns:
      new entry with updated value | 更新值后的新条目
    • withWeight

      public CacheEntry<K,V> withWeight(long newWeight)
      Create new entry with updated weight 创建更新权重的新条目
      Parameters:
      newWeight - the new weight | 新权重
      Returns:
      new entry with updated weight | 更新权重后的新条目
    • 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.
    • 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
    • createTime

      public long createTime()
      Returns the value of the createTime record component.
      Returns:
      the value of the createTime record component
    • lastAccessTime

      public long lastAccessTime()
      Returns the value of the lastAccessTime record component.
      Returns:
      the value of the lastAccessTime record component
    • accessCount

      public long accessCount()
      Returns the value of the accessCount record component.
      Returns:
      the value of the accessCount record component
    • weight

      public long weight()
      Returns the value of the weight record component.
      Returns:
      the value of the weight record component