spring boot 配置Cache

2 篇文章 0 订阅
1 篇文章 0 订阅

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 {
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值