Module bus.cache

Class CaffeineCache<K,V>

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

public class CaffeineCache<K,V> extends Object implements CacheX<K,V>
Caffeine 缓存支持

基于Caffeine实现的高性能本地缓存,提供接近最优的命中率。 支持设置最大容量、过期时间等配置参数,并提供批量读写操作。

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

    • CaffeineCache

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

      使用指定的大小和过期时间创建缓存实例

      Parameters:
      size - 最大缓存条目数
      expire - 过期时间(毫秒)
    • CaffeineCache

      public CaffeineCache(Properties properties)
      构造方法

      使用Properties配置创建缓存实例,支持配置最大容量、过期时间等参数

      Parameters:
      properties - 配置属性
    • CaffeineCache

      public CaffeineCache(long size, long expire, com.github.benmanes.caffeine.cache.CacheLoader<K,V> cacheLoader)
      构造函数(支持异步加载)

      使用指定的大小、过期时间和缓存加载器创建缓存实例

      Parameters:
      size - 最大缓存条目数
      expire - 过期时间(毫秒)
      cacheLoader - 缓存加载器
  • Method Details

    • read

      public V read(K key)
      从缓存中读取单个值
      Specified by:
      read in interface CacheX<K,V>
      Parameters:
      key - 键
      Returns:
      值,如果不存在则返回null
    • read

      public Map<K,V> read(Collection<K> keys)
      从缓存中批量读取值
      Specified by:
      read in interface CacheX<K,V>
      Parameters:
      keys - 键集合
      Returns:
      键值映射
    • write

      public void write(K key, V value, long expire)
      向缓存中写入单个键值对
      Specified by:
      write in interface CacheX<K,V>
      Parameters:
      key - 键
      value - 值
      expire - 过期时间(毫秒)
    • write

      public void write(Map<K,V> keyValueMap, long expire)
      向缓存中批量写入键值对
      Specified by:
      write in interface CacheX<K,V>
      Parameters:
      keyValueMap - 键值映射
      expire - 过期时间(毫秒)
    • remove

      public void remove(K... keys)
      从缓存中移除指定的键
      Specified by:
      remove in interface CacheX<K,V>
      Parameters:
      keys - 要移除的键
    • clear

      public void clear()
      清空缓存
      Specified by:
      clear in interface CacheX<K,V>
    • getStats

      public String getStats()
      获取缓存统计信息
      Returns:
      缓存统计信息
    • estimatedSize

      public long estimatedSize()
      获取缓存估算大小
      Returns:
      缓存估算大小
    • getNativeCache

      public com.github.benmanes.caffeine.cache.Cache<K,V> getNativeCache()
      获取内部缓存实例(用于高级操作)
      Returns:
      内部缓存实例