From 6d1baa0bcb057b9687da99a361b423f2764ad6ab Mon Sep 17 00:00:00 2001 From: hubin6 Date: Fri, 17 Nov 2017 17:13:48 +0800 Subject: [PATCH] cache metric configuration Signed-off-by: hubin6 --- .../data/commons/helper/ModelHelper.java | 51 +++++++++++++------ .../data/implement/ModelServiceImpl.java | 14 +---- 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/dashboard-domain-service/src/main/java/com/jd/logistics/cloud/data/commons/helper/ModelHelper.java b/dashboard-domain-service/src/main/java/com/jd/logistics/cloud/data/commons/helper/ModelHelper.java index b4a1d2a..4accba7 100644 --- a/dashboard-domain-service/src/main/java/com/jd/logistics/cloud/data/commons/helper/ModelHelper.java +++ b/dashboard-domain-service/src/main/java/com/jd/logistics/cloud/data/commons/helper/ModelHelper.java @@ -62,16 +62,21 @@ private static void saveContentToCache (String key, String content) { resourceContent.put(key, content); } - public static BaseModel getBaseModel(String metricId) { + private static String getContentAndSave(String key, String filePath) { String content; - String key = "BaseModel" + metricId; if (getContentFromCache(key) == null){ - content = getStringFromResourcePath(Constants.CONFIG_PARENT_FOLDER + "/" + - metricId + "/" + Constants.CONFIG_MODEL_FILE); + content = getStringFromResourcePath(filePath); saveContentToCache(key, content); } else { content = getContentFromCache(key).toString(); } + return content; + } + + public static BaseModel getBaseModel(String metricId) { + String key = "BaseModel|" + metricId; + String content = getContentAndSave(key, Constants.CONFIG_PARENT_FOLDER + "/" + + metricId + "/" + Constants.CONFIG_MODEL_FILE); BaseModel bm = JSON.parseObject(content, BaseModel.class); return bm; } @@ -133,34 +138,48 @@ public static GridModel getGridModelByDateCycle(String metricId, String dateCycl @NotNull public static String getMetricChartSql(String metricId, String dateCycle) { ChartModel model = getChartModelByDateCycle(metricId, dateCycle); - String key = "MetricChartSql" + metricId + dateCycle; - String content; - if (getContentFromCache(key) == null) { - content = getStringFromResourcePath(Constants.CONFIG_PARENT_FOLDER + "/" + - metricId + "/" + model.getSql()); - saveContentToCache(key, content); - } else { - content = getContentFromCache(key).toString(); - } + String key = "MetricChartSql|" + metricId + '|' + dateCycle; + String content = getContentAndSave(key, Constants.CONFIG_PARENT_FOLDER + "/" + + metricId + "/" + model.getSql()); return content; } @NotNull public static String getMetricValueSql(String metricId, String dateCycle) { ValueModel model = getValueModelByDateCycle(metricId, dateCycle); - return getStringFromResourcePath(Constants.CONFIG_PARENT_FOLDER + "/" + + String key = "MetricValueSql|" + metricId + '|' + dateCycle; + String content = getContentAndSave(key, Constants.CONFIG_PARENT_FOLDER + "/" + metricId + "/" + model.getSql()); + return content; } @NotNull public static String getMetricGridSql(String metricId, String dateCycle) { GridModel model = getGridModelByDateCycle(metricId, dateCycle); - return getStringFromResourcePath(Constants.CONFIG_PARENT_FOLDER + "/" + + String key = "MetricGridSql|" + metricId + '|' + dateCycle; + String content = getContentAndSave(key, Constants.CONFIG_PARENT_FOLDER + "/" + metricId + "/" + model.getSql()); + return content; } public static String getMetricChartOption(String metricId, String dateCycle) { - return getStringFromResourcePath(Constants.CONFIG_PARENT_FOLDER + "/" + + String key = "MetricChartOption|" + metricId + '|' + dateCycle; + String content = getContentAndSave(key, Constants.CONFIG_PARENT_FOLDER + "/" + metricId + "/" + dateCycle + Constants.CHART_OPTION_SUFFIX); + return content; + } + + public static String getMetricChartFormat() { + String key = "MetricChartFormat"; + String content = getContentAndSave(key, Constants.TEMPLATE_PARENT_FOLDER + + "/" + Constants.CHART_TEMPLATE_FILE); + return content; + } + + public static String getMetricValueFormat() { + String key = "MetricValueFormat"; + String content = getContentAndSave(key, Constants.TEMPLATE_PARENT_FOLDER + + "/" + Constants.VALUE_TEMPLATE_FILE); + return content; } } diff --git a/dashboard-domain-service/src/main/java/com/jd/logistics/cloud/data/implement/ModelServiceImpl.java b/dashboard-domain-service/src/main/java/com/jd/logistics/cloud/data/implement/ModelServiceImpl.java index 364b7a6..d77de57 100644 --- a/dashboard-domain-service/src/main/java/com/jd/logistics/cloud/data/implement/ModelServiceImpl.java +++ b/dashboard-domain-service/src/main/java/com/jd/logistics/cloud/data/implement/ModelServiceImpl.java @@ -15,18 +15,8 @@ */ @Service public class ModelServiceImpl implements ModelService { - private static final String chartTemplate = getMetricChartFormat(); - private static final String numberTemplate = getMetricValueFormat(); - - private static String getMetricChartFormat() { - return ModelHelper.getStringFromResourcePath(Constants.TEMPLATE_PARENT_FOLDER + - "/" + Constants.CHART_TEMPLATE_FILE); - } - - private static String getMetricValueFormat() { - return ModelHelper.getStringFromResourcePath(Constants.TEMPLATE_PARENT_FOLDER + - "/" + Constants.VALUE_TEMPLATE_FILE); - } + private static final String chartTemplate = ModelHelper.getMetricChartFormat(); + private static final String numberTemplate = ModelHelper.getMetricValueFormat(); @Override public Map getModel(String metricId, String metricName) {