溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot下集成緩存工具類CacheManager怎么使用

發(fā)布時(shí)間:2023-03-25 11:40:15 來源:億速云 閱讀:125 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了SpringBoot下集成緩存工具類CacheManager怎么使用的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇SpringBoot下集成緩存工具類CacheManager怎么使用文章都會(huì)有所收獲,下面我們一起來看看吧。

一.自定義工具類定義

package com.demo.utils;

import org.springframework.util.StringUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Description: 緩存工具類
 * 1.部分方法未驗(yàn)證,如有問題請自行修改
 * 2.其他方法請自行添加
 *
 * @Author: zhx & moon hongxu_1234@163.com
 * @Date: 2022-04-07 20:54
 * @version: V1.0.0
 */
public class Cache {

    /**
     * 屏蔽工具類的無參構(gòu)造 避免工具類被實(shí)例化
     */
    private Cache(){}

    /**
     * 緩存留存期 30min 1H 24H
     */
    public static final long CACHE_HOLD_TIME_30M = 30 * 60 * 1000L;
    public static final long CACHE_HOLD_TIME_1H = 2 * CACHE_HOLD_TIME_30M;
    public static final long CACHE_HOLD_TIME_24H = 24 * CACHE_HOLD_TIME_1H;
    public static final long CACHE_HOLD_TIME_FOREVER = -1L;

    /**
     * 緩存容量、最少使用容量
     */
    private static final int CACHE_MAX_CAP = 1000;
    private static final int CLEAN_LRU_CAP = 800;

    /**
     * 緩存當(dāng)前大小
     */
    private static AtomicInteger CACHE_CURRENT_SIZE = new AtomicInteger(0);

    /**
     * 緩存對象
     */
    private static final Map<String,Node> CACHE_MAP = new ConcurrentHashMap<>(CACHE_MAX_CAP);

    /**
     * 最少使用記錄
     */
    private static final List<String> LRU_LIST = new LinkedList<>();

    /**
     * 自動(dòng)清理標(biāo)志位
     */
    private static volatile boolean CLEAN_RUN_FLAG = false;

    /**
     * 默認(rèn)30MIN
     * @param key
     * @param val
     */
    public static void put(String key,Object val){
        put(key,val,CACHE_HOLD_TIME_30M);
    }

    /**
     * 添加永久緩存
     * @param key
     * @param val
     */
    public static void putForever(String key,Object val){
        put(key,val,CACHE_HOLD_TIME_FOREVER);
    }

    /**
     * 添加緩存
     * @param key
     * @param val
     * @param ttlTime
     */
    public static void put(String key,Object val,long ttlTime){
        if (!StringUtils.hasLength(key) || null == val){
            return;
        }
        checkSize();
        updateCacheLru(key);
        CACHE_MAP.put(key,new Node(val,ttlTime));
    }

    /**
     * 獲取緩存信息
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T get(String key,Class<T> clazz){
        if (!StringUtils.hasLength(key) || !CACHE_MAP.containsKey(key)){
            return null;
        }
        updateCacheLru(key);
        return (T) CACHE_MAP.get(key).getVal();
    }

    /**
     * 更新最近使用位置
     * @param key
     */
    private static void updateCacheLru(String key){
        synchronized (LRU_LIST){
            LRU_LIST.remove(key);
            LRU_LIST.add(0,key);
        }
    }

    /**
     * 刪除,成功則容量-1
     * @param key
     */
    private static boolean remove(String key){
        Node node = CACHE_MAP.remove(key);
        if (null!=node){
            CACHE_CURRENT_SIZE.getAndDecrement();
            return true;
        }
        return false;
    }

    /**
     * 檢查是否超過容量,先清理過期,在清理最少使用
     */
    private static void checkSize(){
        if (CACHE_CURRENT_SIZE.intValue() > CACHE_MAX_CAP){
            deleteTimeOut();
        }
        if (CACHE_CURRENT_SIZE.intValue() > CLEAN_LRU_CAP){
            deleteLru();
        }
    }

