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