Module bus.cache

Class GuavaCache<K,V>

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

public class GuavaCache<K,V> extends Object implements CacheX<K,V>
Guava 缓存支持

基于Google Guava Cache实现的缓存接口,提供本地缓存功能。 支持设置最大容量、过期时间等配置参数,并提供批量读写操作。

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

    • GuavaCache

      public GuavaCache(long size, long expire)
      构造方法

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

      Parameters:
      size - 缓存最大容量
      expire - 过期时间(毫秒)
    • GuavaCache

      public GuavaCache(Properties properties)
      构造方法

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

      Parameters:
      properties - 配置属性
    • GuavaCache

      public GuavaCache(long size, long expire, com.google.common.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>