2

我是 Ehcache 和 Slf4j 的新手。我正在使用 ehcache-2.6.6

我用过

slf4j-api-1.6.1 罐子

slf4j-jdk14-1.6.1 罐子

当我提取 ehcache-2.6.6-distribution.tar 时,这两个 jar 位于文件夹 lib 中。

这是我的 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 版本与错误绑定的版本不匹配, 但我无法得到错误

我在这里看到了类似的错误EhCache: Simple Program not working 但它涉及分布式缓存。对于我参考 Java/Ehcache 的代码:A simple example of use of Ehcache 2.6.2

任何帮助请为什么我一开始就出错?

4

1 回答 1

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

于 2013-09-23T06:20:39.293 回答