spring之使用 Spring/MyBatis 理解 EhCache

jirigala 阅读:26 2025-05-04 20:05:19 评论:0

我正在将 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> 

我只是在配置默认缓存。如果我在将此行添加到 MyBatis 映射器文件时理解正确,它会创建一个新的缓存。

<cache type="org.mybatis.caches.ehcache.EhcacheCache" /> 

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

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

此外,我正在尝试摆脱 XML,所以我想知道是否可以使用 Java Config 来完成这一切?

我有以下 Java Config,但似乎没有办法使用 Java Config 方法配置默认缓存,所以我想知道它的效果如何,以及它是否是一个不错的选择使用 MyBatis?

@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(); 
    } 
} 

请您参考如下方法:

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

  1. 它正在内部创建一个 CacheManager。也没有配置类的选项(类被标记为最终的),所以我们可以让它使用 Spring 缓存管理器。

    最好的选择是使用 Spring Method 级缓存并停止使用 org.mybatis.caches.ehcache.EhcacheCache

    进行缓存

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


标签:MyBatis
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号