最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mybatis整合ehcache做三級緩存的實現(xiàn)方法

 更新時間:2023年06月20日 09:34:47   作者:ThinkPet  
ehcache是一個快速內(nèi)存緩存框架,java項目里用起來很方便,下面這篇文章主要給大家介紹了關于mybatis整合ehcache做三級緩存的實現(xiàn)方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

mybatis整合ehcache做三級緩存

為什么要使用第三級緩存

mybatis二級緩存對細粒度的數(shù)據(jù)級別的緩存實現(xiàn)不好,對同時緩存較多條數(shù)據(jù)的緩存,比如如下需求:

對商品信息進行緩存,由于商品信息查詢訪問量大,且要求用戶每次都能查詢最新的商品信息,此時如果使用mybatis的二級緩存就無法實現(xiàn)---當一個商品變化時只刷新該商品的緩存信息而不刷新其他商品的信息。

因為mybatis的二級緩存區(qū)域以mapper為單位劃分,當一個商品信息變化會將所有商品信息的緩存數(shù)據(jù)全部清空。

解決此類問題需要在業(yè)務層根據(jù)需求對數(shù)據(jù)進行針對性的緩存,即需要使用三級緩存。

原生mybatis整合ehcache做三級緩存

引入mybatis官方提供的依賴mybatis-ehcache

<!-- mybatis-ehcache用的是1.1.0版本-->
<dependency>
            <groupId>org.mybatis.caches</groupId>
            <artifactId>mybatis-ehcache</artifactId>
            <version>1.1.0</version>
        </dependency>
<!-- 這里原生mybatis用的是2.1.3版本 -->
 <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

在springboot項目的yml配置文件里配置mybatis

要開啟mybatis.configuration.cache-enabled=true

