【SpringBoot实战】整合mybatis+ehcache

star2017 1年前 ⋅ 1478 阅读

ehcache是比较流行的本地缓存框架,当然你也可以让他成为分布式的,但是我们用的最多的还是他的本地内存缓存。示例实现了:如何使用注解,xml配置属性解释。

整体项目

imagepng

pom配置

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.2</version>
</dependency>

核心代码

EhcacheConfig:缓存配置类

package com.cimu.ehcache.config;

import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizer;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Title: EhcacheConfig * Copyright: Copyright (c) 2017 * * date 2018年12月19日 17:10 */@Configuration
public class EhcacheConfig {
    @Bean
  public CacheManagerCustomizer cacheManagerCustomizer() {
        return new CacheManagerCustomizer() {
            @Override
  public void customize(ConcurrentMapCacheManager cacheManager) {
                cacheManager.setAllowNullValues(false);
  }
        };
  }

}

UserServiceImpl:用户实现类

@Service
@CacheConfig(cacheNames="demo")
public class UserServiceImpl implements UserService {
    @Autowired
  private UserDao userDao;

  @Cacheable(value="demo")
    @Override
  public List getByAll() {
        return userDao.getByAll();
  }

    @CacheEvict(value="demo",allEntries = true)
    @Override
  public void update() {

    }

}

@Cacheable:在方法上使用注解,对该方法返回的数据进行缓存。先去缓存里面查是否存在,如果不存在再去数据库里查询。
@CacheEvict:清除缓存,value可以指定多个值,allEntries表示是否所有对象都清除,默认是false,如果要清除所有的数据,设置为true。
@CachePut:每次都去更新缓存。
@Caching:可以设置多个Cacheable,CacheEvict,CachePut。

在ChapterEhcacheApplication类上加上@EnableCaching注解。

配置

application.properties

spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.url= jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name = com.mysql.jdbc.Driver

mybatis.typeAliasesPackage=com.cimu.ehcache.entity
mybatis.mapperLocations=classpath:mapper/*.xml
spring.cache.ehcache.config=ehcache.xml

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="defaultCache">
    <!-- 指定一个文件目录,当EhCache把数据写到硬盘上时,将把数据写到这个文件目录下 -->
    <diskStore path="./temp"/>

    <!-- 设定缓存的默认数据过期策略 -->
    <defaultCache maxElementsInMemory="1000" diskPersistent="true" clearOnFlush="false"
                  eternal="true" timeToIdleSeconds="10" timeToLiveSeconds="10"
                  overflowToDisk="true"/>

    <cache name="demo" maxElementsInMemory="1" diskPersistent="true" clearOnFlush="false"
           eternal="true" timeToIdleSeconds="10" timeToLiveSeconds="10"
           overflowToDisk="true"/>

    <!-- eternal=true //缓存永久有效,false相反  -->
    <!-- maxElementsInMemory  缓存的最大数目 -->
    <!-- overflowToDisk 内存不足时,是否启用磁盘缓存,如果为true则表示启动磁盘来存储,如果为false则表示不启动磁盘 -->
    <!-- diskPersistent 是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。 -->
    <!-- timeToIdleSeconds 当缓存的内容闲置多少时间销毁 -->
    <!-- timeToLiveSeconds  当缓存存活多少时间销毁(单位是秒,如果我们想设置2分钟的缓存存活时间,那么这个值我们需要设置120) -->
    <!-- memoryStoreEvictionPolicy 自动销毁策略 可以配置为LRU(Least Recently Used 最近最少使用)、LFU(Less Frequently Used最不常用的)、FIFO(first in first out先进先出) -->
    <!-- diskExpiryThreadIntervalSeconds 磁盘缓存的清理线程运行间隔,默认是120秒 -->
    <!-- maxElementsOnDisk 磁盘缓存中最多可以存放的元素数量,0表示无穷大 -->
    <!-- clearOnFlush:内存数量最大时是否清除。 -->
    <!-- maxEntriesLocalHeap 是用来限制当前缓存在堆内存上所能保存的最大元素数量的。 -->
</ehcache>

其他的是mybatis的配置。这里就不列出来了

源码地址

本文为博主原创文章,未经博主允许不得转载。
更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: