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