mybatis:
  type-aliases-package: com.example.demo.orm.po
  mapper-locations: classpath:mappers/*Mapper.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true
    # 開啟全局緩存(mybatis二級緩存),默認不會開啟,需要程序員配置開啟
    cache-enabled: true

在resources目錄下配置ehcache.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
         updateCheck="false">
    <!--
       diskStore:為緩存路徑,ehcache分為內(nèi)存和磁盤兩級,此屬性定義磁盤的緩存位置。參數(shù)解釋如下:
       user.home – 用戶主目錄
       user.dir  – 用戶當前工作目錄
       java.io.tmpdir – 默認臨時文件路徑 win7里是 C:\Users\Administrator\AppData\Local\Temp 目錄
     -->
    <diskStore path="java.io.tmpdir/Tmp_EhCache"/>
    <!--
       defaultCache:默認緩存策略,當ehcache找不到定義的緩存時,則使用這個緩存策略。只能定義一個。
     -->
    <!--
      name:緩存名稱。
      maxElementsInMemory:緩存最大數(shù)目
      maxElementsOnDisk:硬盤最大緩存?zhèn)€數(shù)。
      eternal:對象是否永久有效,一但設置了,timeout將不起作用。
      overflowToDisk:是否保存到磁盤,當系統(tǒng)當機時
      timeToIdleSeconds:設置對象在失效前的允許閑置時間(單位:秒)。僅當eternal=false對象不是永久有效時使用,可選屬性,默認值是0,也就是可閑置時間無窮大。
      timeToLiveSeconds:設置對象在失效前允許存活時間(單位:秒)。最大時間介于創(chuàng)建時間和失效時間之間。僅當eternal=false對象不是永久有效時使用,默認是0.,也就是對象存活時間無窮大。
      diskPersistent:是否緩存虛擬機重啟期數(shù)據(jù) Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
      diskSpoolBufferSizeMB:這個參數(shù)設置DiskStore(磁盤緩存)的緩存區(qū)大小。默認是30MB。每個Cache都應該有自己的一個緩沖區(qū)。
      diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認是120秒。
      memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據(jù)指定的策略去清理內(nèi)存。默認策略是LRU(最近最少使用)。你可以設置為FIFO(先進先出)或是LFU(較少使用)。
      clearOnFlush:內(nèi)存數(shù)量最大時是否清除。
      memoryStoreEvictionPolicy:可選策略有:LRU(最近最少使用,默認策略)、FIFO(先進先出)、LFU(最少訪問次數(shù))。
      FIFO,first in first out,這個是大家最熟的,先進先出。
      LFU, Less Frequently Used,就是上面例子中使用的策略,直白一點就是講一直以來最少被使用的。如上面所講,緩存的元素有一個hit屬性,hit值最小的將會被清出緩存。
      LRU,Least Recently Used,最近最少使用的,緩存的元素有一個時間戳,當緩存容量滿了,而又需要騰出地方來緩存新的元素的時候,那么現(xiàn)有緩存元素中時間戳離當前時間最遠的元素將被清出緩存。
   -->
    <defaultCache
            eternal="false"
            maxElementsInMemory="10000"
            overflowToDisk="true"
            diskPersistent="true"
            timeToIdleSeconds="1800"
            timeToLiveSeconds="259200"
            memoryStoreEvictionPolicy="LRU"/>
</ehcache>

在需要使用ehcache緩存的mapper.xml里配置cache標簽

如:在AccountMapper.xml里配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.orm.dao.AccountMapper">
    <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
<!--  ehcache 用作mybatis的三級緩存時
,只會對select語句進行緩存,每次遇到其他語句如insert,update,delete則會清空緩存
 即:新插入的數(shù)據(jù),或修改的數(shù)據(jù),只有當立即select時,會緩存到Ehcache中。
 select 結果緩存到Ehcache后,再次查詢相同數(shù)據(jù)時,會命中Ehcache緩存;
 Ehcache默認配置是LRU, 緩存的內(nèi)容是 最近最少使用的 ,
 緩存的元素有一個時間戳,當緩存容量滿了,而又需要騰出地方來緩存新的元素的時候,
 那么現(xiàn)有緩存元素中時間戳離當前時間最遠的元素將被清出緩存
 -->
    <resultMap id="BaseResultMap"
               type="com.example.demo.orm.po.Account">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="username" jdbcType="VARCHAR" property="username"/>
        <result column="password_ciper" jdbcType="VARCHAR" property="passwordCiper"/>
    </resultMap>
    <select id="findOneById" resultMap="BaseResultMap">
        select * from t_account where id = #{id}
    </select>
    <select id="selectUserList"
            parameterType="com.example.demo.orm.po.Account"
            resultMap="BaseResultMap">
        select * from t_account
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="username != null and username != '' ">
                and username = #{username}
            </if>
            <if test="passwordCiper != null and passwordCiper != '' ">
                and password_ciper = #{passwordCiper}
            </if>
        </where>
    </select>
    <insert id="insertOne"
            useGeneratedKeys="true"
            keyProperty="id"
            parameterType="com.example.demo.orm.po.Account">
       insert into t_account(username,password_ciper)
        values (#{username},#{passwordCiper})
    </insert>
</mapper>

測試Ehcache緩存效果

 @Test
    void test81902(){
        //連續(xù)執(zhí)行2次相同的select操作
        System.out.println(accountMapper.findOneById(17));
        System.out.println(accountMapper.findOneById(17));
}

2023-06-19 19:51:41.924  INFO 6356 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-06-19 19:51:42.186  INFO 6356 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection@1502971166 wrapping com.mysql.cj.jdbc.ConnectionImpl@48a2db72] will not be managed by Spring
==>  Preparing: select * from t_account where id = ?
==> Parameters: 17(Integer)
<==    Columns: id, username, password, password_ciper
<==        Row: 17, ert, sgvf23t, 1a9777393e7fda54
<==      Total: 1
2023-06-19 19:51:42.452  INFO 6356 --- [           main] c.e.demo.plugin.EncryptDecryptUtil       : passwordCiper字段需要解密,1a9777393e7fda54解密后的值是sgvf23t
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@518ddd3b]
Account(id=17, username=ert, password=sgvf23t, passwordCiper=sgvf23t)
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@72443081] was not registered for synchronization because synchronization is not active
Cache Hit Ratio [com.example.demo.orm.dao.AccountMapper]: 0.5
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@72443081]
Account(id=17, username=ert, password=sgvf23t, passwordCiper=sgvf23t)

Cache Hit Ratio [com.example.demo.orm.dao.AccountMapper]: 0.5

意思是緩存命中了AccountMapper,命中率0.5

總結

到此這篇關于mybatis整合ehcache做三級緩存的實現(xiàn)方法的文章就介紹到這了,更多相關mybatis整合ehcache三級緩存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • idea修改只讀/可寫狀態(tài)全過程

    idea修改只讀/可寫狀態(tài)全過程

    本文記錄了解決IntelliJ IDEA(打開文件只讀問題的過程,在設置中找到Editor下的Reader Mode,取消勾選選第一個可選框即可解決問題,同時提到該選項看起來很好看,本文僅供參考,希望能對讀者有所幫助
    2026-05-05
  • Springboot MultipartFile文件上傳與下載的實現(xiàn)示例

    Springboot MultipartFile文件上傳與下載的實現(xiàn)示例

    在Spring Boot項目中,可以使用MultipartFile類來處理文件上傳和下載操作,本文就詳細介紹了如何使用,具有一定的參考價值,感興趣的可以了解一下
    2023-08-08
  • 解決spring jpa中update的坑

    解決spring jpa中update的坑

    這篇文章主要介紹了spring jpa中update遇到的坑及解決方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Spring?Cloud?Gateway整合sentinel?實現(xiàn)流控熔斷的問題

    Spring?Cloud?Gateway整合sentinel?實現(xiàn)流控熔斷的問題

    本文給大家介紹下?spring?cloud?gateway?如何整合?sentinel實現(xiàn)流控熔斷,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友一起看看吧
    2022-02-02
  • Java中StringBuilder與StringBuffer使用及源碼解讀

    Java中StringBuilder與StringBuffer使用及源碼解讀

    我們前面學習的String就屬于不可變字符串,因為理論上一個String字符串一旦定義好,其內(nèi)容就不可再被改變,但實際上,還有另一種可變字符串,包括StringBuilder和StringBuffer兩個類,那可變字符串有什么特點,又怎么使用呢,接下來就請大家跟我一起來學習吧
    2023-05-05
  • Java通過百度地圖API獲取定位(普通IP定位)的方法教程

    Java通過百度地圖API獲取定位(普通IP定位)的方法教程

    這篇文章主要介紹了Java通過百度地圖API獲取定位的方法教程,首先說明了實現(xiàn)這個功能的需求和初衷,然后詳細描述了利用百度地圖API實現(xiàn)這個功能的步驟,包括在百度地圖開放平臺的準備工作、學習官網(wǎng)API文檔、修改API的AK配置、Java代碼獲取定位等,需要的朋友可以參考下
    2024-11-11
  • 聊聊springboot?整合?hbase的問題

    聊聊springboot?整合?hbase的問題

    這篇文章主要介紹了springboot?整合?hbase的問題,文中給大家提到配置linux服務器hosts及配置window?hosts的相關知識,需要的朋友可以參考下
    2021-11-11
  • Spring?Cloud?Gateway?2.x跨域時出現(xiàn)重復Origin的BUG問題

    Spring?Cloud?Gateway?2.x跨域時出現(xiàn)重復Origin的BUG問題

    這篇文章主要介紹了Spring?Cloud?Gateway?2.x跨域時出現(xiàn)重復Origin的BUG問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • SpringBoot之Helloword 快速搭建一個web項目(圖文)

    SpringBoot之Helloword 快速搭建一個web項目(圖文)

    這篇文章主要介紹了SpringBoot之Helloword 快速搭建一個web項目(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • Java8常用的新特性詳解

    Java8常用的新特性詳解

    這篇文章主要介紹了Java8常用的新特性詳解,文中有非常詳細的代碼示例,對正在學習Java8新特性的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04

最新評論

台安县| 苏州市| 新绛县| 博湖县| 朔州市| 河东区| 彭山县| 昌宁县| 台东县| 磐石市| 岑溪市| 敦化市| 教育| 郯城县| 晋州市| 清镇市| 共和县| 突泉县| 临安市| 杭锦后旗| 上思县| 琼结县| 禄丰县| 麻栗坡县| 定日县| 岚皋县| 舒兰市| 永宁县| 蓬安县| 赤水市| 福鼎市| 云林县| 寿光市| 宝应县| 临西县| 海城市| 东城区| 渭源县| 沈丘县| 冕宁县| 全椒县|