欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java本地緩存工具之LoadingCache的使用詳解

 更新時(shí)間:2021年12月31日 16:46:32   作者:劍客阿良_ALiang  
緩存,在我們?nèi)粘i_(kāi)發(fā)中是必不可少的一種解決性能問(wèn)題的方法。簡(jiǎn)單的說(shuō),cache?就是為了提升系統(tǒng)性能而開(kāi)辟的一塊內(nèi)存空間。本文將為大家介紹一個(gè)Java本地緩存的工具——LoadingCache,感興趣的可以了解一下

前言

在工作總常常需要用到緩存,而redis往往是首選,但是短期的數(shù)據(jù)緩存一般我們還是會(huì)用到本地緩存。本文提供一個(gè)我在工作中用到的緩存工具,該工具代碼為了演示做了一些調(diào)整。如果拿去使用的話(huà),可以考慮做成注入Bean對(duì)象,看具體需求了。

環(huán)境依賴(lài)

先添加maven依賴(lài)

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>30.1.1-jre</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

代碼

不廢話(huà),上代碼了。

package ai.guiji.csdn.tools;
 
import cn.hutool.core.thread.ThreadUtil;
import com.google.common.cache.*;
import lombok.extern.slf4j.Slf4j;
 
import java.text.MessageFormat;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.LongStream;
 
/** @Author 劍客阿良_ALiang @Date 2021/12/30 17:57 @Description: 緩存工具 */
@Slf4j
public class CacheUtils {
 
  private static LoadingCache<Long, String> cache;
 
  /**
   * 初始化緩存方法
   *
   * @param totleCount 緩存池上限
   * @param overtime 超時(shí)時(shí)間
   * @param unit 時(shí)間單位
   * @param handleNotExist 處理不存在key方法
   * @param handleRemove 移除主鍵消費(fèi)
   */
  private static void initCache(
      Integer totleCount,
      Integer overtime,
      TimeUnit unit,
      Function<Long, String> handleNotExist,
      Consumer<Long> handleRemove) {
    cache =
        CacheBuilder.newBuilder()
            // 緩存池大小
            .maximumSize(totleCount)
            // 設(shè)置時(shí)間對(duì)象沒(méi)有被讀/寫(xiě)訪(fǎng)問(wèn)則對(duì)象從內(nèi)存中刪除
            .expireAfterWrite(overtime, unit)
            // 移除監(jiān)聽(tīng)器
            .removalListener(
                new RemovalListener<Long, String>() {
                  @Override
                  public void onRemoval(RemovalNotification<Long, String> rn) {
                    handleRemove.accept(rn.getKey());
                  }
                })
            .recordStats()
            .build(
                new CacheLoader<Long, String>() {
                  @Override
                  public String load(Long aLong) throws Exception {
                    return handleNotExist.apply(aLong);
                  }
                });
    log.info("初始化緩存");
  }
 
  /**
   * 存入緩存
   *
   * @param key 鍵
   * @param value 值
   */
  public static void put(Long key, String value) {
    try {
      log.info("緩存存入:[{}]-[{}]", key, value);
      cache.put(key, value);
    } catch (Exception exception) {
      log.error("存入緩存異常", exception);
    }
  }
 
  /**
   * 批量存入緩存
   *
   * @param map 映射
   */
  public static void putMap(Map<Long, String> map) {
    try {
      log.info("批量緩存存入:[{}]", map);
      cache.putAll(map);
    } catch (Exception exception) {
      log.error("批量存入緩存異常", exception);
    }
  }
 
  /**
   * 獲取緩存
   *
   * @param key 鍵
   */
  public static String get(Long key) {
    try {
      return cache.get(key);
    } catch (Exception exception) {
      log.error("獲取緩存異常", exception);
      return null;
    }
  }
 
  /**
   * 刪除緩存
   *
   * @param key 鍵
   */
  public static void removeKey(Long key) {
    try {
      cache.invalidate(key);
    } catch (Exception exception) {
      log.error("刪除緩存異常", exception);
    }
  }
 
