Spring-BootSpringBoot & EasyUIspring boot

spring-boot-starter-data-jpa 的二级

2018-11-25  本文已影响5人  __夏悸

Spring Data JPA是更大的Spring Data系列的一部分,可以轻松实现基于JPA的存储库。它用来处理对基于JPA的数据访问层支持增强。有了它,我们能更加容易构建出,使用数据访问技术的Spring应用程序。

在相当长的一段时间内,实现应用程序的数据访问层一直很麻烦。必须编写太多样板代码来执行简单查询以及执行分页和审计。Spring Data JPA旨在通过减少实际需要的工作量来显着改善数据访问层的实现。作为开发人员,只需编写存储库接口,包括自定义查找器方法,Spring将自动提供实现。

特征

1、依赖

implementation('org.springframework.boot:spring-boot-starter-data-jpa')
implementation('org.hibernate:hibernate-jcache')
implementation('org.ehcache:ehcache')

此处需要注意,如果高版本的 SpringBoot,我们需要使用org.hibernate:hibernate-jcache,而不是org.hibernate:hibernate-ehcache。否则启动异常

2、配置

2.1 ehcache配置

在resources 目录中创建ehcache.xml配置ehcache。

这里需要注意的是,百度里面的文章大部分都是告诉大家怎么配置ehcache2.x,但是ehcache的2.x和3.x完全就两码事情了,所以如果不经过思考,一通 copy 那栽跟头是肯定得了。

下面我贴出我项目中使用的配置:

<?xml version="1.0" encoding="UTF-8"?>
<eh:config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns:eh='http://www.ehcache.org/v3'
        xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.3.xsd">
    <!--指定缓存目录-->
    <eh:persistence directory="${java.io.tmpdir}/cfa-cache-data"/>
    
    <!--缓存模板-->
    <eh:cache-template name="default">
        <eh:expiry>
            <eh:ttl unit="seconds">600</eh:ttl>
        </eh:expiry>
        <eh:resources>
            <!--堆内内存可以放2000个条目,超出部分堆外100MB-->
            <eh:heap unit="entries">2000</eh:heap>
            <eh:offheap unit="MB">100</eh:offheap>
        </eh:resources>
    </eh:cache-template>

    <!--实际的缓存区间,继承了default缓存模板,cfa 完全使用模板默认-->
    <eh:cache alias="cfa" uses-template="default">
        
    </eh:cache>

    <!--下面两个继承了default缓存模板,但覆盖了缓存的过期时间-->
    <eh:cache alias="authority" uses-template="default">
        <eh:expiry>
            <eh:ttl unit="hours">1</eh:ttl>
        </eh:expiry>
    </eh:cache>

    <eh:cache alias="lapp_service" uses-template="default">
        <eh:expiry>
            <eh:ttl unit="hours">24</eh:ttl>
        </eh:expiry>
    </eh:cache>
</eh:config>

2.2 SpringBoot配置

由于data-jpa 使用了 hibernate 作为底层实现,所以我们配置二级缓存其实就是基于 hibernate 的二级缓存。

hibernate 在启动时,会默认读取 classpath 目录下面的hibernate.properties配置。所以我们需要在resources目录中创建这个文件。

文件内容:

hibernate.format_sql=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
hibernate.cache.region_prefix=cfa_repo_
hibernate.cache.region.factory_class=org.hibernate.cache.jcache.internal.JCacheRegionFactory
hibernate.cache.provider_configuration_file_resource_path=ehcache.xml
hibernate.cache.use_structured_entries=true
hibernate.generate_statistics=false
hibernate.javax.cache.missing_cache_strategy=create

此处我们需要注意的是factory_class的配置,如果你去百度hibernate的二级缓存配置,就这一项你可以百度出五个不同的版本。然并卵,基本没有对的。因为这个版本更新太快,类也变得有点快。经过一番尝试之后,得出我当前这个版本factory_class配置有效的类,所以注意,不一定 copy我的就有用。

2.3 启用注解

//在Boot 项目的启动类上标注@EnableCaching来开启缓存功能
@SpringBootApplication
@EnableCaching
public class LappApplication {

    public static void main(String[] args) {
        SpringApplication.run(LappApplication.class, args);
    }
}

3、应用缓存

在需要开启二级缓存的实体类上大标注

@Entity
@Table(name = "lapp_unit"))
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Unit {
  ....
}

嗯,然后就没有然后了。在使用 jpa 的 findById 方法进行查询和 save 进行更新数据的时候,hibernate 就会使用到缓存了。具体测试,可以自行玩耍。

上一篇下一篇

猜你喜欢

热点阅读