• Spring-boot使用Ehcache配置


    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配置文件中对应一致。

  • 相关阅读:
    c#同类型操作最终得到的结果将是同类型
    RabbitMQ的高级特性(五)延迟队列 lq
    RabbitMQ的高级特性(二)消费端限流 lq
    RabbitMQ的高级特性(一)可靠性传递 lq
    RabbitMQ的高级特性(四)死信队列 lq
    RabbitMQ的高级特性(三)TTL lq
    rabbitmq的常见问题及解决方案 lq
    RabbitMQ集群搭建 lq
    炫酷设计
    【转】全球超酷智能眼镜扫描
  • 原文地址:https://www.cnblogs.com/feika/p/4431573.html
Copyright © 2020-2023  润新知