    /**
     * 刪除最久未使用,尾部刪除
     * 永久緩存不會(huì)被清除
     */
    private static void deleteLru(){
        synchronized (LRU_LIST){
            while (LRU_LIST.size() > CLEAN_LRU_CAP){
                int lastIndex = LRU_LIST.size() - 1;
                String key = LRU_LIST.get(lastIndex);
                if (!CACHE_MAP.get(key).isForever() && remove(key)){
                    LRU_LIST.remove(lastIndex);
                }
            }
        }
    }

    /**
     * 刪除過期
     */
    private static void deleteTimeOut(){
        List<String> del = new LinkedList<>();
        for (Map.Entry<String,Node> entry:CACHE_MAP.entrySet()){
            if (entry.getValue().isExpired()){
                del.add(entry.getKey());
            }
        }
        for (String k:del){
            remove(k);
        }
    }

    /**
     * 緩存是否已存在,過期則刪除返回False
     * @param key
     * @return
     */
    public static boolean contains(String key){
        if (CACHE_MAP.containsKey(key)){
            if (!CACHE_MAP.get(key).isExpired()){
                return true;
            }
            if (remove(key)){
                return false;
            }
            return true;
        }
        return false;
    }

    /**
     * 清空緩存
     */
    public static void clear(){
        CACHE_MAP.clear();
        CACHE_CURRENT_SIZE.set(0);
        LRU_LIST.clear();
    }

    /**
     * 重置自動(dòng)清理標(biāo)志
     * @param flag
     */
    public static void setCleanRunFlag(boolean flag){
        CLEAN_RUN_FLAG = flag;
    }

