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)文章
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ù)脫敏詳解
在項(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ǔ)篇)
本文給大家收藏整理了java面試題及回答,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-03-03
Spring Boot 直接用jar運(yùn)行項(xiàng)目的方法
這篇文章主要介紹了Spring Boot 直接用jar運(yùn)行項(xiàng)目的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2018-02-02

