SpringBoot 下集成緩存工具類 CacheManager
一.自定義工具類定義
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)證,如有問(wèn)題請(qǐng)自行修改
* 2.其他方法請(qǐng)自行添加
*
* @Author: zhx & moon hongxu_1234@163.com
* @Date: 2022-04-07 20:54
* @version: V1.0.0
*/
public class Cache {
/**
* 屏蔽工具類的無(wú)參構(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);
/**
* 緩存對(duì)象
*/
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;
}
/**
* 檢查是否超過(guò)容量,先清理過(guò)期,在清理最少使用
*/
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);
}
}
}
}
/**
* 刪除過(guò)期
*/
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);
}
}
/**
* 緩存是否已存在,過(guò)期則刪除返回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)清理過(guò)期緩存
*/
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);
}
}
/**
* 緩存對(duì)象類
*/
public static class Node{
/**
* 緩存值
*/
private Object val;
/**
* 過(guò)期時(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ò)展和軟件升級(jí),緩存在應(yīng)用中的地位和可應(yīng)用性日漸提升,SpringBoot 為此設(shè)計(jì)了一套通用緩存機(jī)制(規(guī)范)
此規(guī)范設(shè)計(jì)了兩個(gè)頂層接口 Cache 和 CacheManager 即緩存和緩存管理,通過(guò)實(shí)現(xiàn)CacheManager 引入緩存組件,即可在SpringBoot項(xiàng)目?jī)?nèi)通過(guò)注解方便的設(shè)置緩存
通過(guò) 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 | 通用配置注解,如果要對(duì)某個(gè)對(duì)象設(shè)置緩存,可以將此注解標(biāo)注在類上設(shè)置緩存名、主鍵生成器等 |
3.緩存測(cè)試(caffeine)
通過(guò) 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后過(guò)期
userInfo: maximumSize=10,expireAfterWrite=10s
## 登陸信息寫入5S后過(guò)期
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.自定義緩存配置
如果不通過(guò)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.測(cè)試類
定義一個(gè) User 對(duì)象
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 類用于測(cè)試
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){
}
/**
* 通過(guò) 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秒后過(guò)期
* @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;
}
/**
* 緩存對(duì)象
* @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.測(cè)試記錄
啟動(dòng)項(xiàng)目,添加強(qiáng)制緩存

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

刪除緩存

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

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

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

10 秒后

緩存對(duì)象信息

到此這篇關(guān)于SpringBoot 下集成緩存工具類 CacheManager的文章就介紹到這了,更多相關(guān)Java緩存工具類Cache內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot實(shí)現(xiàn)分段上傳功能的示例代碼
這篇文章主要介紹了springboot實(shí)現(xiàn)分段上傳,包括文件上傳下載,斷點(diǎn)續(xù)傳,增量上傳功能,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
SpringBoot整合jasypt實(shí)現(xiàn)敏感信息的加密詳解
一般公司的核心業(yè)務(wù)代碼中,都會(huì)存在與數(shù)據(jù)庫(kù)、第三方通信的secret key等敏感信息,如果以明文的方式存儲(chǔ),一旦泄露,那將會(huì)給公司帶來(lái)巨大的損失。本篇文章通過(guò)講解:Springboot集成Jasypt對(duì)項(xiàng)目敏感信息進(jìn)行加密,提高系統(tǒng)的安全性2022-09-09
SpringBoot項(xiàng)目開發(fā)常用技術(shù)整合
今天給大家分享springboot項(xiàng)目開發(fā)常用技術(shù)整合,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-08-08
Java責(zé)任鏈設(shè)計(jì)模式實(shí)例分析
這篇文章主要介紹了Java責(zé)任鏈設(shè)計(jì)模式,結(jié)合實(shí)例形式詳細(xì)分析了Java責(zé)任鏈設(shè)計(jì)模式的原理與相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
springboot的yml配置文件通過(guò)db2的方式整合mysql的教程
這篇文章主要介紹了springboot的yml配置文件通過(guò)db2的方式整合mysql的教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
mybatis resultmap 如何為對(duì)象賦值的調(diào)用順序
這篇文章主要介紹了mybatis resultmap 如何為對(duì)象賦值的調(diào)用順序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

