Commit bfc0c295 authored by Liang Ding's avatar Liang Ding

Fix #12383

parent d3c0584d
<?xml version="1.0" encoding="UTF-8"?>
<!--
Description: Solo POM.
Version: 3.17.2.50, Nov 9, 2017
Version: 3.18.2.50, Dec 16, 2017
Author: <a href="http://88250.b3log.org">Liang Ding</a>
Author: <a href="http://www.annpeter.cn">Ann Peter</a>
Author: <a href="http://vanessa.b3log.org">Vanessa</a>
......@@ -85,6 +85,7 @@
<qiniu.version>7.0.4.1</qiniu.version>
<jetty.version>9.2.9.v20150224</jetty.version>
<commons-cli.version>1.3.1</commons-cli.version>
<commons-codec.version>1.10</commons-codec.version>
<emoji-java.version>3.2.0</emoji-java.version>
<jodd.version>3.6.6</jodd.version>
<snakeyaml.version>1.18</snakeyaml.version>
......@@ -215,6 +216,12 @@
<version>${snakeyaml.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
......
......@@ -19,9 +19,12 @@ import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.profiles.pegdown.Extensions;
import com.vladsch.flexmark.profiles.pegdown.PegdownOptionsAdapter;
import com.vladsch.flexmark.util.options.DataHolder;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.b3log.latke.Latkes;
import org.b3log.latke.cache.Cache;
import org.b3log.latke.cache.CacheFactory;
import org.b3log.latke.ioc.LatkeBeanManagerImpl;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
......@@ -30,6 +33,7 @@ import org.b3log.latke.service.LangPropsServiceImpl;
import org.b3log.latke.util.Callstacks;
import org.b3log.latke.util.Stopwatchs;
import org.b3log.latke.util.Strings;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
......@@ -48,7 +52,7 @@ import java.util.concurrent.*;
* </p>
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 2.2.0.6, Dec 16, 2017
* @version 2.3.0.6, Dec 16, 2017
* @since 0.4.5
*/
public final class Markdowns {
......@@ -63,6 +67,11 @@ public final class Markdowns {
*/
private static final LangPropsService LANG_PROPS_SERVICE = LatkeBeanManagerImpl.getInstance().getReference(LangPropsServiceImpl.class);
/**
* Markdown cache.
*/
private static final Cache MD_CACHE = CacheFactory.getCache("markdown");
/**
* Markdown to HTML timeout.
*/
......@@ -98,6 +107,8 @@ public final class Markdowns {
public static boolean MARKED_AVAILABLE;
static {
MD_CACHE.setMaxCount(1024 * 10 * 4);
try {
final URL url = new URL(MARKED_ENGINE_URL);
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
......@@ -144,6 +155,11 @@ public final class Markdowns {
return "";
}
final String cachedHTML = getHTML(markdownText);
if (null != cachedHTML) {
return cachedHTML;
}
final ExecutorService pool = Executors.newSingleThreadExecutor();
final long[] threadId = new long[1];
......@@ -177,6 +193,9 @@ public final class Markdowns {
String ret = doc.select("body").html();
ret = StringUtils.trim(ret);
// cache it
putHTML(markdownText, ret);
return ret;
};
......@@ -225,4 +244,33 @@ public final class Markdowns {
return html;
}
/**
* Gets HTML for the specified markdown text.
*
* @param markdownText the specified markdown text
* @return HTML
*/
private static String getHTML(final String markdownText) {
final String hash = DigestUtils.md5Hex(markdownText);
final JSONObject value = MD_CACHE.get(hash);
if (null == value) {
return null;
}
return value.optString("data");
}
/**
* Puts the specified HTML into cache.
*
* @param markdownText the specified markdown text
* @param html the specified HTML
*/
private static void putHTML(final String markdownText, final String html) {
final String hash = DigestUtils.md5Hex(markdownText);
final JSONObject value = new JSONObject();
value.put("data", html);
MD_CACHE.put(hash, value);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment