1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.module.cache.ehcache3; |
35 |
|
|
36 |
|
import info.magnolia.cms.core.Path; |
37 |
|
import info.magnolia.init.MagnoliaConfigurationProperties; |
38 |
|
import info.magnolia.init.MagnoliaInitPaths; |
39 |
|
import info.magnolia.module.cache.Cache; |
40 |
|
import info.magnolia.module.cache.CacheFactory; |
41 |
|
import info.magnolia.module.cache.CacheModule; |
42 |
|
import info.magnolia.module.cache.ehcache3.configuration.EhCache3Expiry; |
43 |
|
|
44 |
|
import java.io.Serializable; |
45 |
|
import java.util.ArrayList; |
46 |
|
import java.util.HashMap; |
47 |
|
import java.util.List; |
48 |
|
import java.util.Map; |
49 |
|
|
50 |
|
import javax.inject.Inject; |
51 |
|
|
52 |
|
import org.ehcache.CacheManager; |
53 |
|
import org.ehcache.Status; |
54 |
|
import org.ehcache.config.Builder; |
55 |
|
import org.ehcache.config.CacheConfiguration; |
56 |
|
import org.ehcache.config.builders.CacheConfigurationBuilder; |
57 |
|
import org.ehcache.config.builders.CacheManagerBuilder; |
58 |
|
import org.ehcache.config.builders.ResourcePoolsBuilder; |
59 |
|
import org.ehcache.config.units.EntryUnit; |
60 |
|
import org.slf4j.Logger; |
61 |
|
import org.slf4j.LoggerFactory; |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
|
|
| 89.6% |
Uncovered Elements: 5 (48) |
Complexity: 13 |
Complexity Density: 0.42 |
|
66 |
|
public class EhCache3Factory implements CacheFactory { |
67 |
|
|
68 |
|
private static final Logger log = LoggerFactory.getLogger(EhCache3Factory.class); |
69 |
|
|
70 |
|
static final String MAGNOLIA_PROPERTY_CACHE_MANAGER_ID = "magnolia.cache.manager.id"; |
71 |
|
|
72 |
|
private Map<String, Builder<? extends CacheConfiguration>> caches = new HashMap<>(); |
73 |
|
private final Map<String, CacheConfiguration> cachesConfiguration = new HashMap<>(); |
74 |
|
private CacheManager cacheManager; |
75 |
|
|
76 |
|
private int blockingTimeout = 10000; |
77 |
|
private String diskStorePath; |
78 |
|
|
79 |
|
private final CacheModule cacheModule; |
80 |
|
private final MagnoliaInitPaths magnoliaInitPaths; |
81 |
|
private final MagnoliaConfigurationProperties magnoliaConfigurationProperties; |
82 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
83 |
7 |
@Inject... |
84 |
|
public EhCache3Factory(CacheModule cacheModule, MagnoliaInitPaths magnoliaInitPaths, MagnoliaConfigurationProperties magnoliaConfigurationProperties) { |
85 |
7 |
this.cacheModule = cacheModule; |
86 |
7 |
this.magnoliaInitPaths = magnoliaInitPaths; |
87 |
7 |
this.magnoliaConfigurationProperties = magnoliaConfigurationProperties; |
88 |
7 |
this.diskStorePath = Path.getCacheDirectoryPath() + getCacheManagerIdentifier(); |
89 |
7 |
getCaches().put(DEFAULT_CACHE_NAME, |
90 |
|
CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class, |
91 |
|
ResourcePoolsBuilder.newResourcePoolsBuilder() |
92 |
|
.heap(10000, EntryUnit.ENTRIES) |
93 |
|
).withExpiry(new EhCache3Expiry<>()) |
94 |
|
); |
95 |
|
} |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
97 |
7 |
public void init() {... |
98 |
7 |
this.cacheManager = CacheManagerBuilder.newCacheManagerBuilder() |
99 |
|
.with(CacheManagerBuilder.persistence(diskStorePath)) |
100 |
|
.build(); |
101 |
|
} |
102 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
103 |
|
@Override... |
104 |
|
public void start(boolean isRestart) { |
105 |
|
cacheManager.init(); |
106 |
|
} |
107 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
108 |
|
@Override... |
109 |
|
public void stop(boolean isRestart) { |
110 |
|
cacheManager.close(); |
111 |
|
} |
112 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
113 |
|
@Override... |
114 |
|
public List<String> getCacheNames() { |
115 |
|
return new ArrayList<>(cacheManager.getRuntimeConfiguration().getCacheConfigurations().keySet()); |
116 |
|
} |
117 |
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
118 |
7 |
@Override... |
119 |
|
public Cache getCache(String name) { |
120 |
7 |
Cache cache = null; |
121 |
7 |
if (cacheManager.getStatus() == Status.AVAILABLE) { |
122 |
7 |
final CacheConfiguration cacheConfiguration = getCacheConfiguration(name); |
123 |
7 |
final org.ehcache.Cache ehcache = cacheManager.getCache(name, cacheConfiguration.getKeyType(), cacheConfiguration.getValueType()); |
124 |
|
|
125 |
7 |
if (ehcache == null) { |
126 |
|
|
127 |
3 |
createCache(name); |
128 |
3 |
cache = getCache(name); |
129 |
|
} else { |
130 |
4 |
cache = wrap(name, ehcache); |
131 |
|
} |
132 |
|
} else { |
133 |
0 |
log.warn("Cache manager status is {{}} while retrieving cache named {{}}. Returning null.", cacheManager.getStatus(), name); |
134 |
|
} |
135 |
7 |
return cache; |
136 |
|
} |
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
138 |
3 |
private void createCache(String name) {... |
139 |
3 |
synchronized (this.getClass()) { |
140 |
3 |
cacheManager.createCache(name, getCacheConfiguration(name)); |
141 |
|
} |
142 |
|
} |
143 |
|
|
|
|
| 76.9% |
Uncovered Elements: 3 (13) |
Complexity: 4 |
Complexity Density: 0.44 |
|
144 |
10 |
private CacheConfiguration getCacheConfiguration(String name) {... |
145 |
10 |
CacheConfiguration cacheConfiguration; |
146 |
10 |
if (cachesConfiguration.containsKey(name)) { |
147 |
7 |
cacheConfiguration = cachesConfiguration.get(name); |
148 |
|
} else { |
149 |
3 |
try { |
150 |
3 |
cacheConfiguration = getCaches().containsKey(name) ? getCaches().get(name).build() : getCaches().get(DEFAULT_CACHE_NAME).build(); |
151 |
|
} catch (IllegalArgumentException e) { |
152 |
0 |
cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(Serializable.class, Serializable.class, ResourcePoolsBuilder.heap(10000).build()).build(); |
153 |
0 |
log.error("Cache {{}} is misconfigured: {{}}. Returning fallback config", name, e.getMessage()); |
154 |
|
} |
155 |
3 |
cachesConfiguration.put(name, cacheConfiguration); |
156 |
|
} |
157 |
10 |
return cacheConfiguration; |
158 |
|
} |
159 |
|
|
160 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
161 |
4 |
private EhCache3Wrapper wrap(String name, org.ehcache.Cache ehcache) {... |
162 |
4 |
return new EhCache3Wrapper(name, cacheModule, getBlockingTimeout(), ehcache); |
163 |
|
} |
164 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
165 |
7 |
private String getCacheManagerIdentifier() {... |
166 |
7 |
if (magnoliaConfigurationProperties.hasProperty(MAGNOLIA_PROPERTY_CACHE_MANAGER_ID)) { |
167 |
2 |
return magnoliaConfigurationProperties.getProperty(MAGNOLIA_PROPERTY_CACHE_MANAGER_ID); |
168 |
|
} else { |
169 |
5 |
return magnoliaInitPaths.getContextPath(); |
170 |
|
} |
171 |
|
} |
172 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
173 |
|
public Map<String, Builder<? extends CacheConfiguration>> getCaches() {... |
174 |
|
return caches; |
175 |
|
} |
176 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
177 |
|
public void setCaches(Map<String, Builder<? extends CacheConfiguration>> caches) {... |
178 |
|
this.caches = caches; |
179 |
|
} |
180 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
181 |
|
public String getDiskStorePath() {... |
182 |
|
return diskStorePath; |
183 |
|
} |
184 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
185 |
|
public void setDiskStorePath(String diskStorePath) {... |
186 |
|
this.diskStorePath = diskStorePath; |
187 |
|
} |
188 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
189 |
|
public int getBlockingTimeout() {... |
190 |
|
return blockingTimeout; |
191 |
|
} |
192 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
193 |
|
public void setBlockingTimeout(int blockingTimeout) {... |
194 |
|
this.blockingTimeout = blockingTimeout; |
195 |
|
} |
196 |
|
|
197 |
|
} |