spring boot 配置Cache

来源:互联网 发布:股票行情软件哪家好 编辑:程序博客网 时间:2024/06/06 17:39

1.在pom文件中配置版本信息

<properties>

<jcache.version>1.0.0</jcache.version>

</properties>

并添加依赖包:

<dependency>
            <groupId>javax.cache</groupId>
            <artifactId>cache-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jcache</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

2.新建配置文件


ehache-dev.xml

<?xml version="1.0" encoding="UTF-8"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xmlns='http://www.ehcache.org/v3'         xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd">    <cache-template name="simple">        <expiry>            <ttl unit="seconds">3600</ttl>        </expiry>        <heap unit="entries">100000</heap>    </cache-template>    <cache alias="surveyQuestion" uses-template="simple"/>    <cache alias="surveyOption" uses-template="simple"/>    <cache alias="paper" uses-template="simple"/>        <!-- jhipster-needle-ehcache-add-entry --></config>
ehache-prod.xml

<?xml version="1.0" encoding="UTF-8"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xmlns='http://www.ehcache.org/v3'         xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd">    <cache-template name="simple">        <expiry>            <ttl unit="seconds">3600</ttl>        </expiry>        <heap unit="entries">100000</heap>    </cache-template>   <cache alias="surveyQuestion" uses-template="simple"/>    <cache alias="surveyOption" uses-template="simple"/>    <cache alias="paper" uses-template="simple"/>        <!-- jhipster-needle-ehcache-add-entry --></config>

配置系统配置文件

application-dev.yml

spring:    profiles:        active: dev        include: swagger    devtools:        restart:            enabled: true        livereload:            enabled: false # we use gulp + BrowserSync for livereload    jackson:        serialization.indent_output: true    datasource:        type: com.zaxxer.hikari.HikariDataSource        url: jdbc:mysql://localhost:3306/survey?useUnicode=true&characterEncoding=utf8&useSSL=false        username: root        password: root        hikari:            data-source-properties:                cachePrepStmts: true                prepStmtCacheSize: 250                prepStmtCacheSqlLimit: 2048                useServerPrepStmts: true    jpa:        database-platform: org.hibernate.dialect.MySQL5InnoDBDialect        database: MYSQL        show-sql: true        properties:            hibernate.id.new_generator_mappings: true            hibernate.cache.use_second_level_cache: false            hibernate.cache.use_query_cache: true            hibernate.generate_statistics: true            hibernate.javax.cache.provider: org.ehcache.jsr107.EhcacheCachingProvider            hibernate.javax.cache.uri: ${spring.cache.jcache.config}            hibernate.cache.region.factory_class: io.github.jhipster.config.jcache.SpringCacheRegionFactory    cache:        jcache:            config: config/ehcache/ehcache-dev.xml
application-prod.yml

spring:    devtools:        restart:            enabled: false        livereload:            enabled: false    datasource:        type: com.zaxxer.hikari.HikariDataSource        url: jdbc:mysql://10.0.101.156:3306/survey?useUnicode=true&characterEncoding=utf8&useSSL=false        username: root        password:        hikari:            data-source-properties:                cachePrepStmts: true                prepStmtCacheSize: 250                prepStmtCacheSqlLimit: 2048                useServerPrepStmts: true    jpa:        database-platform: org.hibernate.dialect.MySQL5InnoDBDialect        database: MYSQL        show-sql: false        properties:            hibernate.id.new_generator_mappings: true            hibernate.cache.use_second_level_cache: false            hibernate.cache.use_query_cache: true //需要打开            hibernate.generate_statistics: true  //需要打开            hibernate.javax.cache.provider: org.ehcache.jsr107.EhcacheCachingProvider            hibernate.javax.cache.uri: ${spring.cache.jcache.config}            hibernate.cache.region.factory_class: io.github.jhipster.config.jcache.SpringCacheRegionFactory    cache: //添加上cache的配置文件路径        jcache:            config: config/ehcache/ehcache-prod.xml
3.新建一个java配置文件

package com.qdum.survey.config;import org.springframework.boot.autoconfigure.AutoConfigureAfter;import org.springframework.boot.autoconfigure.AutoConfigureBefore;import org.springframework.cache.annotation.EnableCaching;import org.springframework.context.annotation.*;@Configuration@EnableCaching@AutoConfigureAfter(value = { MetricsConfiguration.class })@AutoConfigureBefore(value = { WebConfigurer.class, DatabaseConfiguration.class })public class CacheConfiguration {}