发布于 2016-10-24 14:08:46 | 353 次阅读 | 评论: 0 | 来源: PHPERZ

在Jodd中有专门的缓存封装。于是一边学习一边借鉴过来,注释文档已全部换成中文并做了简单修改。在此写个Demo说明各种类型的cache使用方法。

缓存种类

FIFOCache

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

LFUCache

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

LRUCache

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

TimedCache

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

NoCache

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

Demo

package com.xiaoleilu.hutool.demo.cache;

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();
  }
}
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务