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