Hutool Hutool
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
  • 快速入门

  • 核心(hutool-core)

    • AI(Hutool-ai)

    • 注解(annotation)

    • 数组(array)

    • JavaBean(bean)

    • 缓存(cache)

      • 概述
      • 缓存工具-CacheUtil
        • 先入先出-FIFOCache
        • 文件缓存-FileCache
        • 最少使用-LFUCache
        • 最近最久未使用-LRUCache
        • 超时-TimedCache
        • 弱引用-WeakCache
      • 类加载器(classloader)

      • 编码解码(codec)

      • 集合(collection)

      • 比较器(comparator)

      • 压缩(compress)

      • 类型转换(convert)

      • 数据(data)

      • 日期时间(date)

      • 异常(exception)

      • 函数(func)

      • IO流(io)

      • 语言特性(lang)

      • Map(map)

      • 数字数学(math)

      • 网络(net)

      • 对象池(pool)

      • 反射(reflect)

      • 正则(regex)

      • 服务提供(spi)

      • 聚合操作(stream)

      • 字符串文本(text)

      • 并发和线程(thread)

      • 树结构(tree)

      • 工具集合(util)

      • XML操作(xml)

    • 定时任务(hutool-cron)

    • 加密(hutool-crypto)

    • 数据库(hutool-db)

    • 扩展(hutool-extra)

    • HTTP(hutool-http)

    • 日志(hutool-log)

    • 配置文件(hutool-setting)

    • JSON(hutool-json)

    • Office办公(hutool-poi)

    • 套接字(hutool-socket)

    • GUI(hutool-swing)

    • 指南
    • 核心(hutool-core)
    • 缓存(cache)
    Hutool
    2026-01-03
    目录

    缓存工具-CacheUtil

    # 介绍

    CacheUtil是缓存创建的快捷工具类。用于快速创建不同的缓存对象。

    # 方法

    # 常用方法

    • newFIFOCache 创建FIFO(first in first out) 先进先出缓存
    • newLFUCache 创建LFU(least frequently used) 最少使用率缓存
    • newLRUCache 创建LRU (least recently used)最近最久未使用缓存
    • newTimedCache 创建定时缓存
    • newWeakCache 创建弱引用缓存
    • newNoCache 创建无缓存实现

    # 示范

    public void fifoCacheTest(){
        final Cache<String,String> fifoCache = CacheUtil.newFIFOCache(3);
        fifoCache.setListener((key, value)->{
            // 监听测试,此测试中只有key1被移除,测试是否监听成功
            Assertions.assertEquals("key1", key);
            Assertions.assertEquals("value1", value);
        });
    
        fifoCache.put("key1", "value1", DateUnit.SECOND.getMillis() * 3);
        fifoCache.put("key2", "value2", DateUnit.SECOND.getMillis() * 3);
        fifoCache.put("key3", "value3", DateUnit.SECOND.getMillis() * 3);
        fifoCache.put("key4", "value4", DateUnit.SECOND.getMillis() * 3);
    
        //由于缓存容量只有3,当加入第四个元素的时候,根据FIFO规则,最先放入的对象将被移除
        final String value1 = fifoCache.get("key1");
        Assertions.assertNull(value1);
    }
    
    概述
    先入先出-FIFOCache

    ← 概述 先入先出-FIFOCache→

    Theme by Vdoing | Copyright © 2025-2026 Hutool | Apache-2.0
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式