Skip to content

Commit 86b3e04

Browse files
Merge pull request #41 from BorderTech/fix-service-helper
Fix service helper API
2 parents 6d8a908 + 3e5e903 commit 86b3e04

35 files changed

+1387
-713
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
<dependency>
9999
<groupId>org.ehcache</groupId>
100100
<artifactId>ehcache</artifactId>
101-
<version>3.7.1</version>
101+
<version>3.8.0</version>
102102
</dependency>
103103

104104
<!-- Common Lang3 -->

taskmaster-cache-ehcache/src/main/java/com/github/bordertech/taskmaster/ehcache/CachingHelperProviderEhCache.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.util.Map;
99
import javax.cache.Cache;
1010
import javax.inject.Singleton;
11-
import org.apache.commons.configuration.Configuration;
1211
import org.apache.commons.lang3.tuple.ImmutableTriple;
1312
import org.apache.commons.lang3.tuple.Triple;
1413
import org.apache.commons.logging.Log;
@@ -40,9 +39,8 @@ public CachingHelperProviderEhCache() {
4039
}
4140

4241
@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);
42+
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass) {
43+
Cache<K, V> cache = super.getOrCreateCache(name, keyClass, valueClass);
4644
if (!caches.containsKey(name)) {
4745
configCachePropertyValues(name, cache);
4846
caches.put(name, new ImmutableTriple(name, keyClass, valueClass));
@@ -89,13 +87,6 @@ private synchronized <K, V> void configCachePropertyValues(final String name, fi
8987

9088
}
9189

92-
/**
93-
* @return the parameter configuration
94-
*/
95-
private Configuration getParams() {
96-
return Config.getInstance();
97-
}
98-
9990
/**
10091
* Get an integer cache property.
10192
*
@@ -105,7 +96,7 @@ private Configuration getParams() {
10596
*/
10697
private Integer getIntegerProperty(final String name, final String property) {
10798
// Cache property then default
108-
return getParams().getInteger(getKey(name, property), null);
99+
return Config.getInstance().getInteger(getKey(name, property), null);
109100
}
110101

111102
/**

taskmaster-cache-helper/src/main/java/com/github/bordertech/taskmaster/cache/CachingHelper.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.github.bordertech.didums.Didums;
44
import com.github.bordertech.taskmaster.cache.impl.CachingHelperProviderDefault;
5+
import javax.cache.Cache;
6+
import javax.cache.configuration.Configuration;
7+
import javax.cache.expiry.Duration;
58

69
/**
710
* Caching helper based on JSR 107.
@@ -28,4 +31,55 @@ public static CachingHelperProvider getProvider() {
2831
return PROVIDER;
2932
}
3033

34+
/**
35+
* Close and release the cache resources.
36+
*/
37+
public static void closeCacheManager() {
38+
PROVIDER.closeCacheManager();
39+
}
40+
41+
/**
42+
* Create a cache with the default configuration.
43+
*
44+
* @param name the cache name
45+
* @param keyClass the key class type
46+
* @param valueClass the value class type
47+
* @param <K> the cache entry key type
48+
* @param <V> the cache entry value value
49+
* @return the cache instance
50+
*/
51+
public static <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass) {
52+
return PROVIDER.getOrCreateCache(name, keyClass, valueClass);
53+
}
54+
55+
/**
56+
* Create a cache with the specified duration.
57+
*
58+
* @param name the cache name
59+
* @param keyClass the key class type
60+
* @param valueClass the value class type
61+
* @param duration the cache entry duration
62+
* @param <K> the cache entry key type
63+
* @param <V> the cache entry value value
64+
* @return the cache instance
65+
*/
66+
public static <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass, final Duration duration) {
67+
return PROVIDER.getOrCreateCache(name, keyClass, valueClass, duration);
68+
}
69+
70+
/**
71+
* Create a cache with the specified configuration.
72+
*
73+
* @param <K> the cache entry key type
74+
* @param <V> the cache entry value value
75+
* @param name the cache name
76+
* @param keyClass the key class type
77+
* @param valueClass the value class type
78+
* @param config the cache configuration
79+
* @return the cache instance
80+
*/
81+
public static <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass, final Configuration<K, V> config) {
82+
return PROVIDER.getOrCreateCache(name, keyClass, valueClass, config);
83+
}
84+
3185
}

taskmaster-cache-helper/src/main/java/com/github/bordertech/taskmaster/cache/CachingHelperProvider.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ public interface CachingHelperProvider {
1919
*/
2020
void closeCacheManager();
2121

22+
/**
23+
* Create a cache with the default configuration.
24+
*
25+
* @param name the cache name
26+
* @param keyClass the key class type
27+
* @param valueClass the value class type
28+
* @param <K> the cache entry key type
29+
* @param <V> the cache entry value value
30+
* @return the cache instance
31+
*/
32+
<K, V> Cache<K, V> getOrCreateCache(String name, Class<K> keyClass, Class<V> valueClass);
33+
2234
/**
2335
* Create a cache with the specified duration.
2436
*
@@ -30,7 +42,7 @@ public interface CachingHelperProvider {
3042
* @param <V> the cache entry value value
3143
* @return the cache instance
3244
*/
33-
<K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass, final Duration duration);
45+
<K, V> Cache<K, V> getOrCreateCache(String name, Class<K> keyClass, Class<V> valueClass, Duration duration);
3446

