Refactor cache owo

pull/137/head
johnniang 2019-03-23 16:11:07 +08:00
parent c8adb9f8f5
commit 847828cd14
4 changed files with 32 additions and 18 deletions

View File

@ -9,7 +9,7 @@ import cc.ryanc.halo.utils.HaloUtils;
import cc.ryanc.halo.utils.ThemeUtils;
import cc.ryanc.halo.web.controller.content.base.BaseContentController;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import freemarker.template.TemplateModelException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@ -19,13 +19,12 @@ import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.charset.Charset;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static cc.ryanc.halo.model.support.HaloConst.DEFAULT_THEME_NAME;
import static org.springframework.util.ResourceUtils.CLASSPATH_URL_PREFIX;
/**
* <pre>
@ -51,6 +50,9 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
@Autowired
private OptionService optionService;
@Autowired
private ObjectMapper objectMapper;
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
// save halo version to database
@ -84,9 +86,6 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
}
private void printStartInfo() {
// Get server port
String serverPort = applicationContext.getEnvironment().getProperty("server.port");
String blogUrl = getBlogUrl();
log.info("Halo started at {}", blogUrl);
@ -103,7 +102,7 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
*/
private String getBlogUrl() {
// Get server port
String serverPort = applicationContext.getEnvironment().getProperty("server.port");
String serverPort = applicationContext.getEnvironment().getProperty("server.port", "8080");
String blogUrl = optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL);
@ -121,10 +120,14 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
*/
private void cacheOwo() {
try {
File file = new File(ResourceUtils.getURL(CLASSPATH_URL_PREFIX).getPath(), "static/halo-common/OwO/OwO.path.json");
HaloConst.OWO = JSONUtil.readJSONObject(file, Charset.forName("UTF-8"));
} catch (FileNotFoundException e) {
e.printStackTrace();
// The Map is LinkedHashMap
@SuppressWarnings("unchecked")
Map<String, String> owoMap = objectMapper.readValue(ResourceUtils.getURL("classpath:static/halo-common/OwO/OwO.path.json"), Map.class);
HaloConst.OWO_MAP = Collections.unmodifiableMap(owoMap);
} catch (IOException e) {
log.error("Failed to read owo json", e);
// TODO Consider to throw an exception
}
}
}

View File

@ -2,6 +2,7 @@ package cc.ryanc.halo.model.support;
import cn.hutool.json.JSONObject;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -34,8 +35,14 @@ public class HaloConst {
/**
* All of the Owo
*/
@Deprecated
public static JSONObject OWO;
/**
* Owo map. (Unmodified)
*/
public static Map<String, String> OWO_MAP = Collections.emptyMap();
/**
* All of the themes
*/

View File

@ -1,8 +1,9 @@
package cc.ryanc.halo.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import static cc.ryanc.halo.model.support.HaloConst.OWO;
import static cc.ryanc.halo.model.support.HaloConst.OWO_MAP;
/**
* Owo util
@ -20,8 +21,12 @@ public class OwoUtil {
* @return picture address
*/
public static String parseOwo(String content) {
for (String key : OWO.keySet()) {
content = content.replace(key, OWO.get(key).toString());
if (CollectionUtils.isEmpty(OWO_MAP)) {
return content;
}
for (String key : OWO_MAP.keySet()) {
content = content.replace(key, OWO_MAP.get(key).toString());
}
return content;
}

View File

@ -24,7 +24,7 @@ spring:
username: admin
password: 123456
#MySql配置
#MySql配置
# driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://127.0.0.1:3306/halodb?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
# username: root
@ -56,7 +56,6 @@ spring:
multipart:
max-file-size: 10MB
max-request-size: 10MB
cache:
type: ehcache
ehcache: