提交 b9e53a5a 编写于 作者: O o2sword

数据中心应用缓存优化2

上级 71ae7b37
...@@ -7,35 +7,34 @@ import java.util.stream.Collectors; ...@@ -7,35 +7,34 @@ import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import com.x.base.core.project.cache.ApplicationCache;
import com.x.base.core.project.tools.StringTools; import com.x.base.core.project.tools.StringTools;
import com.x.query.core.entity.Query; import com.x.query.core.entity.Query;
import com.x.query.service.processing.AbstractFactory; import com.x.query.service.processing.AbstractFactory;
import com.x.query.service.processing.Business; import com.x.query.service.processing.Business;
import com.x.base.core.project.cache.Cache.CacheCategory;
import net.sf.ehcache.Ehcache; import com.x.base.core.project.cache.Cache.CacheKey;
import net.sf.ehcache.Element; import com.x.base.core.project.cache.CacheManager;
import java.util.Optional;
public class QueryFactory extends AbstractFactory { public class QueryFactory extends AbstractFactory {
private Ehcache cache; private CacheCategory cache;
public QueryFactory(Business business) throws Exception { public QueryFactory(Business business) throws Exception {
super(business); super(business);
this.cache = ApplicationCache.instance().getCache(Query.class); this.cache = new CacheCategory(Query.class);
} }
public List<Query> pick(List<String> flags) throws Exception { public List<Query> pick(List<String> flags) throws Exception {
List<Query> list = new ArrayList<>(); List<Query> list = new ArrayList<>();
for (String str : flags) { for (String str : flags) {
Element element = cache.get(str); CacheKey cacheKey = new CacheKey(str);
if (null != element) { Optional<?> optional = CacheManager.get(cache, cacheKey);
if (null != element.getObjectValue()) { if (optional.isPresent()) {
list.add((Query) element.getObjectValue()); list.add((Query) optional.get());
}
} else { } else {
Query o = this.pickObject(str); Query o = this.pickObject(str);
cache.put(new Element(str, o)); CacheManager.put(cache, cacheKey, o);
if (null != o) { if (null != o) {
list.add(o); list.add(o);
} }
...@@ -49,14 +48,13 @@ public class QueryFactory extends AbstractFactory { ...@@ -49,14 +48,13 @@ public class QueryFactory extends AbstractFactory {
return null; return null;
} }
Query o = null; Query o = null;
Element element = cache.get(flag); CacheKey cacheKey = new CacheKey(flag);
if (null != element) { Optional<?> optional = CacheManager.get(cache, cacheKey);
if (null != element.getObjectValue()) { if (optional.isPresent()) {
o = (Query) element.getObjectValue(); o = (Query) optional.get();
}
} else { } else {
o = this.pickObject(flag); o = this.pickObject(flag);
cache.put(new Element(flag, o)); CacheManager.put(cache, cacheKey, o);
} }
return o; return o;
} }
......
...@@ -20,7 +20,6 @@ import com.x.base.core.container.EntityManagerContainer; ...@@ -20,7 +20,6 @@ import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory; import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.entity.dataitem.DataItemConverter; import com.x.base.core.entity.dataitem.DataItemConverter;
import com.x.base.core.entity.dataitem.ItemCategory; import com.x.base.core.entity.dataitem.ItemCategory;
import com.x.base.core.project.cache.ApplicationCache;
import com.x.base.core.project.config.Config; import com.x.base.core.project.config.Config;
import com.x.base.core.project.config.StorageMapping; import com.x.base.core.project.config.StorageMapping;
import com.x.base.core.project.exception.ExceptionEntityNotExist; import com.x.base.core.project.exception.ExceptionEntityNotExist;
...@@ -43,8 +42,9 @@ import com.x.query.service.processing.Business; ...@@ -43,8 +42,9 @@ import com.x.query.service.processing.Business;
import com.x.query.service.processing.ThisApplication; import com.x.query.service.processing.ThisApplication;
import com.x.query.service.processing.helper.ExtractTextHelper; import com.x.query.service.processing.helper.ExtractTextHelper;
import com.x.query.service.processing.helper.LanguageProcessingHelper; import com.x.query.service.processing.helper.LanguageProcessingHelper;
import com.x.base.core.project.cache.Cache.CacheKey;
import net.sf.ehcache.Element; import com.x.base.core.project.cache.CacheManager;
import java.util.Optional;
class ActionListCalculateWithWork extends BaseAction { class ActionListCalculateWithWork extends BaseAction {
...@@ -63,10 +63,10 @@ class ActionListCalculateWithWork extends BaseAction { ...@@ -63,10 +63,10 @@ class ActionListCalculateWithWork extends BaseAction {
throw new ExceptionModelNotReady(model.getName()); throw new ExceptionModelNotReady(model.getName());
} }
NeuralNetwork<MomentumBackpropagation> neuralNetwork = null; NeuralNetwork<MomentumBackpropagation> neuralNetwork = null;
String cacheKey = ApplicationCache.concreteCacheKey(this.getClass(), model.getId()); CacheKey cacheKey = new CacheKey(this.getClass(), model.getId());
Element element = cache.get(cacheKey); Optional<?> optional = CacheManager.get(cache, cacheKey);
if (null != element && (null != element.getObjectValue())) { if (optional.isPresent()) {
neuralNetwork = ((NeuralNetwork<MomentumBackpropagation>) element.getObjectValue()); neuralNetwork = ((NeuralNetwork<MomentumBackpropagation>) optional.get());
} else { } else {
if (StringUtils.isEmpty(model.getNnet())) { if (StringUtils.isEmpty(model.getNnet())) {
throw new ExceptionModelNotReady(model.getName()); throw new ExceptionModelNotReady(model.getName());
...@@ -75,7 +75,7 @@ class ActionListCalculateWithWork extends BaseAction { ...@@ -75,7 +75,7 @@ class ActionListCalculateWithWork extends BaseAction {
NeuralNetworkCODEC.array2network( NeuralNetworkCODEC.array2network(
DoubleTools.byteToDoubleArray(ByteTools.decompressBase64String(model.getNnet())), DoubleTools.byteToDoubleArray(ByteTools.decompressBase64String(model.getNnet())),
neuralNetwork); neuralNetwork);
cache.put(new Element(cacheKey, neuralNetwork)); CacheManager.put(cache, cacheKey, neuralNetwork);
} }
Wo wo = new Wo(); Wo wo = new Wo();
Work work = emc.flag(workId, Work.class); Work work = emc.flag(workId, Work.class);
......
package com.x.query.service.processing.jaxrs.neural; package com.x.query.service.processing.jaxrs.neural;
import com.x.base.core.project.cache.ApplicationCache;
import com.x.base.core.project.jaxrs.StandardJaxrsAction; import com.x.base.core.project.jaxrs.StandardJaxrsAction;
import com.x.base.core.project.logger.Logger; import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory; import com.x.base.core.project.logger.LoggerFactory;
import com.x.query.core.entity.neural.Model; import com.x.query.core.entity.neural.Model;
import com.x.base.core.project.cache.Cache.CacheCategory;
import net.sf.ehcache.Ehcache;
abstract class BaseAction extends StandardJaxrsAction { abstract class BaseAction extends StandardJaxrsAction {
protected Ehcache cache = ApplicationCache.instance().getCache(Model.class); protected CacheCategory cache = new CacheCategory(Model.class);
private static Logger logger = LoggerFactory.getLogger(BaseAction.class); private static Logger logger = LoggerFactory.getLogger(BaseAction.class);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册