使用Spring/MyBatis理解EhCache

使用Spring/MyBatis理解EhCache,spring,spring-mvc,caching,ehcache,mybatis,Spring,Spring Mvc,Caching,Ehcache,Mybatis,我正在将EhCache与Spring和MyBatis一起使用,需要澄清EhCache是如何工作的。我有以下ehcache的配置文件 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false" monitori

我正在将EhCache与Spring和MyBatis一起使用,需要澄清EhCache是如何工作的。我有以下ehcache的配置文件

<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>

如果你看一下org.mybatis.caches.ehcache.EhcacheCache的源代码,你会发现

  • 它正在内部创建一个
    CacheManager
    。也没有配置类的选项(类被标记为final),因此我们可以使用Spring缓存管理器

    对您来说,最好的选择是使用Spring方法级缓存,并停止使用
    org.mybatis.caches.ehcache.EhcacheCache

    最好使用spring注释驱动的缓存,这意味着您不必使用一个大缓存,但可以为每种情况使用单独的缓存


  • 请查看此源代码以了解使用mybatis的缓存实现。GitHib上的代码示例使用mybatis映射器接口上的@Cacheable,但这是否使用了ehcache.xml中默认缓存中定义的任何设置?如果我理解正确,每个方法都会有自己的缓存,对吗?是否自动命名为方法名,因为我们可能需要在某个点刷新它?哦,我看到缓存名为findAll
    <cache type="org.mybatis.caches.ehcache.EhcacheCache" />
    
    @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();
        }
    }