Hutool - Cache 缓存

简介: Hutool - Cache 缓存

缓存种类


FIFOCache

先入先出缓存,当缓存满了就把最先进入缓存的元素清除

LFUCache

最少使用率缓存,当缓存满了就移除使用次数最少的N个元素

LRUCache

最近最久未使用缓存,当缓存满了就移除最久未使用的元素

TimedCache

定时缓存,对象只有在过期后才会被移除

NoCache

无缓存,用于快速关闭缓存

Demo


import com.xiaoleilu.hutool.DateUtil;
import com.xiaoleilu.hutool.cache.FIFOCache;
import com.xiaoleilu.hutool.cache.LFUCache;
import com.xiaoleilu.hutool.cache.LRUCache;
import com.xiaoleilu.hutool.cache.TimedCache;
/**
 * 缓存使用Demo
 * @author Looly
 *
 */
public class CacheDemo {
  public static <V> void main(String[] args) throws InterruptedException {
    FIFOCache<String,String> fifoCache = new FIFOCache<String, String>(3, 0);
    fifoCache.put("key1", "value1", DateUtil.SECOND_MS * 3);
    fifoCache.put("key2", "value2", DateUtil.SECOND_MS * 3);
    fifoCache.put("key3", "value3", DateUtil.SECOND_MS * 3);
    fifoCache.put("key4", "value4", DateUtil.SECOND_MS * 3);
    //由于缓存容量只有3,当加入第四个元素的时候,根据FIFO规则,最先放入的对象将被移除,于是
    for (String value : fifoCache) {
      System.out.println(value);
    }
    System.out.println("----------------------------------------------------");
    LFUCache<String, String> lfuCache = new LFUCache<String, String>(3);
    lfuCache.put("key1", "value1", DateUtil.SECOND_MS * 3);
    lfuCache.get("key1");//使用次数+1
    lfuCache.put("key2", "value2", DateUtil.SECOND_MS * 3);
    lfuCache.put("key3", "value3", DateUtil.SECOND_MS * 3);
    lfuCache.put("key4", "value4", DateUtil.SECOND_MS * 3);
    //由于缓存容量只有3,当加入第四个元素的时候,根据LRU规则,最少使用的将被移除(2,3被移除)
    for (String value : lfuCache) {
      System.out.println(value);
    }
    System.out.println("------------------------------------------------------");
    LRUCache<String, String> lruCache = new LRUCache<String, String>(3);
    lruCache.put("key1", "value1", DateUtil.SECOND_MS * 3);
    lruCache.put("key2", "value2", DateUtil.SECOND_MS * 3);
    lruCache.put("key3", "value3", DateUtil.SECOND_MS * 3);
    lruCache.get("key1");//使用时间推近
    lruCache.put("key4", "value4", DateUtil.SECOND_MS * 3);
    //由于缓存容量只有3,当加入第四个元素的时候,根据LRU规则,最少使用的将被移除(2被移除)
    for (String value : lruCache) {
      System.out.println(value);
    }
    System.out.println("----------------------------------------------------");
    //设置了每个元素的超时时间是3秒,当4秒后此对象便被移除了
    System.out.println("Before expire: " + fifoCache.get("key1"));
    System.out.println("Sleep 4s...");
    Thread.sleep(DateUtil.SECOND_MS * 4);
    System.out.println("After expire: " + fifoCache.get("key1"));
    System.out.println("----------------------------------------------------");
    TimedCache<String, String> timedCache = new TimedCache<String, String>(DateUtil.SECOND_MS * 3);
    timedCache.put("key1", "value1", DateUtil.SECOND_MS * 3);
    timedCache.put("key2", "value2", DateUtil.SECOND_MS * 100);
    timedCache.put("key3", "value3", DateUtil.SECOND_MS * 3);
    timedCache.put("key4", "value4", DateUtil.SECOND_MS * 3);
    //启动定时任务,每4秒检查一次过期
    timedCache.schedulePrune(DateUtil.SECOND_MS * 3);
    System.out.println("Sleep 4s...");
    Thread.sleep(DateUtil.SECOND_MS * 4);
    //四秒后由于value2设置了100秒过期,其他设置了三秒过期,因此只有value2被保留下来
    for (String value : timedCache) {
      System.out.println(value);
    }
    //取消定时清理
    timedCache.cancelPruneSchedule();
  }
}
目录
相关文章
|
3月前
|
缓存 监控 Linux
Linux系统清理缓存(buff/cache)的有效方法。
总结而言,在大多数情形下你不必担心Linux中buffer与cache占用过多内存在影响到其他程序运行;因为当程序请求更多内存在没有足够可用资源时,Linux会自行调整其占有量。只有当你明确知道当前环境与需求并希望立即回收这部分资源给即将运行重负载任务之前才考虑上述方法去主动干预。
1491 10
|
4月前
|
存储 缓存 NoSQL
Spring Cache缓存框架
Spring Cache是Spring体系下的标准化缓存框架,支持多种缓存(如Redis、EhCache、Caffeine),可独立或组合使用。其优势包括平滑迁移、注解与编程两种使用方式,以及高度解耦和灵活管理。通过动态代理实现缓存操作,适用于不同业务场景。
418 0
|
存储 缓存 NoSQL
【Azure Redis 缓存】关于Azure Cache for Redis 服务在传输和存储键值对(Key/Value)的加密问题
【Azure Redis 缓存】关于Azure Cache for Redis 服务在传输和存储键值对(Key/Value)的加密问题
211 2
|
缓存 弹性计算 NoSQL
【Azure Redis 缓存 Azure Cache For Redis】Redis连接池
【Azure Redis 缓存 Azure Cache For Redis】Redis连接池
132 0
|
缓存 NoSQL Redis
【Azure Redis 缓存】Azure Cache for Redis 服务的导出RDB文件无法在自建的Redis服务中导入
【Azure Redis 缓存】Azure Cache for Redis 服务的导出RDB文件无法在自建的Redis服务中导入
118 0
|
缓存 开发框架 NoSQL
【Azure Redis 缓存】VM 里的 Redis 能直接迁移到 Azure Cache for Redis ? 需要改动代码吗?
【Azure Redis 缓存】VM 里的 Redis 能直接迁移到 Azure Cache for Redis ? 需要改动代码吗?
144 0
|
缓存 NoSQL Unix
【Azure Redis 缓存】Azure Cache for Redis 中如何快速查看慢指令情况(Slowlogs)
【Azure Redis 缓存】Azure Cache for Redis 中如何快速查看慢指令情况(Slowlogs)
114 0
|
缓存 NoSQL Redis
【Azure Redis 缓存】Azure Cache for Redis 是否记录具体读/写(Get/Set)或删除(Del)了哪些key呢?
【Azure Redis 缓存】Azure Cache for Redis 是否记录具体读/写(Get/Set)或删除(Del)了哪些key呢?
125 0
|
存储 缓存 NoSQL
【Azure Redis 缓存】Azure Cache for Redis 专用终结点, 虚拟网络, 公网访问链路
【Azure Redis 缓存】Azure Cache for Redis 专用终结点, 虚拟网络, 公网访问链路
128 0
|
存储 缓存 NoSQL
【Azure Redis 缓存】Azure Cache for Redis服务中,除开放端口6379,6380外,对13000,13001,15000,15001 为什么也是开放的呢?
【Azure Redis 缓存】Azure Cache for Redis服务中,除开放端口6379,6380外,对13000,13001,15000,15001 为什么也是开放的呢?
233 0