spring 如何整合ehcache,以及ehcache 如何整合mybatis 这里不作详细介绍。java
简单说一下需求:我有不一样的的模块须要配置缓存,不一样的模块有须要不同的缓存策略,那么就能够为chcache 配置多个的缓存,根据不一样的名称去访问。spring
1.先看 ehcache.xml(放在src目录下)缓存
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <diskStore path="java.io.tmpdir" /> <cache name="localtest" eternal="false" maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" /> <!--配置token 缓存策略 --> <cache name="test1" eternal="false" maxElementsInMemory="20000" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="6000" memoryStoreEvictionPolicy="LRU" /> <!--配置token 缓存策略 --> <cache name="test2" eternal="false" maxElementsInMemory="12350" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="300" timeToLiveSeconds="3000" memoryStoreEvictionPolicy="LRU" /> </ehcache>
能够看到这里配置了三个cache : localtest test1 test2 (具体参数配置请参考官方文档)mybatis
2。再看 applicationContext.xml 中的配置:app
<!-- 配置Ehcache缓存管理器,读取配置文件 --> <bean id="ehCacheManagerFactoryBean" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="cacheManagerName" value="testManager"></property> <property name="configLocation" value="classpath:ehcache.xml"></property> <property name="shared" value="true"></property> </bean> <!-- 配置缓存管理器,获取cache --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" > <property name="cacheManager" ref="ehCacheManagerFactoryBean" /> </bean>
3. 在代码中使用ide
@Resource EhCacheCacheManager cacheManager; //存 org.springframework.cache.Cache cache = cacheManager.getCache("test1"); cache.put(key, value); //取 if (StringUtils.isNotBlank(key)) { Cache cache = cacheManager.getCache("test1"); String value= cache.get(key,String.class); }
最后 说说为何我是这样配置的以及开发中遇到的问题:this
一开始配置文件并无放置在 src 目录下面,同时也没有为ehCacheManagerFactoryBean 配置cachemanagerName,这样的一旦 share 配置为 false 启动便会抱错,url
Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of followingspa
已经存在一个cacheManager ,如何不让共享那么必须配置一个新的惟一的名字,因此share 设置为true ,同时为其配置了一个名字(若是不想设置为true 那么能够添加 命名)。debug
可是这样配置之后,debug 发现:并不能从cacheManaer 中获取到指定名称的cache!
在修改N次配置后,我忽然想也许我配置的ehcache.xml根本没有生效!
继续debug > 发如今启动的时候
EhCacheManagerFactoryBean中有个方法:afterPropertiesSet()
EhCacheManagerUtils.parseConfiguration(this.configLocation) : ConfigurationFactory.parseConfiguration());
public static Configuration parseConfiguration() throws CacheException { ClassLoader standardClassloader = Thread.currentThread().getContextClassLoader(); URL url = null; if (standardClassloader != null) { url = standardClassloader.getResource(DEFAULT_CLASSPATH_CONFIGURATION_FILE); } if (url == null) { url = ConfigurationFactory.class.getResource(DEFAULT_CLASSPATH_CONFIGURATION_FILE); } if (url != null) { LOG.debug("Configuring ehcache from ehcache.xml found in the classpath: " + url); } else { url = ConfigurationFactory.class.getResource(FAILSAFE_CLASSPATH_CONFIGURATION_FILE); LOG.warn("No configuration found. Configuring ehcache from ehcache-failsafe.xml " + " found in the classpath: {}", url); } Configuration configuration = parseConfiguration(url); configuration.setSource(ConfigurationSource.getConfigurationSource()); return configuration; }
这个方法会加载 ehcache的配置文件,奇怪的是并无找我配置ehcache.xml,因此自动调用了默认的配置,可是尚未完,断点第二次跑到这里的时候才加载我配置的xml ! 经过名字会发现两次的cacheManagerName 是不同的。这样就解释了为何经过cacheManger 获取不到的指定的cache了! 由于此时的cacheManager 就是默认的那个而非加载个人xml的那个(真特么拗口!)(若是能到拿到那个cachemanger 天然就能够获取咱们定义的cache了)
既然默认的非要去src下找配置,那就如其所愿好了! 果断把配置传送过去。
而后经过 遍历全部的cache 就能够看到咱们的定义的test1 test2 localtest了
Collection<String> cacheNames = cacheManager.getCacheNames();
for (String string : cacheNames) {
System.out.println("names:"+string);
}
此外:若是配置mybatis + ehcache能够看到cache中同时存在mybatis 的缓存 key 天然是namespace了。
走到这里,我又试了一下,将前面提到的 share 设置为false,经过cacheManager.getName() 获取其名字,获得的就是配置里的 testManager! 而且这个Manager 中不存在mybatis 的cache 由于mybatis 的cache 自_defaut_ 那个 cacheManger 中!!!
固然到这里只是解决了暂时的问题,更多的内容还有待研究。若是有问题但愿你们不吝赐教!