码迷,mamicode.com
首页 > 编程语言 > 详细

Spring-boot使用Ehcache配置

时间:2015-04-16 12:09:27      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:

1、配置类

@Configuration
@EnableCaching
public class CacheConfiguration {// implements CachingConfigurer {

    @Bean
    //@Override
    public CacheManager cacheManager() {
        EhCacheCacheManager cacheManager = new EhCacheCacheManager();
        cacheManager.setCacheManager(ehCacheManagerFactoryBean().getObject());
        return cacheManager;
    }

    @Bean
    //@Override
    public KeyGenerator keyGenerator() {
        return new SimpleKeyGenerator();
    }

    @Bean
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
        EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
        ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
        ehCacheManagerFactoryBean.setCacheManagerName("messageCache");
        ehCacheManagerFactoryBean.setShared(true);
        return ehCacheManagerFactoryBean;
    }

//    @Override
//    public CacheResolver cacheResolver() {
//        // TODO Auto-generated method stub
//        return new SimpleCacheResolver();
//    }
//
//    @Override
//    public CacheErrorHandler errorHandler() {
//        // TODO Auto-generated method stub
//        return new SimpleCacheErrorHandler();
//    }
    
}

 

2、位于resource目录下的配置文件ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <defaultCache eternal="true" maxElementsInMemory="100" overflowToDisk="false" />
    <cache name="messageCache" maxElementsInMemory="10" eternal="true" overflowToDisk="false" />
</ehcache>

 

3、Cache对象

    @Cacheable(value="messageCache")
    public Integer testCashe( String data ) {
        return new Integer(data.length());
    }

 

注意:Cache对象的value参数要与ehcache.xml配置文件中对应一致。

 

Spring-boot使用Ehcache配置

标签:

原文地址:http://www.cnblogs.com/feika/p/4431573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!