gpt4 book ai didi

spring - 使用 Spring/MyBatis 理解 EhCache

转载 作者:行者123 更新时间:2023-12-01 15:43:21 24 4
gpt4 key购买 nike

我正在将 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 注释驱动的缓存,这意味着您不必使用一个大缓存,但可以为每种情况使用单独的缓存。

关于spring - 使用 Spring/MyBatis 理解 EhCache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29641357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com