ehchche配置中配置多个缓存对象

来源:互联网 发布:学霸养成软件 编辑:程序博客网 时间:2024/05/15 00:18
Ehcache不允许创建同样名称的CacheManager对象。如果我们没有ehcache.xml中配置CacaheManager的名称,那么默认的名称是__DEFAULT__。解决方式是<ehcache name="">中配置CacheManager的名称,并确保唯一。这样如下代码就不会报错了

  1. URL url = CacheHelper.class.getClassLoader().getResource("ehcache.xml");  
  2. CacheManager manager = new CacheManager(url);  
  3.  // __DEFAULT__  
  4. System.out.println(manager.getName());  
  5.   
  6. URL url2 = CacheHelper.class.getClassLoader().getResource("ehcache2.xml");  
  7.       
  8. CacheManager manager2 = new CacheManager(url2); 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>

<!--<diskStore path="/home/uat_dm/log/ehcache/oss"/>-->
<diskStore path="java.io.tmpdir/hibernate/oss" />
<defaultCache maxElementsInMemory="10000" memoryStoreEvictionPolicy="LRU" eternal="false"
timeToIdleSeconds="300" timeToLiveSeconds="300" overflowToDisk="false" diskPersistent="false" />

<!-- acegi cache-->
<cache name="userCache"
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true"/>

<!-- acegi cache-->
<cache name="resourceCache"
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true"/>
<cache name="paramCache"
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true"/>

</ehcache>


创建这些缓存对象,如何使用嫩


//创建CacheManager对象

  CacheManager manager = CacheManager.getInstance();
            if(manager == null)
                manager = CacheManager.create();
           //通过Cachemanager对象
            cache = manager.getCache("paramCache");
            
            if(cache == null){
                log.error("init cache failed");
                manager.addCache("paramCache");
//获取ehcache.xmL配置文件里的cache对象
                cache = manager.getCache("paramCache");
                
            }
            for(TC09ParamDic c09 : c09List){
                ParamCacheKey key = new ParamCacheKey(c09.getParamName(), c09.getValue());
                cache.put(new Element(key, c09));
            }


1 0
原创粉丝点击