3547
/**
3648
* Create a cache with the specified configuration.
@@ -43,6 +55,6 @@ public interface CachingHelperProvider {
4355
* @param config the cache configuration
4456
* @return the cache instance
4557
*/
46-
<K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass, final Configuration<K, V> config);
58+
<K, V> Cache<K, V> getOrCreateCache(String name, Class<K> keyClass, Class<V> valueClass, Configuration<K, V> config);
4759

4860
}

taskmaster-cache-helper/src/main/java/com/github/bordertech/taskmaster/cache/impl/CachingHelperProviderDefault.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,25 @@ public synchronized void closeCacheManager() {
2525
}
2626
}
2727

28+
@Override
29+
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass) {
30+
Cache<K, V> cache = Caching.getCache(name, keyClass, valueClass);
31+
if (cache == null) {
32+
// Get cache duration
33+
Duration duration = CachingProperties.getCacheDuration(name);
34+
cache = createCache(name, keyClass, valueClass, duration);
35+
}
36+
return cache;
37+
}
38+
2839
@Override
2940
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass,
3041
final Class<V> valueClass, final Duration duration) {
3142
Cache<K, V> cache = Caching.getCache(name, keyClass, valueClass);
3243
if (cache == null) {
33-
final CacheManager mgr = Caching.getCachingProvider().getCacheManager();
34-
MutableConfiguration<K, V> config = new MutableConfiguration<>();
35-
config.setTypes(keyClass, valueClass);
36-
config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(duration));
37-
cache = mgr.createCache(name, config);
44+
// Check for duration override
45+
Duration durationOverride = CachingProperties.getCacheDuration(name, duration);
46+
cache = createCache(name, keyClass, valueClass, durationOverride);
3847
}
3948
return cache;
4049
}
@@ -50,4 +59,24 @@ public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final
5059
return cache;
5160
}
5261

62+
/**
63+
* Create a cache.
64+
*
65+
* @param <K> the key type
66+
* @param <V> the value type
67+
* @param name the cache name
68+
* @param keyClass the key class type
69+
* @param valueClass the value class type
70+
* @param duration the cache duration amount
71+
* @return the cache
72+
*/
73+
protected synchronized <K, V> Cache<K, V> createCache(final String name, final Class<K> keyClass,
74+
final Class<V> valueClass, final Duration duration) {
75+
final CacheManager mgr = Caching.getCachingProvider().getCacheManager();
76+
MutableConfiguration<K, V> config = new MutableConfiguration<>();
77+
config.setTypes(keyClass, valueClass);
78+
config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(duration));
79+
return mgr.createCache(name, config);
80+
}
81+
5382
}

taskmaster-cache-helper/src/main/java/com/github/bordertech/taskmaster/cache/impl/CachingHelperProviderXmlConfig.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.github.bordertech.taskmaster.cache.impl;
22

3-
import com.github.bordertech.config.Config;
43
import com.github.bordertech.taskmaster.cache.CachingHelperProvider;
54
import java.net.URI;
65
import java.net.URISyntaxException;
@@ -27,7 +26,7 @@ public class CachingHelperProviderXmlConfig implements CachingHelperProvider {
2726

2827
static {
2928
// Load cache configfile location (default tm-cache.xml)
30-
String config = Config.getInstance().getString("bordertech.taskmaster.cache.config", "/tm-cache.xml");
29+
String config = CachingProperties.getConfigXmlLocation();
3130
LOGGER.info("Loading cache config [" + config + "].");
3231
URI uri;
3332
try {
@@ -51,33 +50,24 @@ public synchronized void closeCacheManager() {
5150
}
5251
}
5352

53+
@Override
54+
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass) {
55+
// Return the pre configured cache
56+
return MANAGER.getCache(name, keyClass, valueClass);
57+
}
58+
5459
@Override
5560
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass,
5661
final Class<V> valueClass, final Duration duration) {
5762
// Ignore duration
58-
return handleGetCache(name, keyClass, valueClass);
63+
return getOrCreateCache(name, keyClass, valueClass);
5964
}
6065

6166
@Override
6267
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass,
6368
final Class<V> valueClass, final Configuration<K, V> config) {
6469
// Ignore config
65-
return handleGetCache(name, keyClass, valueClass);
66-
}
67-
68-
/**
69-
* Get the pre-configured cache.
70-
*
71-
* @param name the cache name
72-
* @param keyClass the cache key class
73-
* @param valueClass the cache entry class
74-
* @return the cache instance
75-
* @param <K> the cache key type
76-
* @param <V> the cache entry type
77-
*/
78-
protected synchronized <K, V> Cache<K, V> handleGetCache(final String name, final Class<K> keyClass,
79-
final Class<V> valueClass) {
80-
return MANAGER.getCache(name, keyClass, valueClass);
70+
return getOrCreateCache(name, keyClass, valueClass);
8171
}
8272

8373
/**

0 commit comments

Comments
 (0)