|
| 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 | +} |
0 commit comments