1
14
15 package com.liferay.portlet.wiki.util;
16
17 import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
18 import com.liferay.portal.kernel.cache.PortalCache;
19 import com.liferay.portal.kernel.log.Log;
20 import com.liferay.portal.kernel.log.LogFactoryUtil;
21 import com.liferay.portal.kernel.util.StringBundler;
22 import com.liferay.portal.kernel.util.StringPool;
23 import com.liferay.portlet.wiki.PageContentException;
24 import com.liferay.portlet.wiki.model.WikiPage;
25 import com.liferay.portlet.wiki.model.WikiPageDisplay;
26 import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
27
28 import java.util.Map;
29
30 import javax.portlet.PortletURL;
31
32 import org.apache.commons.lang.time.StopWatch;
33
34
39 public class WikiCacheUtil {
40
41 public static final String CACHE_NAME = WikiCacheUtil.class.getName();
42
43 public static void clearCache(long nodeId) {
44 _portalCache.removeAll();
45 }
46
47 public static void clearCache(long nodeId, String title) {
48 clearCache(nodeId);
49 }
50
51 public static WikiPageDisplay getDisplay(
52 long nodeId, String title, PortletURL viewPageURL,
53 PortletURL editPageURL, String attachmentURLPrefix) {
54
55 StopWatch stopWatch = null;
56
57 if (_log.isDebugEnabled()) {
58 stopWatch = new StopWatch();
59
60 stopWatch.start();
61 }
62
63 String key = _encodeKey(nodeId, title, viewPageURL.toString());
64
65 WikiPageDisplay pageDisplay = (WikiPageDisplay)_portalCache.get(key);
66
67 if (pageDisplay == null) {
68 pageDisplay = _getPageDisplay(
69 nodeId, title, viewPageURL, editPageURL, attachmentURLPrefix);
70
71 _portalCache.put(key, pageDisplay);
72 }
73
74 if (_log.isDebugEnabled()) {
75 _log.debug(
76 "getDisplay for {" + nodeId + ", " + title + ", " +
77 viewPageURL + ", " + editPageURL + "} takes " +
78 stopWatch.getTime() + " ms");
79 }
80
81 return pageDisplay;
82 }
83
84 public static Map<String, Boolean> getOutgoingLinks(WikiPage page)
85 throws PageContentException {
86
87 String key = _encodeKey(
88 page.getNodeId(), page.getTitle(), _OUTGOING_LINKS);
89
90 Map<String, Boolean> links = (Map<String, Boolean>)_portalCache.get(
91 key);
92
93 if (links == null) {
94 links = WikiUtil.getLinks(page);
95
96 _portalCache.put(key, links);
97 }
98
99 return links;
100 }
101
102 private static String _encodeKey(
103 long nodeId, String title, String postfix) {
104
105 StringBundler sb = new StringBundler(6);
106
107 sb.append(CACHE_NAME);
108 sb.append(StringPool.POUND);
109 sb.append(nodeId);
110 sb.append(title);
111
112 if (postfix != null) {
113 sb.append(StringPool.POUND);
114 sb.append(postfix);
115 }
116
117 return sb.toString();
118 }
119
120 private static WikiPageDisplay _getPageDisplay(
121 long nodeId, String title, PortletURL viewPageURL,
122 PortletURL editPageURL, String attachmentURLPrefix) {
123
124 try {
125 if (_log.isInfoEnabled()) {
126 _log.info(
127 "Get page display for {" + nodeId + ", " + title + ", " +
128 viewPageURL + ", " + editPageURL + "}");
129 }
130
131 return WikiPageLocalServiceUtil.getPageDisplay(
132 nodeId, title, viewPageURL, editPageURL, attachmentURLPrefix);
133 }
134 catch (Exception e) {
135 if (_log.isWarnEnabled()) {
136 _log.warn(
137 "Unable to get page display for {" + nodeId + ", " + title +
138 ", " + viewPageURL + ", " + editPageURL + "}");
139 }
140
141 return null;
142 }
143 }
144
145 private static final String _OUTGOING_LINKS = "OUTGOING_LINKS";
146
147 private static Log _log = LogFactoryUtil.getLog(WikiUtil.class);
148
149 private static PortalCache _portalCache = MultiVMPoolUtil.getCache(
150 CACHE_NAME);
151
152 }