首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Spring/MyBatis理解EhCache

用Spring/MyBatis理解EhCache
EN

Stack Overflow用户
提问于 2015-04-15 04:00:25
回答 1查看 3.8K关注 0票数 3

我正在与Spring和MyBatis一起使用EhCache,需要对EhCache的工作方式做一些澄清。我有下面的ehcache配置文件。

代码语言:javascript
复制
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
         updateCheck="false"
         monitoring="autodetect"
         dynamicConfig="true">

    <diskStore path="java.io.tmpdir" />

    <defaultCache maxElementsInMemory="10000"
                  eternal="false"
                  timeToIdleSeconds="300"
                  timeToLiveSeconds="600"
                  diskSpoolBufferSizeMB="30"
                  maxElementsOnDisk="10000"
                  diskExpiryThreadIntervalSeconds="120"
                  memoryStoreEvictionPolicy="LRU" statistics="false">
    </defaultCache>
</ehcache>

我只是在配置默认缓存。如果您将这一行添加到MyBatis映射程序文件中时,我正确地理解了这一点,那么它将创建一个新的缓存。

代码语言:javascript
复制
<cache type="org.mybatis.caches.ehcache.EhcacheCache" />

这让我想知道,这是否继承了默认缓存中的属性?如果不是,那么配置默认缓存的目的是什么呢?

最好的做法是为每个功能/数据创建一个缓存,还是创建一个大缓存?

此外,我还试图摆脱XML,所以我想知道这一切是否都可以用Java完成?

我有下面的Java,但是似乎没有一种使用Java方法配置默认缓存的方法,所以我想知道这会有多好,以及使用MyBatis是否是一个很好的选择?

代码语言:javascript
复制
@Configuration
@EnableCaching
public class CacheConfig implements CachingConfigurer {

    @Autowired
    Environment environment;

    @Bean(destroyMethod = "shutdown")
    public net.sf.ehcache.CacheManager ehCacheManager() {
        CacheConfiguration cacheConfiguration = new CacheConfiguration();
        cacheConfiguration.setName(environment.getRequiredProperty("ehcache.name"));
        cacheConfiguration.setMemoryStoreEvictionPolicy(environment.getRequiredProperty("ehcache.memoryStoreEvictionPolicy"));
        cacheConfiguration.setDiskExpiryThreadIntervalSeconds(environment.getRequiredProperty("ehcache.diskExpiryThreadIntervalSeconds", Integer.class));
        cacheConfiguration.setDiskSpoolBufferSizeMB(50);
        cacheConfiguration.setOverflowToDisk(true);
        cacheConfiguration.setDiskPersistent(true);
        cacheConfiguration.setMaxBytesLocalHeap("512000000");
        cacheConfiguration.setMaxBytesLocalDisk("2048000000");
        cacheConfiguration.eternal(false);
        cacheConfiguration.setTimeToIdleSeconds(1800);
        cacheConfiguration.setTimeToLiveSeconds(3600);
        cacheConfiguration.statistics(true);
        cacheConfiguration.logging(true);

        net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
        config.addCache(cacheConfiguration);

        return net.sf.ehcache.CacheManager.newInstance(config);
    }

    @Bean
    @Override
    public org.springframework.cache.CacheManager cacheManager() {
        return new EhCacheCacheManager(ehCacheManager());
    }

    @Override
    public CacheResolver cacheResolver() {
        return new SimpleCacheResolver();
    }

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

    @Override
    public CacheErrorHandler errorHandler() {
        return new SimpleCacheErrorHandler();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-15 10:53:17

如果您查看`org.mybatis.caches.ehcache.EhcacheCache的源代码,您会发现

  1. 它在内部创建一个CacheManager。也没有配置类的选项(Class标记为final),这样我们就可以让它使用Spring缓存管理器。 最好的选择是使用Spring方法级缓存,停止使用org.mybatis.caches.ehcache.EhcacheCache缓存的想法 最好使用spring注释驱动的缓存,这意味着您不必使用一个大缓存,而是可以为每种情况使用单独的缓存。
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29641357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档