spring boot配置ehcahe缓存

首先是常见的注解配置

@Configuration
@EnableCaching
public class WebConfiguration extends WebMvcConfigurerAdapter{


    /**
     *  ehcache主要的管理器
     * @return
     */
    @Bean
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
        System.out.println("CacheConfiguration.ehCacheManagerFactoryBean()");
        EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean ();
        cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("conf/ehcache.xml"));
        cacheManagerFactoryBean.setShared(true);
        return cacheManagerFactoryBean;
    }


    @Bean
    @Autowired
    public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean ehCacheManagerFactoryBean){
        System.out.println("CacheConfiguration.ehCacheCacheManager()");
        return new EhCacheCacheManager(ehCacheManagerFactoryBean.getObject());
    }

    @Bean
    public KeyGenerator customerKeyGenerator(){
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                sb.append(":")
                        .append(method.getName());
                for(Object obj:params){
                    sb.append(":")
                            .append(obj.toString());
                }
                return sb.toString();
            }
        };
    }
}

其次是ehcache的配置文件描述





    
    

    

    
    

    


    
    
    

    
    


你可能感兴趣的:(spring boot配置ehcahe缓存)