Class CacheMap<K,V>

java.lang.Object
java.util.AbstractMap<K,V>
tech.riemann.nif.utils.CacheMap<K,V>
Type Parameters:
K - 键类型
V - 值类型
All Implemented Interfaces:
Map<K,V>

public class CacheMap<K,V> extends AbstractMap<K,V>
缓存映射实现类,支持自动清除过期条目 这是一个基于时间的缓存容器,会自动清理超过指定超时时间的条目
Author:
Kerbores(kerbores@riemann.tech)
  • Constructor Details

    • CacheMap

      public CacheMap(long timeout)
      构造函数,创建具有指定超时时间的缓存映射
      Parameters:
      timeout - 条目在缓存中的最大存活时间(毫秒)
  • Method Details

    • getDefault

      public static CacheMap<Object,Object> getDefault()
      获取默认实例(单例模式,使用双重检查锁定)
      Returns:
      默认的CacheMap实例
    • shutdown

      public void shutdown()
      关闭缓存清理线程
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
      Specified by:
      entrySet in class AbstractMap<K,V>
    • get

      public V get(Object key)
      根据给定的键从缓存中获取值
      Specified by:
      get in interface Map<K,V>
      Overrides:
      get in class AbstractMap<K,V>
      Parameters:
      key - 要获取值的键
      Returns:
      与指定键关联的值如果键不存在,则返回null
    • put

      public V put(K key, V value)
      向缓存中添加一个键值对。
      Specified by:
      put in interface Map<K,V>
      Overrides:
      put in class AbstractMap<K,V>
      Parameters:
      key - 缓存键,用于标识缓存中的项。
      value - 缓存值,要添加到缓存中的数据。
      Returns:
      返回被替换的旧值,如果之前不存在则返回null。
    • remove

      public V remove(Object key)
      从缓存中移除指定键的条目
      Specified by:
      remove in interface Map<K,V>
      Overrides:
      remove in class AbstractMap<K,V>
      Parameters:
      key - 要移除的键
      Returns:
      被移除的值,如果键不存在则返回null
    • equals

      public boolean equals(Object o)
      判断当前映射是否与另一个对象相等
      Specified by:
      equals in interface Map<K,V>
      Overrides:
      equals in class AbstractMap<K,V>
      Parameters:
      o - 待比较的对象
      Returns:
      如果两个映射包含相同的键值对则返回true,否则返回false
    • hashCode

      public int hashCode()
      计算并返回当前映射的哈希码
      Specified by:
      hashCode in interface Map<K,V>
      Overrides:
      hashCode in class AbstractMap<K,V>
      Returns:
      映射的哈希码
    • size

      public int size()
      获取缓存中的条目数量
      Specified by:
      size in interface Map<K,V>
      Overrides:
      size in class AbstractMap<K,V>
      Returns:
      缓存中键值对的数量
    • containsKey

      public boolean containsKey(Object key)
      检查缓存是否包含指定的键
      Specified by:
      containsKey in interface Map<K,V>
      Overrides:
      containsKey in class AbstractMap<K,V>
      Parameters:
      key - 待检查的键
      Returns:
      如果缓存中包含该键则返回true,否则返回false