缓存_02

1、缓存原理

在这里插入图片描述

测试:

import com.beyond.dao.UserMapper;
import com.beyond.pojo.User;
import com.beyond.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class MyTest {
    @Test
    public void getUserById(){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        SqlSession sqlSession2 = MyBatisUtil.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user1 = mapper.getUserById(1);
        System.out.println(user1);

        sqlSession.close();

        System.out.println("=================================================");
        UserMapper mapper2 = sqlSession2.getMapper(UserMapper.class);
        User user2 = mapper2.getUserById(1);
        System.out.println(user2);

        System.out.println(user1==user2);

        User user3 = mapper2.getUserById(6);
        System.out.println(user3);

        sqlSession2.close();
    }
}

在这里插入图片描述

2、自定义缓存——ehcache

Encache是一种广泛使用的开源Java分布式缓存。

使用步骤:

  1. 在项目中导入ehcache的jar包(在maven仓库下载);

    <!-- https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache -->
            <dependency>
                <groupId>org.mybatis.caches</groupId>
                <artifactId>mybatis-ehcache</artifactId>
                <version>1.1.0</version>
            </dependency>
    
  2. 在Mapper.xml中指定使用我们的ehcache缓存实现;

     <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
    
  3. 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分为内存和磁盘两级,此属性定义磁盘的缓存位置。参数解释如下:
        user.home——用户主目录
        user.dir——用户当前工作目录
        java.io.tmpdir——默认临时文件路径
        -->
        <diskStore path="./tmpdir/Tmp_Ehcache"/>
    
        <defaultCache
            eternal="false"
            maxElementsInMemory="10000"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="1800"
            timeToLiveSeconds="259200"
            memoryStoreEvictionPolicy="LRU"/>
    
        <cache
            name="cloud_user"
            eternal="false"
            maxElementsInMemory="5000"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="1800"
            timeToLiveSeconds="1800"
            memoryStoreEvictionPolicy="LRU"/>
    <!--
    defaultCache:默认缓存策略,当ehcache找不到定义的缓存时,则使用这个缓存策略。只能定义一个。
    name:缓存名称;
    maxElementsInMemory:缓存最大数目;
    eternal:对象是否永久有效,一旦设置了,timeout将不起作用;
    overflowToDisk:是否保存到磁盘,当系统宕机时;
    timeToIdleSeconds:设置对象在失效前允许闲置时间(单位:秒)。仅当eternal="false"对象不是永久有效使用,可选属性,默认值是0,也就是可闲置时间无穷大。
    timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal="false"对象不是永久有效使用,默认值是0,也就是对象存活时间无穷大。
    diskPersistent:是否缓存虚拟机重启时的数据(whether the disk store persists between restarts of the Virtual Machine. The default value is false.);
    diskSpoolBufferSizeMB:这个参数设置(磁盘缓存)的缓存区大小。默认是30MB。每个cache都应该有自己的一个缓存区。
    diskExpiryThreadIntervalSeconds:磁盘是小线程运行时间间隔,默认是120秒。
    memoryStoreEvictionPolicy:可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。
    clearOnFlush:内存数量最大时是否清除。
    -->
    
    </ehcache>
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值