Java中本地緩存的4種實(shí)現(xiàn)方式總結(jié)
前言
在Java開(kāi)發(fā)中,緩存技術(shù)是提高應(yīng)用性能的關(guān)鍵手段之一。
今天,我們來(lái)聊聊Java中的四種主流本地緩存技術(shù),并通過(guò)實(shí)例代碼幫助大家更好地理解和應(yīng)用這些技術(shù)。
一、基礎(chǔ)緩存實(shí)現(xiàn)
首先,我們從最基礎(chǔ)的緩存實(shí)現(xiàn)講起。
一個(gè)簡(jiǎn)單的緩存系統(tǒng)通常包括緩存實(shí)體類(lèi)、添加、刪除、查詢和清除緩存的功能。
1. 緩存實(shí)體類(lèi)
緩存實(shí)體類(lèi)用于存儲(chǔ)緩存的鍵值對(duì)以及過(guò)期時(shí)間。
代碼如下:。
public class CacheEntity {
private String cacheKey;
private Object cacheValue;
private long expireTime; // 過(guò)期時(shí)間戳
// 構(gòu)造方法、getter和setter省略
}2. 緩存工具類(lèi)
接下來(lái),我們實(shí)現(xiàn)一個(gè)緩存工具類(lèi),使用ConcurrentHashMap作為存儲(chǔ)結(jié)構(gòu),并通過(guò)定時(shí)任務(wù)清除過(guò)期數(shù)據(jù)。
import java.util.concurrent.*;
import java.util.Map;
publicclassCacheUtil {
privatefinalstatic Map<String, CacheEntity> CACHE_MAP = newConcurrentHashMap<>();
privatestaticScheduledExecutorServiceexecutorService= Executors.newSingleThreadScheduledExecutor();
static {
executorService.scheduleAtFixedRate(() -> {
longcurrentTime= System.currentTimeMillis();
CACHE_MAP.values().removeIf(entity -> entity.getExpireTime() < currentTime);
}, 0, 500, TimeUnit.MILLISECONDS);
}
publicstaticvoidput(String key, Object value, long expireTimeInSeconds) {
longexpireTime= System.currentTimeMillis() + expireTimeInSeconds * 1000;
CACHE_MAP.put(key, newCacheEntity(key, value, expireTime));
}
publicstatic Object get(String key) {
CacheEntityentity= CACHE_MAP.get(key);
if (entity == null || entity.getExpireTime() < System.currentTimeMillis()) {
CACHE_MAP.remove(key);
returnnull;
}
return entity.getCacheValue();
}
publicstaticvoiddelete(String key) {
CACHE_MAP.remove(key);
}
publicstaticvoidclear() {
CACHE_MAP.clear();
}
}測(cè)試代碼:
public class Test {
public static void main(String[] args) throws InterruptedException {
CacheUtil.put("name", "zzc", 10L);
System.out.println("第一次查詢結(jié)果:" + CacheUtil.get("name"));
Thread.sleep(2000L);
System.out.println("第二次查詢結(jié)果:" + CacheUtil.get("name"));
}
}二、Guava LoadingCache
Guava是Google提供的一個(gè)Java基礎(chǔ)庫(kù),其中的LoadingCache是一個(gè)強(qiáng)大的緩存工具。
1. 使用示例
import com.google.common.cache.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
publicclassTest {
publicstaticvoidmain(String[] args)throws ExecutionException {
LoadingCache<String, String> cache = CacheBuilder.newBuilder()
.concurrencyLevel(8)
.expireAfterWrite(10, TimeUnit.SECONDS)
.build(newCacheLoader<String, String>() {
@Override
public String load(String key)throws Exception {
return"default_value";
}
});
cache.put("name", "zzc");
StringnameValue= cache.get("name");
StringageValue= cache.get("age", () -> "default_age");
StringsexValue= cache.get("sex", () -> "key 不存在");
System.out.println("nameValue: " + nameValue);
System.out.println("ageValue: " + ageValue);
System.out.println("sexValue: " + sexValue);
}
}在上面的代碼中,當(dāng)調(diào)用cache.get(key)方法時(shí),如果緩存中不存在對(duì)應(yīng)的key,則會(huì)通過(guò)CacheLoader的load方法加載默認(rèn)值。
三、SpringBoot整合Caffeine
Caffeine是一個(gè)高性能的Java緩存庫(kù),SpringBoot提供了與Caffeine的無(wú)縫整合。
1. 開(kāi)啟緩存功能
在啟動(dòng)類(lèi)上添加@EnableCaching注解。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@EnableCaching
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}2. 配置緩存管理器
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
@Configuration
@EnableCaching
publicclassCacheConfig {
@Bean("caffeineCacheManager")
public CacheManager cacheManager() {
CaffeineCacheManagercacheManager=newCaffeineCacheManager("userCache");
cacheManager.getCache("userCache").getConfig().setCaffeine(caffeineCacheBuilder());
return cacheManager;
}
Caffeine<Object, Object> caffeineCacheBuilder() {
return Caffeine.newBuilder()
.expireAfterWrite(10, TimeUnit.SECONDS)
.maximumSize(100);
}
}3. 使用緩存注解
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
publicclassUserService {
// 模擬數(shù)據(jù)庫(kù)操作
private Map<Integer, User> userMap = newHashMap<>();
@Cacheable(value = "userCache", key = "#id")
public User getUserById(Integer id) {
// 假設(shè)從數(shù)據(jù)庫(kù)獲取用戶數(shù)據(jù)
Useruser=newUser();
user.setId(id);
user.setName("User" + id);
userMap.put(id, user);
return user;
}
@CacheEvict(value = "userCache", key = "#id")
publicvoiddeleteUserById(Integer id) {
userMap.remove(id);
}
}四、JetCache——阿里巴巴的分布式緩存框架
JetCache是阿里巴巴開(kāi)源的一款基于Spring和Redis的分布式緩存框架,提供了強(qiáng)大的緩存抽象和注解支持。
1. 引入依賴
在pom.xml中添加JetCache依賴。
<dependency>
<groupId>com.alicp.jetcache</groupId>
<artifactId>jetcache-starter-redis</artifactId>
<version>最新版本號(hào)</version>
</dependency>2. 配置JetCache
在application.yml中配置JetCache。
jetcache:
stat:enable# 開(kāi)啟統(tǒng)計(jì)
remote:
default:
type:redis
keyConvertor:fastjson# 序列化方式
valueEncoder:java
valueDecoder:java
poolConfig:
minIdle:5
maxIdle:20
maxTotal:50
host:localhost
port: 63793. 使用JetCache注解
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.alicp.jetcache.anno.CacheUpdate;
import com.alicp.jetcache.anno.CacheInvalidate;
import org.springframework.stereotype.Service;
@Service
publicclassUserService {
@Cached(name = "userCache", key = "#id", cacheType = CacheType.BOTH)
public String getUser(int id) {
return"用戶:" + id;
}
@CacheUpdate(name = "userCache", key = "#id", value = "#user")
publicvoidupdateUser(int id, String user) {
System.out.println("更新用戶:" + user);
}
@CacheInvalidate(name = "userCache", key = "#id")
publicvoiddeleteUser(int id) {
System.out.println("刪除用戶:" + id);
}
}JetCache支持本地緩存和遠(yuǎn)程緩存的組合,非常適合分布式系統(tǒng)。
總結(jié)
今天我們一起探索了Java本地緩存的多種實(shí)現(xiàn)方式,從手寫(xiě)緩存到Guava Cache、Caffeine、Ehcache和JetCache。每種方式都有自己的特點(diǎn)和適用場(chǎng)景。
到此這篇關(guān)于Java中本地緩存的4種實(shí)現(xiàn)方式的文章就介紹到這了,更多相關(guān)Java本地緩存實(shí)現(xiàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Arrays.sort如何實(shí)現(xiàn)降序排序
這篇文章主要介紹了Arrays.sort如何實(shí)現(xiàn)降序排序問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
JavaWeb響應(yīng)下載功能實(shí)例代碼(包含工具類(lèi))
今天通過(guò)本文給大家分享的是關(guān)于javaweb的響應(yīng)(response)下載功能,需要的朋友參考下吧2017-07-07
java實(shí)現(xiàn)兩臺(tái)服務(wù)器間文件復(fù)制的方法
這篇文章主要介紹了java實(shí)現(xiàn)兩臺(tái)服務(wù)器間文件復(fù)制的方法,是對(duì)單臺(tái)服務(wù)器上文件復(fù)制功能的升級(jí)與改進(jìn),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01
IDEA報(bào)錯(cuò):Process terminated的問(wèn)題及解決
這篇文章主要介紹了IDEA報(bào)錯(cuò):Process terminated的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
詳解rabbitmq使用springboot實(shí)現(xiàn)fanout模式
這篇文章主要介紹了rabbitmq使用springboot實(shí)現(xiàn)fanout模式,Fanout特點(diǎn)是發(fā)布與訂閱模式,是一種廣播機(jī)制,它是沒(méi)有路由key的模式,需要的朋友可以參考下2023-07-07
Java實(shí)現(xiàn)新建有返回值的線程的示例詳解
本文主要介紹了一個(gè)Java多線程的例題,題目是:使用ThreadLocal管理一號(hào)和二號(hào)線程,分別存入100元,在三號(hào)線程中使用利用一號(hào)和二號(hào)的計(jì)算結(jié)果來(lái)算出賬戶的實(shí)際金額。感興趣的可以了解一下2022-09-09

