1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.macro.cache; |
21 |
|
|
22 |
|
import java.util.List; |
23 |
|
import java.util.Map; |
24 |
|
import java.util.concurrent.ConcurrentHashMap; |
25 |
|
|
26 |
|
import javax.inject.Inject; |
27 |
|
import javax.inject.Named; |
28 |
|
import javax.inject.Singleton; |
29 |
|
|
30 |
|
import org.xwiki.cache.Cache; |
31 |
|
import org.xwiki.cache.CacheException; |
32 |
|
import org.xwiki.cache.CacheManager; |
33 |
|
import org.xwiki.cache.config.LRUCacheConfiguration; |
34 |
|
import org.xwiki.component.annotation.Component; |
35 |
|
import org.xwiki.rendering.block.Block; |
36 |
|
import org.xwiki.rendering.macro.AbstractMacro; |
37 |
|
import org.xwiki.rendering.macro.MacroContentParser; |
38 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
39 |
|
import org.xwiki.rendering.macro.cache.CacheMacroParameters; |
40 |
|
import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor; |
41 |
|
import org.xwiki.rendering.renderer.BlockRenderer; |
42 |
|
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter; |
43 |
|
import org.xwiki.rendering.renderer.printer.WikiPrinter; |
44 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
@version |
50 |
|
@since |
51 |
|
|
52 |
|
@Component |
53 |
|
@Named("cache") |
54 |
|
@Singleton |
|
|
| 80% |
Uncovered Elements: 7 (35) |
Complexity: 8 |
Complexity Density: 0.32 |
|
55 |
|
public class CacheMacro extends AbstractMacro<CacheMacroParameters> |
56 |
|
{ |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
private static final String DESCRIPTION = "Caches content."; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
private static final String CONTENT_DESCRIPTION = "the content to cache."; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
@Inject |
71 |
|
private CacheManager cacheManager; |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@Inject |
77 |
|
private MacroContentParser contentParser; |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
@Inject |
83 |
|
@Named("plain/1.0") |
84 |
|
private BlockRenderer plainTextBlockRenderer; |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
private Map<CacheKey, Cache<List<Block>>> contentCacheMap = new ConcurrentHashMap<>(); |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
95 |
6 |
public CacheMacro()... |
96 |
|
{ |
97 |
6 |
super("Cache", DESCRIPTION, new DefaultContentDescriptor(CONTENT_DESCRIPTION), CacheMacroParameters.class); |
98 |
6 |
setDefaultCategory(DEFAULT_CATEGORY_DEVELOPMENT); |
99 |
|
} |
100 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
101 |
0 |
@Override... |
102 |
|
public boolean supportsInlineMode() |
103 |
|
{ |
104 |
0 |
return true; |
105 |
|
} |
106 |
|
|
|
|
| 81.2% |
Uncovered Elements: 3 (16) |
Complexity: 3 |
Complexity Density: 0.25 |
|
107 |
4 |
@Override... |
108 |
|
public List<Block> execute(CacheMacroParameters parameters, String content, MacroTransformationContext context) |
109 |
|
throws MacroExecutionException |
110 |
|
{ |
111 |
|
|
112 |
|
|
113 |
4 |
String cacheKey; |
114 |
4 |
if (parameters.getId() != null) { |
115 |
|
|
116 |
|
|
117 |
4 |
WikiPrinter printer = new DefaultWikiPrinter(); |
118 |
4 |
this.plainTextBlockRenderer.render(this.contentParser.parse(parameters.getId(), context, true, false), |
119 |
|
printer); |
120 |
4 |
cacheKey = printer.toString(); |
121 |
|
} else { |
122 |
0 |
cacheKey = content; |
123 |
|
} |
124 |
|
|
125 |
4 |
Cache<List<Block>> contentCache = getContentCache(parameters.getTimeToLive(), parameters.getMaxEntries()); |
126 |
4 |
List<Block> result = contentCache.get(cacheKey); |
127 |
4 |
if (result == null) { |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
4 |
result = this.contentParser.parse(content, context, true, context.isInline()).getChildren(); |
132 |
4 |
contentCache.set(cacheKey, result); |
133 |
|
} |
134 |
|
|
135 |
4 |
return result; |
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
@param |
147 |
|
@param |
148 |
|
@return |
149 |
|
@throws |
150 |
|
|
|
|
| 83.3% |
Uncovered Elements: 2 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
151 |
4 |
Cache<List<Block>> getContentCache(int lifespan, int maxEntries) throws MacroExecutionException... |
152 |
|
{ |
153 |
4 |
CacheKey cacheKey = new CacheKey(lifespan, maxEntries); |
154 |
4 |
Cache<List<Block>> contentCache = this.contentCacheMap.get(cacheKey); |
155 |
4 |
if (contentCache == null) { |
156 |
|
|
157 |
4 |
LRUCacheConfiguration configuration = |
158 |
|
new LRUCacheConfiguration(String.format("cacheMacro.%s", cacheKey.toString()), maxEntries); |
159 |
4 |
configuration.getLRUEvictionConfiguration().setLifespan(lifespan); |
160 |
|
|
161 |
4 |
try { |
162 |
4 |
contentCache = this.cacheManager.createNewLocalCache(configuration); |
163 |
|
} catch (CacheException e) { |
164 |
0 |
throw new MacroExecutionException("Failed to create content cache", e); |
165 |
|
} |
166 |
|
|
167 |
4 |
this.contentCacheMap.put(cacheKey, contentCache); |
168 |
|
} |
169 |
|
|
170 |
4 |
return contentCache; |
171 |
|
} |
172 |
|
} |