BootstrapCacheLoader不使用spring引导
我将Spring引导与EhCache和Jersey一起使用,我在使BootstrapCacheLoader工作时遇到问题,调试显示加载函数执行并调用该函数(我希望缓存结果)。但一旦应用程序启动,第一个请求仍会调用该函数,并需要时间加载数据,然后在后记中它会很快,即第一个调用大约需要2分钟,然后后续请求需要不到一秒钟的时间 以下是我的实现: ehcache.xmlBootstrapCacheLoader不使用spring引导,spring,ehcache,Spring,Ehcache,我将Spring引导与EhCache和Jersey一起使用,我在使BootstrapCacheLoader工作时遇到问题,调试显示加载函数执行并调用该函数(我希望缓存结果)。但一旦应用程序启动,第一个请求仍会调用该函数,并需要时间加载数据,然后在后记中它会很快,即第一个调用大约需要2分钟,然后后续请求需要不到一秒钟的时间 以下是我的实现: ehcache.xml <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<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>
下面是我对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();
}
}
}
package com.spinners.rest.config;
导入java.util.List;
导入java.util.Map;
导入java.util.Properties;
导入javax.annotation.PostConstruct;
导入org.springframework.beans.factory.annotation.Autowired;
导入com.spinners.rest.repositories.idsmapprepository;
导入net.sf.ehcache.CacheException;
导入net.sf.ehcache.ehcache;
导入net.sf.ehcache.bootstrap.BootstrapCacheLoader;
导入net.sf.ehcache.bootstrap.BootstrapCacheLoaderFactory;
公共类MyBootstrapCacheLoaderFactory扩展BootstrapCacheLoaderFactory实现BootstrapCacheLoader{
@自动连线
私有idsmap存储库idsMapRepo;
公共MyBootstrapCacheLoaderFactory(){
超级();
}
@凌驾
公共BootstrapCacheLoader createBootstrapCacheLoader(属性){
返回新的MyBootstrapCacheLoaderFactory();
}
公共对象克隆()引发CloneNotSupportedException{
返回super.clone();
}
公共布尔同步(){
返回false;
}
公共无效加载(Ehcache Ehcache)引发缓存异常{
试一试{
//要在缓存中加载数据的函数
Map idsMap=idsMapRepo.getIdsMap();
}捕获(例外e){
e、 printStackTrace();
}
}
}
有什么建议吗?提前感谢朋友们
致意
萨吉德
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();
}
}
}