Skip to content

Commit 540ea03

Browse files
Merge pull request #16 from BorderTech/refactor-submodules
CacheHelper that can configure ehcache via XML and runtime properties.
2 parents ecc678a + 01b0b23 commit 540ea03

File tree

4 files changed

+215
-0
lines changed

4 files changed

+215
-0
lines changed

pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,26 @@
8787
<version>2.5</version>
8888
</dependency>
8989

90+
<!-- ehcache -->
91+
<dependency>
92+
<groupId>org.ehcache</groupId>
93+
<artifactId>ehcache</artifactId>
94+
<version>3.5.2</version>
95+
</dependency>
96+
97+
<!-- Common Lang3 -->
98+
<dependency>
99+
<groupId>org.apache.commons</groupId>
100+
<artifactId>commons-lang3</artifactId>
101+
<version>3.8</version>
102+
</dependency>
103+
90104
</dependencies>
91105
</dependencyManagement>
92106

93107
<modules>
94108
<module>taskmaster-cache-helper</module>
109+
<module>taskmaster-cache-ehcache</module>
95110
<module>taskmaster-core</module>
96111
<module>taskmaster-service-helper</module>
97112
</modules>

taskmaster-cache-ehcache/pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.github.bordertech.taskmaster</groupId>
6+
<name>taskmaster-cache-ehcache</name>
7+
<artifactId>taskmaster-cache-ehcache</artifactId>
8+
<version>1.0.2-SNAPSHOT</version>
9+
10+
<parent>
11+
<groupId>com.github.bordertech.taskmaster</groupId>
12+
<artifactId>taskmaster-parent</artifactId>
13+
<version>1.0.2-SNAPSHOT</version>
14+
<relativePath>../pom.xml</relativePath>
15+
</parent>
16+
17+
<packaging>jar</packaging>
18+
19+
<properties>
20+
<bt.qa.skip>false</bt.qa.skip>
21+
</properties>
22+
23+
<description>
24+
Task Master provides a ehcache helper.
25+
</description>
26+
27+
<dependencies>
28+
29+
<!-- TaskMaster Cache Helper API -->
30+
<dependency>
31+
<groupId>com.github.bordertech.taskmaster</groupId>
32+
<artifactId>taskmaster-cache-helper</artifactId>
33+
<version>${project.version}</version>
34+
</dependency>
35+
36+
<!-- Commons Lang3 -->
37+
<dependency>
38+
<groupId>org.apache.commons</groupId>
39+
<artifactId>commons-lang3</artifactId>
40+
</dependency>
41+
42+
<!-- Injection interface. JSR 330 -->
43+
<dependency>
44+
<groupId>javax.inject</groupId>
45+
<artifactId>javax.inject</artifactId>
46+
<scope>provided</scope>
47+
</dependency>
48+
49+
<!-- ehcache -->
50+
<dependency>
51+
<groupId>org.ehcache</groupId>
52+
<artifactId>ehcache</artifactId>
53+
<scope>provided</scope>
54+
</dependency>
55+
56+
</dependencies>
57+
</project>
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package com.github.bordertech.taskmaster.ehcache;
2+
3+
import com.github.bordertech.config.Config;
4+
import com.github.bordertech.taskmaster.cache.CacheHelperXmlConfigImpl;
5+
import java.beans.PropertyChangeEvent;
6+
import java.beans.PropertyChangeListener;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
import javax.cache.Cache;
10+
import javax.inject.Singleton;
11+
import org.apache.commons.configuration.Configuration;
12+
import org.apache.commons.lang3.tuple.ImmutableTriple;
13+
import org.apache.commons.lang3.tuple.Triple;
14+
import org.apache.commons.logging.Log;
15+
import org.apache.commons.logging.LogFactory;
16+
import org.ehcache.config.CacheRuntimeConfiguration;
17+
import org.ehcache.config.ResourcePools;
18+
import org.ehcache.config.builders.ResourcePoolsBuilder;
19+
import org.ehcache.config.units.EntryUnit;
20+
import org.ehcache.config.units.MemoryUnit;
21+
import org.ehcache.jsr107.Eh107Configuration;
22+
23+
/**
24+
* Cache Helper that configures ehcache with properties and wraps the cache with JSR107 interface.
25+
*/
26+
@Singleton
27+
public class CacheHelperEhCachePropertiesConfigImpl extends CacheHelperXmlConfigImpl {
28+
29+
private static final Log LOGGER = LogFactory.getLog(CacheHelperEhCachePropertiesConfigImpl.class);
30+
private static final String CACHE_PREFIX = "Cache [";
31+
private final Map<String, Triple<String, Class, Class>> caches = new HashMap<>();
32+
33+
/**
34+
* Construct Helper.
35+
*/
36+
public CacheHelperEhCachePropertiesConfigImpl() {
37+
// Add property change listener
38+
PropertyChangeListener listener = new RefreshCachePropertyChangeListener();
39+
Config.addPropertyChangeListener(listener);
40+
}
41+
42+
@Override
43+
protected synchronized <K, V> Cache<K, V> handleGetCache(final String name, final Class<K> keyClass,
44+
final Class<V> valueClass) {
45+
Cache<K, V> cache = super.handleGetCache(name, keyClass, valueClass);
46+
if (!caches.containsKey(name)) {
47+
configCachePropertyValues(name, cache);
48+
caches.put(name, new ImmutableTriple(name, keyClass, valueClass));
49+
}
50+
return cache;
51+
}
52+
53+
/**
54+
* Update cache heap properties. DISK resources cannot be updated on the fly.
55+
*
56+
* @param <K> the key type
57+
* @param <V> the value type
58+
* @param name the cache name
59+
* @param cache the cache
60+
*/
61+
private synchronized <K, V> void configCachePropertyValues(final String name, final Cache<K, V> cache) {
62+
63+
LOGGER.info("Config cache [" + name + "] properties.");
64+
65+
Eh107Configuration<K, V> eh107Configuration = cache.getConfiguration(Eh107Configuration.class);
66+
CacheRuntimeConfiguration<K, V> config = eh107Configuration.unwrap(CacheRuntimeConfiguration.class);
67+
68+
ResourcePoolsBuilder builder = ResourcePoolsBuilder.newResourcePoolsBuilder();
69+
boolean changed = false;
70+
// Heap size (Megabytes)
71+
Integer value = getIntegerProperty(name, "heap.size");
72+
if (value != null) {
73+
LOGGER.info(CACHE_PREFIX + name + "] heap size [" + value + "MB].");
74+
builder = builder.heap(value, MemoryUnit.MB);
75+
changed = true;
76+
}
77+
// Heap entries (ie limit of entries)
78+
value = getIntegerProperty(name, "heap.entries");
79+
if (value != null) {
80+
LOGGER.info(CACHE_PREFIX + name + "] heap entries [" + value + "].");
81+
builder = builder.heap(value, EntryUnit.ENTRIES);
82+
changed = true;
83+
}
84+
if (changed) {
85+
LOGGER.info(CACHE_PREFIX + name + "] config updated.");
86+
ResourcePools pools = builder.build();
87+
config.updateResourcePools(pools);
88+
}
89+
90+
}
91+
92+
/**
93+
* @return the parameter configuration
94+
*/
95+
private Configuration getParams() {
96+
return Config.getInstance();
97+
}
98+
99+
/**
100+
* Get an integer cache property.
101+
*
102+
* @param name the cache name
103+
* @param property the cache property
104+
* @return the integer property value or null
105+
*/
106+
private Integer getIntegerProperty(final String name, final String property) {
107+
// Cache property then default
108+
return getParams().getInteger(getKey(name, property), null);
109+
}
110+
111+
/**
112+
* Derive parameter key for the cache and property.
113+
*
114+
* @param name the cache name
115+
* @param property the cache property
116+
* @return the property key
117+
*/
118+
private String getKey(final String name, final String property) {
119+
return "av.cache." + name + "." + property;
120+
}
121+
122+
/**
123+
* Property change listener that handles updating cache properties.
124+
*/
125+
private class RefreshCachePropertyChangeListener implements PropertyChangeListener {
126+
127+
@Override
128+
public void propertyChange(final PropertyChangeEvent evt) {
129+
LOGGER.info("Refresh cache properties.");
130+
for (Triple<String, Class, Class> value : caches.values()) {
131+
String name = value.getLeft();
132+
Cache cache = getManager().getCache(name, value.getMiddle(), value.getRight());
133+
if (cache != null) {
134+
configCachePropertyValues(name, cache);
135+
}
136+
}
137+
}
138+
}
139+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* CacheHelper that can configure ehcache.
3+
*/
4+
package com.github.bordertech.taskmaster.ehcache;

0 commit comments

Comments
 (0)