缓存_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
    评论
要在Redis中设置星期天缓存,首先需要在项目的根包下创建一个配置类RedisConfiguration,并在此类中通过@Bean方法来配置RedisTemplate。可以参考以下代码进行配置: @Slf4j @Configuration public class RedisConfiguration { @Bean public RedisTemplate<String, Serializable> redisTemplate( RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(RedisSerializer.string()); redisTemplate.setValueSerializer(RedisSerializer.json()); redisTemplate.setConnectionFactory(redisConnectionFactory); return redisTemplate; } } 这个配置类会创建一个RedisTemplate对象,用于操作Redis缓存。在这个对象中,我们可以通过设置键值对的序列化方式来实现对不同类型数据的存储和读取。 接下来,你可以在具体的业务逻辑中使用RedisTemplate来设置星期天缓存。比如,你可以通过以下代码将星期天缓存写入Redis: @Autowired private RedisTemplate<String, Serializable> redisTemplate; public void setSundayCache(String key, Object value) { // 将星期天缓存保存到Redis中,key为星期天的日期,value为对应的数据 redisTemplate.opsForValue().set(key, value); } 注意,这里的key可以是星期天的日期,比如"2022-01-02",value可以是你需要缓存的具体数据。 通过上述步骤,你就可以在Redis中设置星期天缓存了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Redis(含Redis安装,CMD命令,使用Redis编程,缓存预热,计划任务)](https://blog.csdn.net/weixin_73849581/article/details/128390152)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值