  /**
   * 批量刪除緩存
   *
   * @param keys 鍵
   */
  public static void removeAll(Iterable<Long> keys) {
    try {
      cache.invalidateAll(keys);
    } catch (Exception exception) {
      log.error("批量刪除緩存異常", exception);
    }
  }
 
  /** 清理緩存 */
  public static void clear() {
    try {
      cache.invalidateAll();
    } catch (Exception exception) {
      log.error("清理緩存異常", exception);
    }
  }
 
  /**
   * 獲取緩存大小
   *
   * @return 長(zhǎng)度
   */
  public static long size() {
    return cache.size();
  }
 
  public static void main(String[] args) {
    initCache(
        Integer.MAX_VALUE,
        10,
        TimeUnit.SECONDS,
        k -> {
          log.info("緩存:[{}],不存在", k);
          return "";
        },
        x -> log.info("緩存:[{}],已經(jīng)移除", x));
    System.out.println(size());
    LongStream.range(0, 10).forEach(a -> put(a, MessageFormat.format("tt-{0}", a)));
    System.out.println(cache.asMap());
    ThreadUtil.sleep(5000);
    LongStream.range(0, 10)
        .forEach(
            a -> {
              System.out.println(get(a));
              ThreadUtil.sleep(1000);
            });
    System.out.println(cache.asMap());
    ThreadUtil.sleep(10000);
    System.out.println(cache.asMap());
  }
}

代碼說(shuō)明

1、在初始化loadingCache的時(shí)候,可以添加緩存的最大數(shù)量、消逝時(shí)間、消逝或者移除監(jiān)聽(tīng)事件、不存在鍵處理等等。在上面的代碼中,我初始化緩存大小為Integer的最大值,寫(xiě)入10秒后消逝,如不存在key返回空字符串等等。

2、該類(lèi)也提供了put、putAll、get、remove、removeAll、clear、size方法,可以對(duì)緩存進(jìn)行存、取、刪、清理、大小等操作。

3、main演示方法中,先往緩存存入10個(gè)數(shù)據(jù),然后過(guò)5秒后每秒取一個(gè)數(shù)據(jù),并且打印一下緩存中的全部?jī)?nèi)容。

4、補(bǔ)充一句LoadingCache是線(xiàn)程安全的哦。

演示一下

15:31:53.495 [main] INFO ai.guiji.csdn.tools.CacheUtils - 初始化緩存
0
15:31:53.502 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存存入:[0]-[tt-0]
15:31:53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存存入:[1]-[tt-1]
15:31:53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存存入:[2]-[tt-2]
15:31:53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存存入:[3]-[tt-3]
15:31:53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存存入:[4]-[tt-4]
15:31:53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存存入:[5]-[tt-5]
15:31:53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存存入:[6]-[tt-6]
15:31:53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存存入:[7]-[tt-7]
15:31:53.509 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存存入:[8]-[tt-8]
15:31:53.509 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存存入:[9]-[tt-9]
{6=tt-6, 5=tt-5, 0=tt-0, 8=tt-8, 7=tt-7, 2=tt-2, 1=tt-1, 9=tt-9, 3=tt-3, 4=tt-4}
tt-0
tt-1
tt-2
tt-3
tt-4
15:32:03.572 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存:[5],已經(jīng)移除
15:32:03.573 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存:[6],已經(jīng)移除
15:32:03.573 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存:[5],不存在
15:32:04.581 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存:[6],不存在
15:32:05.589 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存:[0],已經(jīng)移除
15:32:05.589 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存:[7],已經(jīng)移除
15:32:05.589 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存:[8],已經(jīng)移除
15:32:05.589 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存:[7],不存在
15:32:06.589 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存:[8],不存在
15:32:07.591 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存:[1],已經(jīng)移除
15:32:07.591 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存:[2],已經(jīng)移除
15:32:07.591 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存:[9],已經(jīng)移除
15:32:07.591 [main] INFO ai.guiji.csdn.tools.CacheUtils - 緩存:[9],不存在
{6=, 5=, 8=, 7=, 9=}
{}
Process finished with exit code 0

