ehcache在SpringBoot中的配置过程

1、pom依赖配置

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>

2、ehcache配置文件(src/main/resources资源目录下)

<?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 path="java.io.tmpdir/ehcache"/>
    <!-- 默认缓存 -->
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        maxElementsOnDisk="10000000"
        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU"
        />
    <!-- 定义缓存 -->
    <cache name="table1Cache"
        maxElementsInMemory="1000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="false"
        memoryStoreEvictionPolicy="LRU"/>
</ehcache> 

3、ehcache配置类(@Configuration)

@Configuration
@EnableCaching // 标注启动缓存
public class CacheConfiguration {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(CacheConfiguration.class);


    /**
     * ehcache 主要的管理器
     * @param bean
     * @return
     */
    @Bean
    public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){
        logger.warn("初始化EhCacheCacheManager");
        return new EhCacheCacheManager(bean.getObject());
    }


    @Bean
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
        logger.warn("初始化EhCacheManagerFactoryBean");
        EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();

        factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
        factoryBean.setShared(true);

        return factoryBean;
    }
}

4、ehcache注解使用

在service接口方法上进行缓存标注

/**
     * 重工业工业增加值占比(%)历史月份
     * 
     * @param startMonth
     * @param endMonth
     * @return
     */
    @Cacheable(value="table1Cache") // value为已定义缓存的名字
    List<JsonSetBean> indIncPercentageHistData(String startMonth, String endMonth);

5、缓存测试

测试@Cacheable标注的接口,第一次执行了Dao层查询,往后的调用没有执行Dao层,此时说明ehcache已经工作了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,454评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,251评论 6 342
  • Ehcache是现在最流行的纯Java开源缓存框架,配置简单、结构清晰、功能强大,最初知道它,是从Hibernat...
    安易学车阅读 2,097评论 0 11
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 34,530评论 18 399
  • 怀此贞秀姿 卓为霜下杰 ——陶渊明咏菊诗赏析 王传学 菊花是一年四季花事中开得晚的一种,其时万木凋零,众芳摇落。当...
    王传学阅读 3,140评论 0 8

友情链接更多精彩内容