Skip to content

Commit 3e5e903

Browse files
Configuration Properties
1 parent 4cca07c commit 3e5e903

File tree

7 files changed

+65
-66
lines changed

7 files changed

+65
-66
lines changed

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

Lines changed: 1 addition & 9 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;
@@ -88,13 +87,6 @@ private synchronized <K, V> void configCachePropertyValues(final String name, fi
8887

8988
}
9089

91-
/**
92-
* @return the parameter configuration
93-
*/
94-
private Configuration getParams() {
95-
return Config.getInstance();
96-
}
97-
9890
/**
9991
* Get an integer cache property.
10092
*
@@ -104,7 +96,7 @@ private Configuration getParams() {
10496
*/
10597
private Integer getIntegerProperty(final String name, final String property) {
10698
// Cache property then default
107-
return getParams().getInteger(getKey(name, property), null);
99+
return Config.getInstance().getInteger(getKey(name, property), null);
108100
}
109101

110102
/**

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ public synchronized void closeCacheManager() {
2929
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass, final Class<V> valueClass) {
3030
Cache<K, V> cache = Caching.getCache(name, keyClass, valueClass);
3131
if (cache == null) {
32+
// Get cache duration
3233
Duration duration = CachingProperties.getCacheDuration(name);
33-
cache = getOrCreateCache(name, keyClass, valueClass, duration);
34+
cache = createCache(name, keyClass, valueClass, duration);
3435
}
3536
return cache;
3637
}
@@ -40,11 +41,9 @@ public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final
4041
final Class<V> valueClass, final Duration duration) {
4142
Cache<K, V> cache = Caching.getCache(name, keyClass, valueClass);
4243
if (cache == null) {
43-
final CacheManager mgr = Caching.getCachingProvider().getCacheManager();
44-
MutableConfiguration<K, V> config = new MutableConfiguration<>();
45-
config.setTypes(keyClass, valueClass);
46-
config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(duration));
47-
cache = mgr.createCache(name, config);
44+
// Check for duration override
45+
Duration durationOverride = CachingProperties.getCacheDuration(name, duration);
46+
cache = createCache(name, keyClass, valueClass, durationOverride);
4847
}
4948
return cache;
5049
}
@@ -60,4 +59,24 @@ public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final
6059
return cache;
6160
}
6261

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+
6382
}

taskmaster-core/src/main/java/com/github/bordertech/taskmaster/impl/TaskFutureWrapper.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ public class TaskFutureWrapper<T extends Serializable> implements TaskFuture<T>
2828
private final String id = UUID.randomUUID().toString();
2929

3030
static {
31-
// Get future task cache name
32-
String name = TaskMasterProperties.getFutureTaskCacheName();
3331

34-
// Get cache duration
32+
String cacheName = TaskMasterProperties.FUTURE_TASK_CACHE_NAME;
3533
Duration duration = TaskMasterProperties.getFutureTaskCacheDuration();
3634

3735
// Setup cache config
@@ -42,7 +40,7 @@ public class TaskFutureWrapper<T extends Serializable> implements TaskFuture<T>
4240
config.setStoreByValue(false);
4341

4442
// Create Cache
45-
CACHE = CachingHelper.getOrCreateCache(name, String.class, Future.class, config);
43+
CACHE = CachingHelper.getOrCreateCache(cacheName, String.class, Future.class, config);
4644
}
4745

4846
/**

taskmaster-core/src/main/java/com/github/bordertech/taskmaster/impl/TaskMasterProperties.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
*/
1212
public final class TaskMasterProperties {
1313

14+
/**
15+
* Future task cache name.
16+
*/
17+
public static final String FUTURE_TASK_CACHE_NAME = "bordertech-tm-future-task";
18+
19+
private static final Duration FUTURE_TASK_CACHE_DURATION = new Duration(TimeUnit.SECONDS, Long.valueOf("300"));
1420
private static final String TP_PARAM_PREFIX = "bordertech.taskmaster.pool.";
1521
private static final int DEFAULT_MAX_THREADS = 20;
1622
private static final int DEFAULT_QUEUE_LENGTH = 0;
@@ -22,19 +28,12 @@ private TaskMasterProperties() {
2228
//No-impl
2329
}
2430

25-
/**
26-
* @return the future task cache name
27-
*/
28-
public static String getFutureTaskCacheName() {
29-
return "bordertech-tm-future-task";
30-
}
31-
3231
/**
3332
* @return the future task cache duration
3433
*/
3534
public static Duration getFutureTaskCacheDuration() {
36-
Duration defaultDuration = new Duration(TimeUnit.SECONDS, Long.valueOf("300"));
37-
return CachingProperties.getCacheDuration(getFutureTaskCacheName(), defaultDuration);
35+
// Check for override
36+
return CachingProperties.getCacheDuration(FUTURE_TASK_CACHE_NAME, FUTURE_TASK_CACHE_DURATION);
3837
}
3938

4039
/**

taskmaster-service-helper/src/main/java/com/github/bordertech/taskmaster/service/impl/ServiceHelperProperties.java

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,39 @@
1212
public final class ServiceHelperProperties {
1313

1414
/**
15-
* Private constructor for static class.
15+
* Default result holder cache name.
1616
*/
17-
private ServiceHelperProperties() {
18-
//No-impl
19-
}
17+
public static final String RESULT_HOLDER_CACHE_NAME = "taskmaster-resultholder";
2018

2119
/**
22-
* @return the default result holder cache name
20+
* In progress cache name.
2321
*/
24-
public static String getDefaultResultHolderCacheName() {
25-
return "taskmaster-resultholder";
26-
}
22+
public static final String INPROGRESS_CACHE_NAME = "taskmaster-inprogress";
2723

28-
/**
29-
* @return the default result holder cache duration
30-
*/
31-
public static Duration getDefaultResultHolderCacheDuration() {
32-
Duration defaultDuration = new Duration(TimeUnit.SECONDS, Long.valueOf("1800"));
33-
return CachingProperties.getCacheDuration(getDefaultResultHolderCacheName(), defaultDuration);
34-
}
24+
private static final Duration RESULT_HOLDER_CACHE_DURATION = new Duration(TimeUnit.SECONDS, Long.valueOf("1800"));
25+
private static final Duration INPROGRESS_CACHE_DURATION = new Duration(TimeUnit.SECONDS, Long.valueOf("300"));
3526

3627
/**
37-
* @param name the result holder cache name
38-
* @return the result holder cache duration
28+
* Private constructor for static class.
3929
*/
40-
public static Duration getResultHolderCacheDuration(final String name) {
41-
Duration defaultDuration = getDefaultResultHolderCacheDuration();
42-
return CachingProperties.getCacheDuration(name, defaultDuration);
30+
private ServiceHelperProperties() {
31+
//No-impl
4332
}
4433

4534
/**
46-
* @return the in progress cache name
35+
* @return the default result holder cache duration
4736
*/
48-
public static String getInProgressCacheName() {
49-
return "taskmaster-inprogress";
37+
public static Duration getResultHolderCacheDuration() {
38+
// Check for overrides
39+
return CachingProperties.getCacheDuration(ServiceHelperProperties.RESULT_HOLDER_CACHE_NAME, RESULT_HOLDER_CACHE_DURATION);
5040
}
5141

5242
/**
5343
* @return the in progress cache duration
5444
*/
5545
public static Duration getInProgressCacheDuration() {
56-
Duration defaultDuration = new Duration(TimeUnit.SECONDS, Long.valueOf("300"));
57-
return CachingProperties.getCacheDuration(getInProgressCacheName(), defaultDuration);
46+
// Check for overrides
47+
return CachingProperties.getCacheDuration(ServiceHelperProperties.INPROGRESS_CACHE_NAME, INPROGRESS_CACHE_DURATION);
5848
}
5949

6050
/**

taskmaster-service-helper/src/main/java/com/github/bordertech/taskmaster/service/impl/ServiceHelperProviderDefault.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ public class ServiceHelperProviderDefault implements ServiceHelperProvider {
2828

2929
static {
3030
if (IN_PROGRESS_ENABLED) {
31-
String name = ServiceHelperProperties.getInProgressCacheName();
32-
// Setup the cache for tracking in progress ASync Cached service calls
33-
Duration duration = ServiceHelperProperties.getInProgressCacheDuration();
34-
IN_PROGRESS_CACHE = CachingHelper.getOrCreateCache(name, String.class, Boolean.class, duration);
31+
String cacheName = ServiceHelperProperties.INPROGRESS_CACHE_NAME;
32+
Duration defaultDuration = ServiceHelperProperties.getInProgressCacheDuration();
33+
IN_PROGRESS_CACHE = CachingHelper.getOrCreateCache(cacheName, String.class, Boolean.class, defaultDuration);
3534
} else {
3635
IN_PROGRESS_CACHE = null;
3736
}
@@ -220,11 +219,10 @@ protected <S extends Serializable, T extends Serializable> ResultHolder<S, T> ch
220219
}
221220

222221
/**
223-
* Flag if tracking in progress ASync cached service calls which helps avoid multiple calls for the same service
224-
* call.
222+
* Flag if tracking in progress ASync cached service calls which helps avoid multiple calls for the same service call.
225223
* <p>
226-
* It only makes sense to track cached ASync calls as cached results are expected to be called multiple times and
227-
* Sync calls require the result immediately anyway.
224+
* It only makes sense to track cached ASync calls as cached results are expected to be called multiple times and Sync calls require the result
225+
* immediately anyway.
228226
* </p>
229227
*
230228
* @return true if tracking in progress ASync cached service calls

taskmaster-service-helper/src/main/java/com/github/bordertech/taskmaster/service/util/ServiceCacheUtil.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.bordertech.taskmaster.service.util;
22

33
import com.github.bordertech.taskmaster.cache.CachingHelper;
4+
import com.github.bordertech.taskmaster.cache.impl.CachingProperties;
45
import com.github.bordertech.taskmaster.service.ResultHolder;
56
import com.github.bordertech.taskmaster.service.impl.ServiceHelperProperties;
67
import javax.cache.Cache;
@@ -11,6 +12,11 @@
1112
* <p>
1213
* This helper provides a default cache or projects can create caches with assigned names.
1314
* </p>
15+
* <p>
16+
* The duration of the caches can be overridden by setting the runtime properties for {@link CachingProperties}.
17+
* </p>
18+
*
19+
* @see CachingProperties
1420
*/
1521
public final class ServiceCacheUtil {
1622

@@ -27,9 +33,7 @@ private ServiceCacheUtil() {
2733
* @return the default result holder cache instance
2834
*/
2935
public static Cache<String, ResultHolder> getDefaultResultHolderCache() {
30-
String name = ServiceHelperProperties.getDefaultResultHolderCacheName();
31-
Duration duration = ServiceHelperProperties.getDefaultResultHolderCacheDuration();
32-
return getResultHolderCache(name, duration);
36+
return getResultHolderCache(ServiceHelperProperties.RESULT_HOLDER_CACHE_NAME);
3337
}
3438

3539
/**
@@ -39,8 +43,7 @@ public static Cache<String, ResultHolder> getDefaultResultHolderCache() {
3943
* @return the cache instance
4044
*/
4145
public static Cache<String, ResultHolder> getResultHolderCache(final String name) {
42-
Duration duration = ServiceHelperProperties.getResultHolderCacheDuration(name);
43-
return getResultHolderCache(name, duration);
46+
return getResultHolderCache(name, ServiceHelperProperties.getResultHolderCacheDuration());
4447
}
4548

4649
/**

0 commit comments

Comments
 (0)