可以看到,后面的5-9在內(nèi)存中已經(jīng)不存在對(duì)應(yīng)的值了。

總結(jié)

本文提供的工具代碼主要是為了演示,實(shí)際工作中可以按照自己的需求做調(diào)整。

到此這篇關(guān)于Java本地緩存工具之LoadingCache的使用詳解的文章就介紹到這了,更多相關(guān)Java LoadingCache本地緩存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java基礎(chǔ)之java處理ip的工具類(lèi)

    Java基礎(chǔ)之java處理ip的工具類(lèi)

    這篇文章主要介紹了Java基礎(chǔ)應(yīng)用,使用java處理ip的工具類(lèi)的相關(guān)資料,需要的朋友可以參考下
    2014-10-10
  • SpringMVC @RequestBody出現(xiàn)400 Bad Request的解決

    SpringMVC @RequestBody出現(xiàn)400 Bad Request的解決

    這篇文章主要介紹了SpringMVC @RequestBody出現(xiàn)400 Bad Request的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Java利用Jackson序列化實(shí)現(xiàn)數(shù)據(jù)脫敏詳解

    Java利用Jackson序列化實(shí)現(xiàn)數(shù)據(jù)脫敏詳解

    在項(xiàng)目中有些敏感信息不能直接展示,比如客戶(hù)手機(jī)號(hào)、身份證、車(chē)牌號(hào)等信息,展示時(shí)均需要進(jìn)行數(shù)據(jù)脫敏,防止泄露客戶(hù)隱私。本文將利用Jackson序列化實(shí)現(xiàn)數(shù)據(jù)脫敏,需要的可以參考一下
    2023-03-03
  • 經(jīng)典的Java面試題及回答集錦(基礎(chǔ)篇)

    經(jīng)典的Java面試題及回答集錦(基礎(chǔ)篇)

    本文給大家收藏整理了java面試題及回答,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2018-03-03
  • 基于MapperXML掃描的問(wèn)題

    基于MapperXML掃描的問(wèn)題

    這篇文章主要介紹了MapperXML掃描的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • resultMap如何處理復(fù)雜映射問(wèn)題

    resultMap如何處理復(fù)雜映射問(wèn)題

    這篇文章主要介紹了resultMap如何處理復(fù)雜映射問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • JAVA中阻止類(lèi)的繼承(官方和非官方)

    JAVA中阻止類(lèi)的繼承(官方和非官方)

    在面向?qū)ο蟮睦碚撝? 有一些方案要求你用一個(gè)辦法來(lái)聲明一個(gè)不可繼承的類(lèi)。一般而言,如果類(lèi)提供的功能不應(yīng)該被改變,或者更恰當(dāng)?shù)恼f(shuō),是被覆蓋(override)的時(shí)候才會(huì)出現(xiàn)這種情況。在這篇文章里,我討論在JAVA語(yǔ)言中的實(shí)現(xiàn)辦法--官方和非官方的辦法
    2014-01-01
  • java打印國(guó)際象棋棋盤(pán)的方法

    java打印國(guó)際象棋棋盤(pán)的方法

    這篇文章主要為大家詳細(xì)介紹了java打印出國(guó)際象棋棋盤(pán)的相關(guān)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • 一文詳解JDK21中虛擬線(xiàn)程

    一文詳解JDK21中虛擬線(xiàn)程

    虛擬線(xiàn)程是JDK19中引入的,JDK21正式發(fā)布,本文主要介紹了JDK21中虛擬線(xiàn)程,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • Spring Boot 直接用jar運(yùn)行項(xiàng)目的方法

    Spring Boot 直接用jar運(yùn)行項(xiàng)目的方法

    這篇文章主要介紹了Spring Boot 直接用jar運(yùn)行項(xiàng)目的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下
    2018-02-02

最新評(píng)論