diff --git a/src/main/java/org/greencheek/caching/herdcache/memcached/BaseMemcachedCache.java b/src/main/java/org/greencheek/caching/herdcache/memcached/BaseMemcachedCache.java index 1556b8a..507d70f 100644 --- a/src/main/java/org/greencheek/caching/herdcache/memcached/BaseMemcachedCache.java +++ b/src/main/java/org/greencheek/caching/herdcache/memcached/BaseMemcachedCache.java @@ -37,7 +37,7 @@ /** * */ - class BaseMemcachedCache implements RequiresShutdown,ClearableCache, + abstract class BaseMemcachedCache implements RequiresShutdown,ClearableCache, SerializableOnlyCacheWithExpiry, RevalidateInBackgroundCapableCache { @@ -91,12 +91,22 @@ public static ReferencedClientFactory createReferenceClientFactory(ElastiCacheCa private final CacheKeyCreator cacheKeyCreator; + public BaseMemcachedCache(MemcachedCacheConfig config) { + this(null,config); + } + public BaseMemcachedCache( MemcachedClientFactory clientFactory, MemcachedCacheConfig config) { this.config = config; + if(clientFactory == null) { + this.clientFactory = buildClientFactory(config); + } else { + this.clientFactory = clientFactory; + } + + cacheKeyCreator = CacheKeyCreatorFactory.DEFAULT_INSTANCE.create(config); - this.clientFactory = clientFactory; int maxCapacity = config.getMaxCapacity(); @@ -142,6 +152,9 @@ public BaseMemcachedCache( staleCacheWriter = new NoWaitForCacheWrite(metricRecorder); } + public abstract MemcachedClientFactory buildClientFactory(Object cfg); + + private ConcurrentMap createInternalCache(boolean createCache, int initialCapacity, int maxCapacity) { diff --git a/src/main/java/org/greencheek/caching/herdcache/memcached/ElastiCacheMemcachedCache.java b/src/main/java/org/greencheek/caching/herdcache/memcached/ElastiCacheMemcachedCache.java index 09ae28e..2541b2d 100644 --- a/src/main/java/org/greencheek/caching/herdcache/memcached/ElastiCacheMemcachedCache.java +++ b/src/main/java/org/greencheek/caching/herdcache/memcached/ElastiCacheMemcachedCache.java @@ -9,6 +9,7 @@ import org.greencheek.caching.herdcache.memcached.elasticacheconfig.client.ElastiCacheServerConnectionDetails; import org.greencheek.caching.herdcache.memcached.elasticacheconfig.client.LocalhostElastiCacheServerConnectionDetails; import org.greencheek.caching.herdcache.memcached.factory.ElastiCacheClientFactory; +import org.greencheek.caching.herdcache.memcached.factory.MemcachedClientFactory; import org.greencheek.caching.herdcache.memcached.factory.ReferencedClientFactory; import java.io.Serializable; @@ -45,6 +46,29 @@ private ElastiCacheMemcachedCache(MemcachedCacheConfig mConfig,ElastiCacheCacheC } + @Override + public MemcachedClientFactory buildClientFactory(Object cfg) { + ElastiCacheCacheConfig config = (ElastiCacheCacheConfig) cfg; + MemcachedCacheConfig mConfig = config.getMemcachedCacheConfig(); + return new ElastiCacheClientFactory( + createReferenceClientFactory(config), + ElastiCacheConfigHostsParser.parseElastiCacheConfigHosts(config.getElastiCacheConfigHosts()), + config.getConfigPollingTime(), + config.getInitialConfigPollingDelay(), + config.getIdleReadTimeout(), + config.getReconnectDelay(), + config.getDelayBeforeClientClose(), + mConfig.getHostResolver(), + mConfig.getDnsConnectionTimeout(), + config.isUpdateConfigVersionOnDnsTimeout(), + config.getNumberOfConsecutiveInvalidConfigurationsBeforeReconnect(), + config.getConnectionTimeoutInMillis(), + config.getClusterUpdatedObservers(), + config.getConfigUrlUpdater(), + config.isUpdateConfigOnlyOnVersionChange()); + + } + } diff --git a/src/main/java/org/greencheek/caching/herdcache/memcached/FolsomMemcachedCache.java b/src/main/java/org/greencheek/caching/herdcache/memcached/FolsomMemcachedCache.java index 63e4591..c70228a 100644 --- a/src/main/java/org/greencheek/caching/herdcache/memcached/FolsomMemcachedCache.java +++ b/src/main/java/org/greencheek/caching/herdcache/memcached/FolsomMemcachedCache.java @@ -1,7 +1,9 @@ package org.greencheek.caching.herdcache.memcached; import org.greencheek.caching.herdcache.memcached.config.ElastiCacheCacheConfig; +import org.greencheek.caching.herdcache.memcached.config.MemcachedCacheConfig; import org.greencheek.caching.herdcache.memcached.factory.FolsomReferencedClientFactory; +import org.greencheek.caching.herdcache.memcached.factory.MemcachedClientFactory; import org.greencheek.caching.herdcache.memcached.factory.SpyMemcachedClientFactory; import java.io.Serializable; @@ -15,4 +17,13 @@ public FolsomMemcachedCache(ElastiCacheCacheConfig config) { config.getMemcachedCacheConfig().getDnsConnectionTimeout(), config.getMemcachedCacheConfig().getHostStringParser(), config.getMemcachedCacheConfig().getHostResolver(), new FolsomReferencedClientFactory(config)), config.getMemcachedCacheConfig()); } + + @Override + public MemcachedClientFactory buildClientFactory(Object cfg) { + ElastiCacheCacheConfig config = (ElastiCacheCacheConfig) cfg; + + return new SpyMemcachedClientFactory(config.getMemcachedCacheConfig().getMemcachedHosts(), + config.getMemcachedCacheConfig().getDnsConnectionTimeout(), config.getMemcachedCacheConfig().getHostStringParser(), + config.getMemcachedCacheConfig().getHostResolver(), new FolsomReferencedClientFactory(config)); + } } \ No newline at end of file diff --git a/src/main/java/org/greencheek/caching/herdcache/memcached/SpyMemcachedCache.java b/src/main/java/org/greencheek/caching/herdcache/memcached/SpyMemcachedCache.java index 700918f..2da81f8 100644 --- a/src/main/java/org/greencheek/caching/herdcache/memcached/SpyMemcachedCache.java +++ b/src/main/java/org/greencheek/caching/herdcache/memcached/SpyMemcachedCache.java @@ -1,6 +1,8 @@ package org.greencheek.caching.herdcache.memcached; import org.greencheek.caching.herdcache.memcached.config.MemcachedCacheConfig; +import org.greencheek.caching.herdcache.memcached.factory.DynamicSpyMemcachedClientFactory; +import org.greencheek.caching.herdcache.memcached.factory.MemcachedClientFactory; import org.greencheek.caching.herdcache.memcached.factory.SpyMemcachedClientFactory; import org.greencheek.caching.herdcache.memcached.factory.SpyMemcachedReferencedClientFactory; @@ -11,8 +13,19 @@ */ public class SpyMemcachedCache extends BaseMemcachedCache { public SpyMemcachedCache(MemcachedCacheConfig config) { - super(new SpyMemcachedClientFactory(config.getMemcachedHosts(), - config.getDnsConnectionTimeout(),config.getHostStringParser(), - config.getHostResolver(),new SpyMemcachedReferencedClientFactory(createMemcachedConnectionFactory(config))), config); + super(config); + } + + public MemcachedClientFactory buildClientFactory(Object cfg) { + MemcachedCacheConfig config = (MemcachedCacheConfig)cfg; + if (config.resolveHostsFromDns()) { + return new DynamicSpyMemcachedClientFactory(config.getMemcachedHosts(), + config.getDurationForResolvingHostsFromDns(),config.getHostStringParser(), + new SpyMemcachedReferencedClientFactory(createMemcachedConnectionFactory(config))); + } else { + return new SpyMemcachedClientFactory(config.getMemcachedHosts(), + config.getDnsConnectionTimeout(),config.getHostStringParser(), + config.getHostResolver(),new SpyMemcachedReferencedClientFactory(createMemcachedConnectionFactory(config))); + } } }