java.lang.Object
org.miaixz.bus.cache.metric.MemoryCache<K,V>
- Type Parameters:
K- 键类型V- 值类型
- All Implemented Interfaces:
CacheX<K,V>
内存缓存支持
基于 ConcurrentHashMap 实现的线程安全内存缓存,支持最大容量、访问后过期时间、写入后过期时间和初始容量配置。 提供定时清理过期缓存、批量读写操作和统计信息获取功能。
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic boolean是否开启定时清理过期缓存的任务static long默认缓存过期时间:3分钟 -
Constructor Summary
ConstructorsConstructorDescription默认构造方法MemoryCache(long size, long expire) 构造函数MemoryCache(Properties properties) 构造函数 -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()清理过期的缓存booleancontainsKey(K key) 判断缓存中是否存在指定的键long获取缓存估算大小获取内部缓存实例getStats()获取缓存统计信息read(Collection<K> keys) 批量获取缓存获取缓存void移除指定的缓存voidschedulePrune(long delay) 定时清理过期缓存void批量设置缓存void设置缓存
-
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
构造函数使用 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
获取缓存从缓存中读取指定键的值,如果键不存在或已过期则返回 null。 示例代码:
MemoryCache<String, String> cache = new MemoryCache<>(); cache.write("key1", "value1", timeout); String value = cache.read("key1"); System.out.println("值: " + value); -
read
批量获取缓存从缓存中批量读取指定键集合的值,不存在的键对应的值为 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); -
write
批量设置缓存向缓存中批量写入键值对,超出最大容量时移除最旧的条目。 示例代码:
MemoryCache<String, String> cache = new MemoryCache<>(); Map<String, String> map = new HashMap<>(); map.put("key1", "value1"); map.put("key2", "value2"); cache.write(map, timeout); -
write
设置缓存向缓存中写入单个键值对,超出最大容量时移除最旧的条目。 示例代码:
MemoryCache<String, String> cache = new MemoryCache<>(); cache.write("key1", "value1", timeout); System.out.println("已写入: key1"); -
containsKey
判断缓存中是否存在指定的键检查指定键是否存在且未过期。 示例代码:
MemoryCache<String, String> cache = new MemoryCache<>(); cache.write("key1", "value1", timeout); boolean exists = cache.containsKey("key1"); System.out.println("键存在: " + exists);- Specified by:
containsKeyin interfaceCacheX<K,V> - Parameters:
key- 缓存键- Returns:
- 如果存在且未过期则返回 true,否则返回 false
-
clear
public void clear()清理过期的缓存移除所有已过期的缓存条目(包括写入后过期和访问后过期)。 示例代码:
MemoryCache<String, String> cache = new MemoryCache<>(); cache.clear(); System.out.println("缓存已清理"); -
remove
移除指定的缓存移除指定键的缓存条目。 示例代码:
MemoryCache<String, String> cache = new MemoryCache<>(); cache.write("key1", "value1", timeout); cache.remove("key1"); System.out.println("已移除: key1"); -
schedulePrune
public void schedulePrune(long delay) 定时清理过期缓存调度定时任务以清理过期缓存,清理间隔为过期时间。 示例代码:
MemoryCache<String, String> cache = new MemoryCache<>(); cache.schedulePrune(600_000); System.out.println("定时清理已调度");- Parameters:
delay- 间隔时长,单位毫秒
-
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
获取内部缓存实例返回底层的 ConcurrentHashMap 实例,用于高级操作。 示例代码:
MemoryCache<String, String> cache = new MemoryCache<>(); Map<String, CacheState> nativeCache = cache.getNativeCache(); System.out.println("内部缓存: " + nativeCache);- Returns:
- 内部缓存实例
-