Module bus.cache

Class MemoryCache<K,V>

java.lang.Object
org.miaixz.bus.cache.metric.MemoryCache<K,V>
Type Parameters:
K - 键类型
V - 值类型
All Implemented Interfaces:
CacheX<K,V>

public class MemoryCache<K,V> extends Object implements CacheX<K,V>
内存缓存支持

基于 ConcurrentHashMap 实现的线程安全内存缓存,支持最大容量、访问后过期时间、写入后过期时间和初始容量配置。 提供定时清理过期缓存、批量读写操作和统计信息获取功能。

Since:
Java 17+
Author:
Kimi Liu
  • Field Details

    • timeout

      public static long timeout
      默认缓存过期时间:3分钟

      鉴于授权过程中,根据个人的操作习惯或授权平台(如 Google)的差异,授权流程耗时不同, 但通常不会过长。本缓存工具默认过期时间为3分钟,超过3分钟的缓存将失效并被删除。

    • schedulePrune

      public static boolean schedulePrune
      是否开启定时清理过期缓存的任务
  • Constructor Details

    • MemoryCache

      public MemoryCache()
      默认构造方法

      创建内存缓存实例,使用默认配置(最大容量 1000,写入后过期时间 3 分钟,无访问后过期时间), 如果开启了定时清理,则启动定时清理任务。 示例代码:

      
       MemoryCache<String, String> cache = new MemoryCache<>();
       cache.write("key1", "value1", timeout);
       
    • MemoryCache

      public MemoryCache(long size, long expire)
      构造函数

      使用指定的最大容量和过期时间创建缓存实例。 示例代码:

      
       MemoryCache<String, String> cache = new MemoryCache<>(1000, 600_000);
       cache.write("key1", "value1", 600_000);
       
      Parameters:
      size - 最大缓存条目数
      expire - 写入后过期时间(毫秒)
    • MemoryCache

      public MemoryCache(Properties properties)
      构造函数

      使用 Properties 配置创建缓存实例,支持最大容量、访问后过期时间、写入后过期时间和初始容量。 示例代码:

      
       Properties props = new Properties();
       props.setProperty("maximumSize", "1000");
       props.setProperty("expireAfterWrite", "600000");
       props.setProperty("expireAfterAccess", "300000");
       props.setProperty("initialCapacity", "16");
       MemoryCache<String, String> cache = new MemoryCache<>(props);
       
      Parameters:
      properties - 配置属性
  • Method Details

    • read

      public V read(K key)
      获取缓存

      从缓存中读取指定键的值,如果键不存在或已过期则返回 null。 示例代码:

      
       MemoryCache<String, String> cache = new MemoryCache<>();
       cache.write("key1", "value1", timeout);
       String value = cache.read("key1");
       System.out.println("值: " + value);
       
      Specified by:
      read in interface CacheX<K,V>
      Parameters:
      key - 缓存键
      Returns:
      缓存值,或 null 如果不存在或已过期
    • read

      public Map<K,V> read(Collection<K> keys)
      批量获取缓存

      从缓存中批量读取指定键集合的值,不存在的键对应的值为 null。 示例代码:

      
       MemoryCache<String, String> cache = new MemoryCache<>();
       cache.write("key1", "value1", timeout);
       cache.write("key2", "value2", timeout);
       Map<String, String> values = cache.read(Arrays.asList("key1", "key2"));
       System.out.println("批量值: " + values);
       
      Specified by:
      read in interface CacheX<K,V>
      Parameters:
      keys - 缓存键集合
      Returns:
      缓存键值映射
    • write

      public void write(Map<K,V> keyValueMap, long expire)
      批量设置缓存

      向缓存中批量写入键值对,超出最大容量时移除最旧的条目。 示例代码:

      
       MemoryCache<String, String> cache = new MemoryCache<>();
       Map<String, String> map = new HashMap<>();
       map.put("key1", "value1");
       map.put("key2", "value2");
       cache.write(map, timeout);
       
      Specified by:
      write in interface CacheX<K,V>
      Parameters:
      keyValueMap - 缓存键值映射
      expire - 过期时间(毫秒)
    • write

      public void write(K key, V value, long expire)
      设置缓存

      向缓存中写入单个键值对,超出最大容量时移除最旧的条目。 示例代码:

      
       MemoryCache<String, String> cache = new MemoryCache<>();
       cache.write("key1", "value1", timeout);
       System.out.println("已写入: key1");
       
      Specified by:
      write in interface CacheX<K,V>
      Parameters:
      key - 缓存键
      value - 缓存值
      expire - 过期时间(毫秒)
    • containsKey

      public boolean containsKey(K key)
      判断缓存中是否存在指定的键

      检查指定键是否存在且未过期。 示例代码:

      
       MemoryCache<String, String> cache = new MemoryCache<>();
       cache.write("key1", "value1", timeout);
       boolean exists = cache.containsKey("key1");
       System.out.println("键存在: " + exists);
       
      Specified by:
      containsKey in interface CacheX<K,V>
      Parameters:
      key - 缓存键
      Returns:
      如果存在且未过期则返回 true,否则返回 false
    • clear

      public void clear()
      清理过期的缓存

      移除所有已过期的缓存条目(包括写入后过期和访问后过期)。 示例代码:

      
       MemoryCache<String, String> cache = new MemoryCache<>();
       cache.clear();
       System.out.println("缓存已清理");
       
      Specified by:
      clear in interface CacheX<K,V>
    • remove

      public void remove(K... keys)
      移除指定的缓存

      移除指定键的缓存条目。 示例代码:

      
       MemoryCache<String, String> cache = new MemoryCache<>();
       cache.write("key1", "value1", timeout);
       cache.remove("key1");
       System.out.println("已移除: key1");
       
      Specified by:
      remove in interface CacheX<K,V>
      Parameters:
      keys - 要移除的缓存键
    • schedulePrune

      public void schedulePrune(long delay)
      定时清理过期缓存

      调度定时任务以清理过期缓存,清理间隔为过期时间。 示例代码:

      
       MemoryCache<String, String> cache = new MemoryCache<>();
       cache.schedulePrune(600_000);
       System.out.println("定时清理已调度");
       
      Parameters:
      delay - 间隔时长,单位毫秒
    • getStats

      public String getStats()
      获取缓存统计信息

      返回缓存的统计信息,包括请求次数、命中次数、命中率和当前大小。 示例代码:

      
       MemoryCache<String, String> cache = new MemoryCache<>();
       cache.read("key1");
       String stats = cache.getStats();
       System.out.println("统计信息: " + stats);
       
      Returns:
      缓存统计信息字符串
    • estimatedSize

      public long estimatedSize()
      获取缓存估算大小

      返回当前缓存中的条目数量。 示例代码:

      
       MemoryCache<String, String> cache = new MemoryCache<>();
       cache.write("key1", "value1", timeout);
       long size = cache.estimatedSize();
       System.out.println("缓存大小: " + size);
       
      Returns:
      缓存估算大小
    • getNativeCache

      public Map<K,org.miaixz.bus.cache.metric.MemoryCache.CacheState> getNativeCache()
      获取内部缓存实例

      返回底层的 ConcurrentHashMap 实例,用于高级操作。 示例代码:

      
       MemoryCache<String, String> cache = new MemoryCache<>();
       Map<String, CacheState> nativeCache = cache.getNativeCache();
       System.out.println("内部缓存: " + nativeCache);
       
      Returns:
      内部缓存实例