SpringBoo2.x,整合Ehcache3.x

作者: __夏悸 | 来源:发表于2018-11-25 21:39 被阅读8次

    1、依赖

    ...
    implementation('org.springframework.boot:spring-boot-starter-cache')
    implementation('org.ehcache: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.1 SpringBoot配置

    2.1.1 application.yml配置

    这里需要注意了,在老版本Boot或者使用 ehcache2.x版本,在这里的配置是有差异的。

    老版本:

    spring:
        cache:
            ehcache:
                config: classpath:/ehcache.xml
    

    新版本:

    spring:
        cache:
            jcache:
                config: classpath:/ehcache.xml
    

    从上面新老配置可以看出,不一样的地方是,老版本使用ehcache新版本中改为了jcache,其他的不变。

    2.1.1 启用注解

    //在Boot 项目的启动类上标注@EnableCaching来开启缓存功能
    @SpringBootApplication
    @EnableCaching
    public class LappApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(LappApplication.class, args);
        }
    }
    

    3、应用缓存

    在需要使用缓存的方法或类上增加@Cacheable注解

    例如:

    @Override
    @Cacheable(cacheNames = "authority", key = "'authority_'+#uid")
    public UserPojo getUserById(Long uid) throws UsernameNotFoundException {
        User user = userDao.findById(uid).get();
        if (user == null) {
            throw new UsernameNotFoundException(uid + "");
        }
        return user.toPojo();
    }
    

    此处的cacheNames的值authority需要和ehcache.xml中 cache 的alias名称对应,否则访问是会抛找不到缓存空间的异常。

    关于更多Spring缓存注解@Cacheable、@CacheEvict、@CachePut的使用,读者可以自行百度,这里就不过多描述了。

    相关文章

      网友评论

        本文标题:SpringBoo2.x,整合Ehcache3.x

        本文链接:https://www.haomeiwen.com/subject/utpmqqtx.html