    /**
     * 自動(dòng)清理過期緩存
     */
    private static void startAutoClean(){

        if (!CLEAN_RUN_FLAG){
            setCleanRunFlag(true);
            ScheduledExecutorService scheduledExecutor = new ScheduledThreadPoolExecutor(1);
            scheduledExecutor.scheduleAtFixedRate(()->{
                try {
                    Cache.setCleanRunFlag(true);
                    while (CLEAN_RUN_FLAG){
                        Cache.deleteTimeOut();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            },10,Cache.CACHE_HOLD_TIME_1H, TimeUnit.SECONDS);
        }
    }

    /**
     * 緩存對象類
     */
    public static class Node{
        /**
         * 緩存值
         */
        private Object val;
        /**
         * 過期時(shí)間
         */
        private long ttlTime;

        public Node(Object val,long ttlTime){
            this.val = val;
            if (ttlTime<0){
                this.ttlTime = ttlTime;
            }else{
                this.ttlTime = System.currentTimeMillis() + ttlTime;
            }
        }

        public Object getVal(){
            return this.val;
        }

        public boolean isExpired(){
            if (this.ttlTime<0){
                return false;
            }
            return System.currentTimeMillis() > this.ttlTime;
        }

        public boolean isForever(){
            if (this.ttlTime<0){
                return true;
            }
            return false;
        }

    }


}

二.SpringBoot 集成開源緩存組件

1.開源緩存組件

緩存組件類型
HAZELCAST分布式緩存
INFINISPAN分布式緩存
COUCHBASE分布式緩存
REDIS分布式緩存
CAFFEINE本地緩存
CACHE2K本地緩存

隨著硬件系統(tǒng)系統(tǒng)擴(kuò)展和軟件升級,緩存在應(yīng)用中的地位和可應(yīng)用性日漸提升,SpringBoot 為此設(shè)計(jì)了一套通用緩存機(jī)制(規(guī)范)
此規(guī)范設(shè)計(jì)了兩個(gè)頂層接口 Cache 和 CacheManager 即緩存和緩存管理,通過實(shí)現(xiàn)CacheManager 引入緩存組件,即可在SpringBoot項(xiàng)目內(nèi)通過注解方便的設(shè)置緩存

通過 SpringBoot 的緩存自動(dòng)配置類,查看其可支持哪些緩存組件的使用,部分源碼如下:

//org.springframework.boot.autoconfigure.cache.CacheConfigurations
static {
    Map<CacheType, String> mappings = new EnumMap<>(CacheType.class);
    mappings.put(CacheType.GENERIC, GenericCacheConfiguration.class.getName());
    mappings.put(CacheType.HAZELCAST, HazelcastCacheConfiguration.class.getName());
    mappings.put(CacheType.INFINISPAN, InfinispanCacheConfiguration.class.getName());
    mappings.put(CacheType.JCACHE, JCacheCacheConfiguration.class.getName());
    mappings.put(CacheType.COUCHBASE, CouchbaseCacheConfiguration.class.getName());
    mappings.put(CacheType.REDIS, RedisCacheConfiguration.class.getName());
    mappings.put(CacheType.CAFFEINE, CaffeineCacheConfiguration.class.getName());
    mappings.put(CacheType.CACHE2K, Cache2kCacheConfiguration.class.getName());
    mappings.put(CacheType.SIMPLE, SimpleCacheConfiguration.class.getName());
    mappings.put(CacheType.NONE, NoOpCacheConfiguration.class.getName());
    MAPPINGS = Collections.unmodifiableMap(mappings);
}

2.緩存注解

注解功能
@EenableCacheing啟用注解式緩存的功能,一般加在項(xiàng)目啟動(dòng)類上
@Cacheable如果存在緩存,則返回緩存信息;不存在則獲取值并添加到緩存
@CachePut添加緩存,可用于更新方法(強(qiáng)制將方法返回值添加到指定Key)
@CacheEvict刪除緩存
@Caching打包操作,將上面幾種注解打包在一起作用
@CacheConfig通用配置注解,如果要對某個(gè)對象設(shè)置緩存,可以將此注解標(biāo)注在類上設(shè)置緩存名、主鍵生成器等

3.緩存測試(caffeine)

通過 SpringBoot 集成 Caffeine 進(jìn)行緩存注解演示,相關(guān)版本信息參考依賴

1.Pom依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>LenovoTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>19</maven.compiler.source>
        <maven.compiler.target>19</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>3.0.0</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
            <version>3.1.2</version>
        </dependency>
        
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.14.graal</version>
        </dependency>
    </dependencies>
</project>
2.Yml配置(指定緩存實(shí)現(xiàn)類型)
server:
  port: 8088

spring:
  cache:
   type: caffeine

custom-caffeine:
  specs:
    ## 用戶信息寫入10S后過期
    userInfo: maximumSize=10,expireAfterWrite=10s
    ## 登陸信息寫入5S后過期
    accessInfo: maximumSize=10,expireAfterWrite=5s
3.項(xiàng)目啟動(dòng)類
package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

/**
 * @author 
 * @date 
 * @since 1.8
 */
@EnableCaching
@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class,args);
    }
}
4.自定義緩存配置

如果不通過Yml指定緩存實(shí)現(xiàn)類型,則將使用默認(rèn)實(shí)現(xiàn)

package com.demo.comfig;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author 
 * @date 
 * @since 1.8
 */
@Configuration
public class CustomCaffeineConfig {

    /**
     * 加載 Caffeine 配置
     * @return
     */
    @Bean(name = "caffeineProperties")
    @ConfigurationProperties(prefix = "custom-caffeine.specs")
    public Map<String,String> caffeineProperties(){
        return new HashMap(16);
    }

    /**
     * 自定義 CacheManager
     * @param properties
     * @return
     */
    @Bean
    @Primary
    public CacheManager caffeineManager(@Qualifier("caffeineProperties") Map<String,String> properties){

        CaffeineCacheManager manager = new CaffeineCacheManager();
        Map.Entry<String,String> entry;
        Iterator<Map.Entry<String,String>> iterator = properties.entrySet().iterator();
        while (iterator.hasNext()){
            entry = iterator.next();
            manager.registerCustomCache(entry.getKey(), Caffeine.from(entry.getValue()).build());
        }
        return manager;
    }
}
5.測試類

定義一個(gè) User 對象

package com.demo.entity;

