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

View File

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

View File

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