天天看點

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();
  }
}      

繼續閱讀