import lombok.Data;

/**
 * @author zhanghx19
 * @date 2023-01-28 15:53
 * @since 1.8
 */
@Data
public class UserInfo {
    private String name;
    private String account;
    private long age;
}

定義一個(gè) Controller 類用于測試

package com.demo.controller;

import com.demo.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 
 * @date 2023-01-28 11:36
 * @since 1.8
 */

@RestController
@RequestMapping("/test")
public class TestController {

    /**
     * 注入緩存管理器,所有注解操作也可以直接操作管理器實(shí)現(xiàn)
     */
    @Autowired
    CacheManager cacheManager;

    /**
     * CachePut 強(qiáng)制刷新緩存
     * @param id
     * @param val
     * @return
     */
    @GetMapping("/update")
    @CachePut(cacheNames = "test" ,key = "#id")
    public String update(String id,String val){
        //TODO Query Data By @{id}
        return val;
    }

	/**
     * Cacheable 查看緩存,存在則直接返回;否則查詢數(shù)據(jù),添加緩存并返回
     * @param id
     * @param val
     * @return
     */
    @GetMapping("/query")
    @Cacheable(cacheNames = "test" ,key = "#id" )
    public String query(String id,String val){
        //TODO Query Data By @{id}
        return val;
    }

    /**
     * 刪除注解內(nèi)指定的 緩存名下的 Key
     * @param id
     */
    @GetMapping("/deleteTest")
    @CacheEvict(cacheNames = "test",key = "#id")
    public void deleteTest(String id){
    }

    /**
     * 通過 cacheManager 刪除緩存
     * @param cacheNames
     * @param id
     */
    @GetMapping("/deleteByNameAndKet")
    public void deleteByNameAndKet(String cacheNames,String id){
        Cache cache = cacheManager.getCache(cacheNames);
        cache.evict(id);
    }

    /**
     * CachePut 強(qiáng)制緩存用戶信息 且10秒后過期
     * @param id
     * @param val
     * @return
     */
    @GetMapping("/updateUser")
    @CachePut(cacheNames = "userInfo" ,key = "#id")
    public String updateUser(String id,String val){
        return val;
    }

    /**
     * Cacheable 10秒內(nèi)緩存不更新,丟失后可刷新為當(dāng)前值
     * @param id
     * @param val
     * @return
     */
    @GetMapping("/queryUser")
    @Cacheable(cacheNames = "userInfo" ,key = "#id")
    public String queryUser(String id,String val){
        return val;
    }

    /**
     * 緩存對象
     * @param id
     * @param val
     * @return
     */
    @GetMapping("/queryUserById")
    @Cacheable(cacheNames = "userInfo" ,key = "#id")
    public UserInfo getUserInfo(String id,String val){
        UserInfo info = new UserInfo();
        info.setAccount(id);
        info.setName(val);
        info.setAge(System.currentTimeMillis());
        return info;
    }

}
6.測試記錄

啟動(dòng)項(xiàng)目,添加強(qiáng)制緩存

SpringBoot下集成緩存工具類CacheManager怎么使用

利用 Cacheable 嘗試刷新緩存(返回已存在值)

SpringBoot下集成緩存工具類CacheManager怎么使用

刪除緩存

SpringBoot下集成緩存工具類CacheManager怎么使用

再次利用 Cacheable 嘗試刷新緩存(上面清除后則可刷新)

SpringBoot下集成緩存工具類CacheManager怎么使用

自動(dòng)過期測試,通過 CachePut 添加用戶信息

SpringBoot下集成緩存工具類CacheManager怎么使用

嘗試用 Cacheable 刷新緩存,則 10S 后可生效

SpringBoot下集成緩存工具類CacheManager怎么使用

10 秒后

SpringBoot下集成緩存工具類CacheManager怎么使用

緩存對象信息

SpringBoot下集成緩存工具類CacheManager怎么使用

關(guān)于“SpringBoot下集成緩存工具類CacheManager怎么使用”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“SpringBoot下集成緩存工具類CacheManager怎么使用”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI