2013-05-15 77 views
1

我是新來的Ehcache和SLF4J。我現在用的Ehcache-2.6.6的Ehcache和SLF4J錯誤

我用

SLF4J-API-1.6.1罐子

SLF4J -jdk14-1.6.1罐子

當我提取ehcache-2.6.6-distribution.tar這兩個罐子在文件夾庫。

這是我ehcache.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:noNamespaceSchemaLocation="ehcache.xsd" 
     updateCheck="true" monitoring="autodetect" 
     dynamicConfig="true"> 


    <diskStore path="java.io.tmpdir"/> 

    <defaultCache 
      maxEntriesLocalHeap="10000" 
      eternal="false" 
      timeToIdleSeconds="120" 
      timeToLiveSeconds="120" 
      diskSpoolBufferSizeMB="30" 
      maxEntriesLocalDisk="10000000" 
      diskExpiryThreadIntervalSeconds="120" 
      memoryStoreEvictionPolicy="LRU" 
      statistics="false"> 
     <persistence strategy="localTempSwap"/> 
    </defaultCache> 

    <cache name="myCache1" 
      maxEntriesLocalHeap="10000" 
      maxEntriesLocalDisk="1000" 
      eternal="false" 
      diskSpoolBufferSizeMB="20" 
      timeToIdleSeconds="300" 
      timeToLiveSeconds="600" 
      memoryStoreEvictionPolicy="LFU" 
      transactionalMode="off"> 
     <persistence strategy="localTempSwap"/> 
    </cache> 

</ehcache> 

這是我的代碼

package cache; 

import java.util.ArrayList; 
import java.util.List; 

import net.sf.ehcache.CacheManager; 
import net.sf.ehcache.Ehcache; 
import net.sf.ehcache.Element; 

public class EhcacheWrapper { 
    private static CacheManager cacheManager; 
    private static final String CACHE_NAME = "myCache1"; 






    private static Ehcache getCache(String cacheName) { 
     if (cacheManager == null) { 
      cacheManager = CacheManager.create("ehcache.xml"); 
     } 

     Ehcache cache = null; 
     if (cacheManager != null) { 

      cache = cacheManager.getEhcache(cacheName); 
     } 

     return cache; 
    } 

    public static <T> List<T> getListFromCache(String cacheName, String key, CacheCreation<T> cacheCreation){ 
     List<T> all = new ArrayList<T>(); 

     Ehcache cache = getCache(cacheName); 
     Element element = null; 
     if(cache!=null){ 
      element = cache.get(key); 
     } 

     if(element==null){ 
      System.out.println(" : CacheUtil.getListFromCache() : the element '"+key+"' has not been found in the cache ---> get the original data."); 

      all = cacheCreation.getAll(); 
      cache.put(new Element(key, all)); 
      System.out.println(" : CacheUtil.getListFromCache() : the original data for the element '"+key+"' has been added in the cache."); 


     }else{ 
      System.out.println(" : CacheUtil.getListFromCache() : the element '"+key+"' has been found in the cache."); 


      all = (List<T>) element.getObjectValue(); 
     } 
     return all; 

    } 
    public List<String> getAllData1(){ 
     return getListFromCache(CACHE_NAME, "data1", new CacheCreation<String>(){ 
      @Override 
      public List<String> getAll(){ 
       System.out.println(" : UseCaseClass.getAllData1() : the target original method is called to get the values."); 
       List<String> list = new ArrayList<String>(); 
       list.add("data1-value1"); 
       list.add("data1-value2"); 
       list.add("data1-value3"); 
       list.add("data1-value4"); 
       return list; 
      } 
     }); 
    } 



    public static void main(String[] args) { 
     EhcacheWrapper wrappertest=new EhcacheWrapper(); 
     wrappertest.getAllData1(); 
     try { 
      Thread.sleep(1500); 
     } catch (InterruptedException e) { 


     } 
     wrappertest.getAllData1(); 
     try { 
      Thread.sleep(1500); 
     } catch (InterruptedException e) { 

     } 
     wrappertest.getAllData1(); 
     try { 
      Thread.sleep(1500); 
     } catch (InterruptedException e) { 

     } 




    } 
} 

當我運行程序我得到下面的輸出

SLF4J: The requested version 1.6 by your slf4j binding is not compatible with [1.5.5, 1.5.6, 1.5.7, 1.5.8, 1.5.9, 1.5.10, 1.5.11] 
SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details. 
May 15, 2013 1:52:36 PM net.sf.ehcache.DiskStorePathManager resolveAndLockIfNeeded 
WARNING: diskStorePath 'C:\Users\toshiba\AppData\Local\Temp' is already used by an existing CacheManager either in the same VM or in a different process. 
The diskStore path for this CacheManager will be set to C:\Users\toshiba\AppData\Local\Temp\ehcache_auto_created3199473242323720768diskstore. 
To avoid this warning consider using the CacheManager factory methods to create a singleton CacheManager or specifying a separate ehcache configuration (ehcache.xml) for each CacheManager instance. 
: CacheUtil.getListFromCache() : the element 'data1' has not been found in the cache ---> get the original data. 
: UseCaseClass.getAllData1() : the target original method is called to get the values. 
: CacheUtil.getListFromCache() : the original data for the element 'data1' has been added in the cache. 
: CacheUtil.getListFromCache() : the element 'data1' has been found in the cache. 
: CacheUtil.getListFromCache() : the element 'data1' has been found in the cache. 

雖然我得到預期輸出但開始時會出現錯誤:

SLF4J: The requested version 1.6 by your slf4j binding is not compatible with [1.5.5, 1.5.6, 1.5.7, 1.5.8, 1.5.9, 1.5.10, 1.5.11] 
SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details. 
May 15, 2013 1:52:36 PM net.sf.ehcache.DiskStorePathManager resolveAndLockIfNeeded 
WARNING: diskStorePath 'C:\Users\toshiba\AppData\Local\Temp' is already used by an existing CacheManager either in the same VM or in a different process. 
The diskStore path for this CacheManager will be set to C:\Users\toshiba\AppData\Local\Temp\ehcache_auto_created3199473242323720768diskstore. 
To avoid this warning consider using the CacheManager factory methods to create a singleton CacheManager or specifying a separate ehcache configuration (ehcache.xml) for each CacheManager instance. 

我期待在 slf4j-api version does not match that of the binding 的錯誤,但我不能爲這

得到的錯誤,我在這裏看到了類似的錯誤EhCache: Simple Program not working 但它正在採取有關分佈式緩存。 代碼我參考 Java/Ehcache: A simple example of use of Ehcache 2.6.2

任何幫助,請問爲什麼我開始出錯?

回答

0

正如警告說

To avoid this warning consider using the CacheManager factory methods to create a singleton CacheManager or specifying a separate ehcache configuration (ehcache.xml) for each CacheManager instance. 

使用SingletonEhCacheProvider可能會解決您的問題,下面一行

<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</property> 

SingletonEhCacheProvider