spring boot整合ehcache缓存框架

为了增添后台应用系统的吞吐量 , 需要为spring boot项目添加缓存模块 , 这里选择ehcache缓存框架为例 , 来具体实践介绍spring boot若何整合ehcache内存缓存框架

spring boot整合ehcache缓存框架

文章插图

spring boot整合ehcache缓存框架

文章插图

需要这些哦
spring boot框架
ehcache框架
intellij idea软件
方式/
1打开intellij idea软件 , 建立spring boot项目 , 添加maven依靠 , 代码如下
                   <depency>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter-cache</artifactId>
                   </depency>
                   <depency>
                            <groupId>net.sf.ehcache</groupId>
                            <artifactId>ehcache</artifactId>
                            <version>2.10.5</version>
                   </depency>

spring boot整合ehcache缓存框架

文章插图

2添加@EnableCaching注解 , 注入自界说KeyGenerator , 代码如下
@Bean
    public KeyGenerator keyGenerator(){
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                sb.app(target.getClass().getName());
                sb.app(method.getName());
                for (Object obj : params) {
                    if (obj!=null){
                        sb.app(obj.toString());
                    }
                }

推荐阅读