BootstrapCacheLoader无法使用spring boot

时间:2016-04-11 16:16:26

标签: spring ehcache

我正在使用带有EhCache和Jersey的Spring Boot,我遇到了使BootstrapCacheLoader工作的问题,调试显示加载函数执行并调用函数(我想要缓存结果)。但是一旦应用程序启动,第一个请求仍然会调用该函数,并且需要时间加载数据,然后是快速的,即第一次调用大约需要2分钟,然后跟随请求的时间不到一秒钟。

这是我的实施:

ehcache.xml中

<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" />

    <cache name="idsMap" 
        maxEntriesLocalHeap="1000"
        maxEntriesLocalDisk="10000" 
        eternal="true" 
        diskSpoolBufferSizeMB="20"
        memoryStoreEvictionPolicy="LFU" 
        transactionalMode="off">
        <persistence strategy="localTempSwap" />
    </cache>

</ehcache>

EhCache配置

package com.spinners.rest.config;

import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheFactoryBean;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

/**
 * @author sajid
 *
 */
@Configuration
@EnableCaching
public class EhCacheConfig {

    @Autowired
    CacheManager cacheManager;

    @Bean
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
        EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
        ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
        return ehCacheManagerFactoryBean;
    }

    @Bean
    public CacheManager cacheManager() {
        EhCacheCacheManager cacheManager = new EhCacheCacheManager();
        cacheManager.setCacheManager(ehCacheManagerFactoryBean().getObject());
        return cacheManager;
    }

    @Bean
    public MyBootstrapCacheLoaderFactory myBootstrapCacheLoaderFactory() {
        return new MyBootstrapCacheLoaderFactory();
    }

    @Bean
    public EhCacheFactoryBean ehCacheFactory() {
        EhCacheFactoryBean ehCacheFactory = new EhCacheFactoryBean();
        ehCacheFactory.setCacheManager(ehCacheManagerFactoryBean().getObject());
        ehCacheFactory.setBootstrapCacheLoader(myBootstrapCacheLoaderFactory());
        return ehCacheFactory;
    }

}

这是我的BootstrapCacheLoader

的实现
package com.spinners.rest.config;

import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;

import com.spinners.rest.repositories.IdsMapRepository;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.bootstrap.BootstrapCacheLoader;
import net.sf.ehcache.bootstrap.BootstrapCacheLoaderFactory;

public class MyBootstrapCacheLoaderFactory extends BootstrapCacheLoaderFactory implements BootstrapCacheLoader {

    @Autowired
    private IdsMapRepository idsMapRepo;

    public MyBootstrapCacheLoaderFactory() {
        super();
    }

    @Override
    public BootstrapCacheLoader createBootstrapCacheLoader(Properties properties) {
        return new MyBootstrapCacheLoaderFactory();
    }

    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public boolean isAsynchronous() {
        return false;
    }

    public void load(Ehcache ehCache) throws CacheException {   

        try {
            // function for which I want to load data in cache
            Map<String, String> idsMap = idsMapRepo.getIdsMap();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

有什么建议吗?提前感谢朋友们。

最诚挚的问候 Sajid

0 个答案:

没有答案