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

Java中本地緩存的4種實(shí)現(xiàn)方式總結(jié)

 更新時(shí)間:2025年04月11日 10:04:52   作者:呱牛?do?IT  
這篇文章主要介紹了Java中本地緩存的4種實(shí)現(xiàn)方式,分別是基礎(chǔ)緩存實(shí)現(xiàn)、GuavaLoadingCache、SpringBoot整合Caffeine和JetCache,通過實(shí)例代碼,詳細(xì)講解了每種緩存技術(shù)的特點(diǎn)和使用方法,需要的朋友可以參考下

前言

在Java開發(fā)中,緩存技術(shù)是提高應(yīng)用性能的關(guān)鍵手段之一。

今天,我們來聊聊Java中的四種主流本地緩存技術(shù),并通過實(shí)例代碼幫助大家更好地理解和應(yīng)用這些技術(shù)。

一、基礎(chǔ)緩存實(shí)現(xiàn)

首先,我們從最基礎(chǔ)的緩存實(shí)現(xiàn)講起。

一個(gè)簡(jiǎn)單的緩存系統(tǒng)通常包括緩存實(shí)體類、添加、刪除、查詢和清除緩存的功能。

1. 緩存實(shí)體類

緩存實(shí)體類用于存儲(chǔ)緩存的鍵值對(duì)以及過期時(shí)間。

代碼如下:。

public class CacheEntity {
    private String cacheKey;
    private Object cacheValue;
    private long expireTime; // 過期時(shí)間戳

    // 構(gòu)造方法、getter和setter省略
}

2. 緩存工具類

接下來,我們實(shí)現(xiàn)一個(gè)緩存工具類,使用ConcurrentHashMap作為存儲(chǔ)結(jié)構(gòu),并通過定時(shí)任務(wù)清除過期數(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ì)通過CacheLoaderload方法加載默認(rèn)值。

三、SpringBoot整合Caffeine

Caffeine是一個(gè)高性能的Java緩存庫(kù),SpringBoot提供了與Caffeine的無縫整合。

1. 開啟緩存功能

在啟動(dòng)類上添加@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是阿里巴巴開源的一款基于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# 開啟統(tǒng)計(jì)
remote:
    default:
      type:redis
      keyConvertor:fastjson# 序列化方式
      valueEncoder:java
      valueDecoder:java
      poolConfig:
        minIdle:5
        maxIdle:20
        maxTotal:50
      host:localhost
      port: 6379

3. 使用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)方式,從手寫緩存到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)文章

  • Spring整合Springmvc的相關(guān)介紹

    Spring整合Springmvc的相關(guān)介紹

    今天小編就為大家分享一篇關(guān)于Spring整合Springmvc的相關(guān)介紹,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Arrays.sort如何實(shí)現(xiàn)降序排序

    Arrays.sort如何實(shí)現(xiàn)降序排序

    這篇文章主要介紹了Arrays.sort如何實(shí)現(xiàn)降序排序問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • JavaWeb響應(yīng)下載功能實(shí)例代碼(包含工具類)

    JavaWeb響應(yīng)下載功能實(shí)例代碼(包含工具類)

    今天通過本文給大家分享的是關(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ù)制的方法

    這篇文章主要介紹了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的問題及解決

    IDEA報(bào)錯(cuò):Process terminated的問題及解決

    這篇文章主要介紹了IDEA報(bào)錯(cuò):Process terminated的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Java中的ThreadLocal線程變量詳解

    Java中的ThreadLocal線程變量詳解

    這篇文章主要介紹了Java中的ThreadLocal線程變量詳解,ThreadLocal叫做線程變量,意思是在ThreadLocal中填充的變量屬于當(dāng)前線程,該變量對(duì)其他線程而言是隔離的,它是用來提供線程內(nèi)部的局部變量,需要的朋友可以參考下
    2024-01-01
  • 詳解rabbitmq使用springboot實(shí)現(xiàn)fanout模式

    詳解rabbitmq使用springboot實(shí)現(xiàn)fanout模式

    這篇文章主要介紹了rabbitmq使用springboot實(shí)現(xiàn)fanout模式,Fanout特點(diǎn)是發(fā)布與訂閱模式,是一種廣播機(jī)制,它是沒有路由key的模式,需要的朋友可以參考下
    2023-07-07
  • 詳解Java 集合類 List 的那些坑

    詳解Java 集合類 List 的那些坑

    這篇文章主要介紹了Java 集合類 List 的那些坑,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Java實(shí)現(xiàn)新建有返回值的線程的示例詳解

    Java實(shí)現(xiàn)新建有返回值的線程的示例詳解

    本文主要介紹了一個(gè)Java多線程的例題,題目是:使用ThreadLocal管理一號(hào)和二號(hào)線程,分別存入100元,在三號(hào)線程中使用利用一號(hào)和二號(hào)的計(jì)算結(jié)果來算出賬戶的實(shí)際金額。感興趣的可以了解一下
    2022-09-09
  • java實(shí)現(xiàn)文件重命名

    java實(shí)現(xiàn)文件重命名

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)文件重命名,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03

最新評(píng)論