1. 程式人生 > 其它 >【TcaplusDB知識庫】如何部署和使用TcaplusDB docker版 (JDBC)

【TcaplusDB知識庫】如何部署和使用TcaplusDB docker版 (JDBC)

 一級快取預設開啟,快取範圍是SqlSession會話,二級快取需手動開啟,快取範圍是Mapper Namespace。在某個namespace中手動開啟的二級快取被所有SqlSession共享。二級快取開啟後預設所有查詢操作均使用快取,為保證資料的一致性,寫操作commit提交時對該namespace快取強制清空。

 配置useCache=false可以不用快取

 配置flushCache=true代表強制清空快取

<select id="selectById" parameterType="Integer" resultType="com.MyBatis.entity.Goods"
> select * from t_goods where goods_id = #{value} </select>

驗證一級快取:

 在同一個SqlSession物件中,執行兩次相同的查詢操作

@Test
    public void testlv1Cache(){
        SqlSession sqlSession=null;
        try{
            sqlSession=MyBatisUtils.openSession();
            Goods goods = sqlSession.selectOne("goods.selectById",1603);
            Goods goods1 
= sqlSession.selectOne("goods.selectById",1603); System.out.println(goods.hashCode()+":"+goods1.hashCode()); }catch (Exception e){ if(sqlSession!=null){ sqlSession.rollback(); } throw e; }finally { MyBatisUtils.closeSession(sqlSession); } }

如下圖,goods和goods1指向同一個物件

 

 在兩個SqlSession物件中執行重複查詢

@Test
    public void testlv1Cache(){
        SqlSession sqlSession=null;
        try{
            sqlSession=MyBatisUtils.openSession();
            Goods goods = sqlSession.selectOne("goods.selectById",1603);
            Goods goods1 = sqlSession.selectOne("goods.selectById",1603);
            System.out.println(goods.hashCode()+":"+goods1.hashCode());
        }catch (Exception e){
            if(sqlSession!=null){
                sqlSession.rollback();
            }
            throw e;
        }finally {
            MyBatisUtils.closeSession(sqlSession);
        }

        try{
            sqlSession=MyBatisUtils.openSession();
            Goods goods = sqlSession.selectOne("goods.selectById",1603);
            Goods goods1 = sqlSession.selectOne("goods.selectById",1603);
            System.out.println(goods.hashCode()+":"+goods1.hashCode());
        }catch (Exception e){
            if(sqlSession!=null){
                sqlSession.rollback();
            }
            throw e;
        }finally {
            MyBatisUtils.closeSession(sqlSession);
        }
    }

一級快取的範圍只在SqlSession物件中

 

 二級快取的開啟

 在mapper下增加cache標籤

<?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="goods">

    <!--開啟了二級快取-->
    <cache eviction="LRU" flushInterval="600000" size="512" readOnly="true"/>
 
    <select id="selectById" parameterType="Integer" resultType="com.MyBatis.entity.Goods">
        select * from t_goods where goods_id = #{value}
    </select>

</mapper>

測試程式碼:

@Test
    public void testlv2Cache(){
        SqlSession sqlSession=null;
        try{
            sqlSession=MyBatisUtils.openSession();
            Goods goods = sqlSession.selectOne("goods.selectById",1603);
            System.out.println(goods.hashCode());
        }catch (Exception e){
            if(sqlSession!=null){
                sqlSession.rollback();
            }
            throw e;
        }finally {
            MyBatisUtils.closeSession(sqlSession);
        }

        try{
            sqlSession=MyBatisUtils.openSession();
            Goods goods = sqlSession.selectOne("goods.selectById",1603);
            System.out.println(goods.hashCode());
        }catch (Exception e){
            if(sqlSession!=null){
                sqlSession.rollback();
            }
            throw e;
        }finally {
            MyBatisUtils.closeSession(sqlSession);
        }
    }

 

 cache的四個設定項:

1.eviction是快取的清楚策略,當快取物件數量達到上限後,自動觸發對應演算法對快取物件清除
1.LRU - 最近最久未使用:已出最長時間不被使用的物件。
O1 O2 O3 O4 .. O512
14 99 83 1 893
2.FIFO - 先進先出:按物件進入快取的順序移除
3.SOFT - 軟引用:移除基於垃圾收集器狀態和軟引用規則的物件
4.WEAK - 弱引用:更積極的移除基於垃圾收集器狀態和弱引用規則的物件

2.flushInterval 代表間隔多長時間自動清空快取,單位毫秒 600000ms=10min

3.size 快取儲存上限,用於儲存物件或集合(一個集合算一個物件)的數量上限

4.readOnly 設定為true,代表返回只讀快取,每次從快取取出的是快取物件本身,執行效率更高
設定為false,代表每次取出的是快取 ‘副本’ ,每一次取出的物件是不同的,安全性較高

List在放入快取時會被當做一個物件,對記憶體的使用非常大,因此可以在<select>標籤中新增屬性 useCache="false" 表示不被快取。在<select>標籤中新增屬性flushCache="true" 表示在sql執行後強制清空快取