001
014
015 package com.liferay.portlet.wiki.util;
016
017 import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
018 import com.liferay.portal.kernel.cache.PortalCache;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.util.StringBundler;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portlet.wiki.PageContentException;
024 import com.liferay.portlet.wiki.model.WikiPage;
025 import com.liferay.portlet.wiki.model.WikiPageDisplay;
026 import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
027
028 import java.util.Map;
029
030 import javax.portlet.PortletURL;
031
032 import org.apache.commons.lang.time.StopWatch;
033
034
037 public class WikiCacheUtil {
038
039 public static final String CACHE_NAME = WikiCacheUtil.class.getName();
040
041 public static void clearCache(long nodeId) {
042 _portalCache.removeAll();
043 }
044
045 public static void clearCache(long nodeId, String title) {
046 clearCache(nodeId);
047 }
048
049 public static WikiPageDisplay getDisplay(
050 long nodeId, String title, PortletURL viewPageURL,
051 PortletURL editPageURL, String attachmentURLPrefix) {
052
053 StopWatch stopWatch = null;
054
055 if (_log.isDebugEnabled()) {
056 stopWatch = new StopWatch();
057
058 stopWatch.start();
059 }
060
061 String key = _encodeKey(nodeId, title, viewPageURL.toString());
062
063 WikiPageDisplay pageDisplay = (WikiPageDisplay)_portalCache.get(key);
064
065 if (pageDisplay == null) {
066 pageDisplay = _getPageDisplay(
067 nodeId, title, viewPageURL, editPageURL, attachmentURLPrefix);
068
069 _portalCache.put(key, pageDisplay);
070 }
071
072 if (_log.isDebugEnabled()) {
073 _log.debug(
074 "getDisplay for {" + nodeId + ", " + title + ", " +
075 viewPageURL + ", " + editPageURL + "} takes " +
076 stopWatch.getTime() + " ms");
077 }
078
079 return pageDisplay;
080 }
081
082 public static Map<String, Boolean> getOutgoingLinks(WikiPage page)
083 throws PageContentException {
084
085 String key = _encodeKey(
086 page.getNodeId(), page.getTitle(), _OUTGOING_LINKS);
087
088 Map<String, Boolean> links = (Map<String, Boolean>)_portalCache.get(
089 key);
090
091 if (links == null) {
092 links = WikiUtil.getLinks(page);
093
094 _portalCache.put(key, links);
095 }
096
097 return links;
098 }
099
100 private static String _encodeKey(
101 long nodeId, String title, String postfix) {
102
103 StringBundler sb = new StringBundler(6);
104
105 sb.append(CACHE_NAME);
106 sb.append(StringPool.POUND);
107 sb.append(nodeId);
108 sb.append(title);
109
110 if (postfix != null) {
111 sb.append(StringPool.POUND);
112 sb.append(postfix);
113 }
114
115 return sb.toString();
116 }
117
118 private static WikiPageDisplay _getPageDisplay(
119 long nodeId, String title, PortletURL viewPageURL,
120 PortletURL editPageURL, String attachmentURLPrefix) {
121
122 try {
123 if (_log.isInfoEnabled()) {
124 _log.info(
125 "Get page display for {" + nodeId + ", " + title + ", " +
126 viewPageURL + ", " + editPageURL + "}");
127 }
128
129 return WikiPageLocalServiceUtil.getPageDisplay(
130 nodeId, title, viewPageURL, editPageURL, attachmentURLPrefix);
131 }
132 catch (Exception e) {
133 if (_log.isWarnEnabled()) {
134 _log.warn(
135 "Unable to get page display for {" + nodeId + ", " + title +
136 ", " + viewPageURL + ", " + editPageURL + "}");
137 }
138
139 return null;
140 }
141 }
142
143 private static final String _OUTGOING_LINKS = "OUTGOING_LINKS";
144
145 private static Log _log = LogFactoryUtil.getLog(WikiUtil.class);
146
147 private static PortalCache _portalCache = MultiVMPoolUtil.getCache(
148 CACHE_NAME);
149
150 }