我正在与Spring和MyBatis一起使用EhCache,需要对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完成?
我有下面的Java,但是似乎没有一种使用Java方法配置默认缓存的方法,所以我想知道这会有多好,以及使用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();
}
}发布于 2015-04-15 10:53:17
如果您查看`org.mybatis.caches.ehcache.EhcacheCache的源代码,您会发现
CacheManager。也没有配置类的选项(Class标记为final),这样我们就可以让它使用Spring缓存管理器。
最好的选择是使用Spring方法级缓存,停止使用org.mybatis.caches.ehcache.EhcacheCache缓存的想法
最好使用spring注释驱动的缓存,这意味着您不必使用一个大缓存,而是可以为每种情况使用单独的缓存。https://stackoverflow.com/questions/29641357
复制相似问题