mercredi 15 avril 2015

Understanding EhCache with Spring/MyBatis

I am using EhCache with Spring and MyBatis and need some clarification on how EhCache is working. I have the following configuration file for ehcache.



<ehcache xmlns:xsi="http://ift.tt/ra1lAU"
xsi:noNamespaceSchemaLocation="http://ift.tt/LO4PtW"
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>


I am only configuring the default cache. If I am understanding this correctly when you add this line to a MyBatis mapper file it creates a new cache.



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


Which leads my to wonder does that inherit the properties from the default cache? If not what if anything is the purpose of configuring the default cache?


Is it best practice to create a cache for each piece of functionality / data, or one big cache?


Also I am trying to get away from XML, so I'm wondering if this could all be accomplished with Java Config?


I have the following Java Config, but there doesn't appear to be a way to configure the default cache with the Java Config method, so I'm wondering how well that will work, and if its a good option with working with 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();
}
}

Aucun commentaire:

Enregistrer un commentaire