網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
我的項(xiàng)目版本號(hào)如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
首先選一個(gè)緩存框架(EhCache )EhCache 是一個(gè)廣泛使用的開(kāi)源 Java 分布式緩存庫(kù),主要用于提高應(yīng)用程序的性能,減少數(shù)據(jù)庫(kù)訪問(wèn)次數(shù),通過(guò)緩存頻繁讀取的數(shù)據(jù)來(lái)實(shí)現(xiàn)。它可以作為 Hibernate、Spring、MyBatis 等框架的緩存提供者,用于提升這些框架在數(shù)據(jù)處理方面的性能。可以緩存來(lái)自數(shù)據(jù)庫(kù)的數(shù)據(jù),當(dāng)應(yīng)用程序需要這些數(shù)據(jù)時(shí),可以直接從緩存中讀取,而不是每次都查詢數(shù)據(jù)庫(kù)。這減少了數(shù)據(jù)庫(kù)的訪問(wèn)壓力和響應(yīng)時(shí)間。
第一步導(dǎo)入依賴
<!-- mybatis 二級(jí)緩存使用-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.8</version>
</dependency>
第二步編寫(xiě)配置文件
src/main/resources
目錄下創(chuàng)建一個(gè)名為 ehcache.xml
的 EhCache 配置文件。這個(gè)文件用于定義緩存的具體參數(shù),例如緩存策略、生命周期等:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.ehcache.org/ehcache.xsd">
<cache name="default"
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU">
</cache>
</ehcache>
?第三步加注解
對(duì)于 Mapper 接口,可以通過(guò)在接口上添加 @CacheNamespace
注解開(kāi)啟二級(jí)緩存
如下:
import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Mapper;
@Mapper
@CacheNamespace
public interface YourMapper {
// 方法定義
}
?第四步修改配置文件
application.properties
spring.cache.type=ehcache
spring.cache.ehcache.config=classpath:ehcache.xml
application.yml
spring:
cache:
type: ehcache
ehcache:
config: classpath:ehcache.xml
原文鏈接:https://blog.csdn.net/m0_71507863/article/details/136113026
- 上一篇:沒(méi)有了
- 下一篇:沒(méi)有了
相關(guān)推薦
- 2022-12-05 Go?reflect?反射原理示例詳解_Golang
- 2022-04-08 從頭學(xué)習(xí)C語(yǔ)言之字符串處理函數(shù)_C 語(yǔ)言
- 2022-05-23 python中3種等待元素出現(xiàn)的方法總結(jié)_python
- 2022-09-22 get方法和post方法的區(qū)別
- 2022-09-14 Go語(yǔ)言中序列化與反序列化示例詳解_Golang
- 2022-12-14 詳解如何利用Redis實(shí)現(xiàn)生成唯一ID_Redis
- 2022-12-21 k8s安裝CICD?devtron過(guò)程詳解_云其它
- 2023-01-26 Android?源碼淺析RecyclerView?Adapter_Android
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支