SpringBoot 集成ehcache

1, 项目实在springboot 集成mybatis 的基础上的:

https://www.cnblogs.com/pickKnow/p/11189729.html

2,pom 如下,有的不需要加,只需要加下ehcache 相关就行

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
    <dependencies>
        <!-- SpringBoot 对lombok 支持 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- SpringBoot web 核心组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <!-- SpringBoot 外部tomcat支持 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <!-- springboot-log4j -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.3.8.RELEASE</version>
        </dependency>
        <!-- springboot-aop 技术 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <!--开启 cache 缓存 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <!-- ehcache缓存 -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.9.1</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!-- mysql 依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>
  

3,需要缓存的方法上加上注解

@Cacheable

在类上给缓存加上名字
@CacheConfig(cacheNames = "userCache")
package com.aiyuesheng.mapper;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;

import com.aiyuesheng.entity.User;

@CacheConfig(cacheNames = "userCache")
public interface UserMapperdb {

    void insertUser(User user);

    @Cacheable
    User selectUserById(int id);

}

4,启动类上加上读取缓存

@EnableCaching
@MapperScan("com.aiyuesheng.mapper")
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

5,设置了缓存的名字userCache,那么这个缓存的参数在哪里?

在配置文件: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">
    <!-- 指定一个文件目录,当EhCache把数据写到硬盘上时,将把数据写到这个文件目录下 -->
    <diskStore path="java.io.tmpdir" />

    <!-- userCache缓存 -->
    <cache name="userCache" maxElementsInMemory="1000" eternal="false"
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
        diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
        diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU">
    </cache>
</ehcache>

6,在application.yml 定义去读取这个文件

#单数据源
spring:
   datasource:
     url: jdbc:mysql://127.0.0.1:3306/testdb
     username: root
     password: 123456
     driver-class-name: com.mysql.jdbc.Driver

mybatis:
  mapper-locations: classpath:com/aiyuesheng/mapping/*Mapper.xml
  type-aliases-package: com.aiyuesheng.entity

# 缓存配置读取
cache:
  type: ehcache
  ehcache:
     config: classpath:ehcache.xml

7,配置OK ,缓存里面有数据,他就会读取缓存里面的值。

8,配置信息解读

 <diskStore path="java.io.tmpdir" />  指定数据(.data and .index)存储位置,可指定磁盘中的文件夹位置期
 <cache name="userCache"  Cache的名称,必须是唯一的(ehcache会把这个cache放到HashMap里)。
    maxElementsInMemory="1000"  在内存中缓存的element的最大数目
    eternal="false"  设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断。
    timeToIdleSeconds="120" timeToLiveSeconds="120" 

    overflowToDisk="true" 如果内存中数据超过内存限制,是否要缓存到磁盘上。
    diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
           diskPersistent="false"  否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。

    diskExpiryThreadIntervalSeconds="120" 对象检测线程运行时间间隔。标识对象状态的线程多长时间运行一次。
           memoryStoreEvictionPolicy="LRU"> 缓存策略。默认值LRU,可选FIFO、LFU。

原文地址:https://www.cnblogs.com/pickKnow/p/11290732.html

时间: 2024-08-30 12:03:05

SpringBoot 集成ehcache的相关文章

【spring-boot】spring-boot集成ehcache实现缓存机制

EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. ehcache提供了多种缓存策略,主要分为内存和磁盘两级,所以无需担心容量问题. spring-boot是一个快速的集成框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. 由于spring-boot无需任何样板化的配置文件,所以spring-boot集成一些其他框架时会有略微的

springboot集成springcache

今天尝试了一下springboot集成springcache做缓存,springcache是基于annotation(注释)的一个缓存技术 特点总结如下: 通过少量的配置 annotation 注释即可使得既有代码支持缓存 支持开箱即用 Out-Of-The-Box,即不用安装和部署额外第三方组件即可使用缓存 支持 Spring Express Language,能使用对象的任何属性或者方法来定义缓存的 key 和 condition 支持 AspectJ,并通过其实现任何方法的缓存支持 支持自

springboot 配置Ehcache

Ehcache的基本配置说明我就不说了.小编记录一下在springboot中使用Ehcache的使用方法. 第一步:在classpath下引入配置文件ehcache.xml 代码如下: <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"> <cache name="demo&qu

spring-boot+mybatis+ehcache实现快速查询

项目中需要用到一些查询,数据的修改很少但查询度很大.有时还是按频率查询的.  无论如何缓存都是针对查询远远大于更新和插入的情况 mybatis 有自带的缓存,一级缓存是session级别,二级缓存是namespace . 开启二级缓的缺点:1)只有在一个namespace操作单表时使用,比如:user,和user_role两张表,如果user_role修改了,利用user的namespace去查询的结果就是脏数据. 2)在更新其中一条的时候,整个namespace都会被刷新.我们其实知道只要刷新

SpringBoot集成MyBatis的分页插件PageHelper

俗话说:好??不吃回头草,但是在这里我建议不管你是好马还是不好马,都来吃吃,带你复习一下分页插件PageHelper. 昨天给各位总结了本人学习springboot整合mybatis第一阶段的一些学习心得和源码,主要就算是敲了一下SpringBoot的门儿,希望能给各位的入门带给一点儿捷径,今天给各位温习一下MyBatis的分页插件PageHelper和SpringBoot的集成,它的使用也非常简单,开发更为高效.因为PageHelper插件是属于MyBatis框架的,所以相信很多哥们儿都已经用

springboot集成swagger2构建RESTful API文档

在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可以在访问接口上,直接添加注释 先介绍一下开发环境: jdk版本是1.8 springboot的版本是1.4.1 开发工具为 intellij idea 我们先引入swagger2的jar包,pom文件引入依赖如下: <dependency> <groupId>io.springfox&

spring-boot集成Springfox-Swagger2

import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documenta

SpringBoot集成MyBatis的分页插件PageHelper(回头草)

俗话说:好??不吃回头草,但是在这里我建议不管你是好马还是不好马,都来吃吃,带你复习一下分页插件PageHelper. 昨天给各位总结了本人学习springboot整合mybatis第一阶段的一些学习心得和源码,主要就算是敲了一下SpringBoot的门儿,希望能给各位的入门带给一点儿捷径,今天给各位温习一下MyBatis的分页插件PageHelper和SpringBoot的集成,它的使用也非常简单,开发更为高效.因为PageHelper插件是属于MyBatis框架的,所以相信很多哥们儿都已经用

springboot集成redis详解

欢迎扫码加入Java高知群交流 springboot集成redis非常简单 1.引入maven依赖redis包 <!-- springboot整合 redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> 2.appli