Skip to content

Commit

Permalink
change EVCacheManager constructor, for inject EVCacheClientPoolManager
Browse files Browse the repository at this point in the history
  • Loading branch information
aafwu00 committed May 23, 2020
1 parent 9b932ae commit 6febe0c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.springframework.cloud.netflix.archaius.ConfigurableEnvironmentConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import static java.util.stream.Collectors.toList;

Expand All @@ -69,13 +68,14 @@ public CacheManagerCustomizers cacheManagerCustomizers(final ObjectProvider<Cach
}

@Bean
@DependsOn("evcacheClientPoolManager")
@ConditionalOnMissingBean
public EVCacheManager cacheManager(final CacheManagerCustomizers customizers,
final EVCacheClientPoolManager evcacheClientPoolManager,
final EVCacheProperties properties,
final ObjectProvider<EVCache.Builder.Customizer> builders,
final ObjectProvider<Transcoder<?>> transcoder) {
final EVCacheManager cacheManager = new EVCacheManager(properties.toConfigurations(),
final EVCacheManager cacheManager = new EVCacheManager(evcacheClientPoolManager,
properties.toConfigurations(),
builders.orderedStream().collect(toList()));
cacheManager.setAllowNullValues(properties.isAllowNullValues());
transcoder.ifAvailable(cacheManager::setTranscoder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.MyDataCenterInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.evcache.connection.ConnectionFactoryBuilder;
import com.netflix.evcache.connection.DIConnectionFactoryBuilderProvider;
import com.netflix.evcache.pool.EVCacheClientPoolManager;
import com.netflix.evcache.pool.SimpleNodeListProvider;
import com.netflix.evcache.pool.eureka.EurekaNodeListProvider;
import com.netflix.evcache.util.EVCacheConfig;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -166,7 +168,9 @@ EurekaClient eurekaClient() {
static class ExistsEVCacheClientPoolManagerConfiguration extends EnableCachingConfiguration {
@Bean
EVCacheClientPoolManager evcacheClientPoolManager() {
return mock(EVCacheClientPoolManager.class);
return new EVCacheClientPoolManager(new ConnectionFactoryBuilder(),
new SimpleNodeListProvider(),
EVCacheConfig.getInstance());
}
}

Expand Down
13 changes: 8 additions & 5 deletions evcache-client-spring/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ dependencies {
public class TodoApp {
@Bean
public CacheManager cacheManager() {
EVCacheConfig.getInstance()
.getDynamicStringProperty("TODO-NODES",
"shard1=localhost:11211,localhost:11212;shard2=localhost:11213,localhost:11214") # <2>
.get();
final EVCacheConfig config = EVCacheConfig.getInstance();
config.getPropertyRepository()
.get("TODO-NODES", String.class)
.orElseGet("shard1=localhost:11211,localhost:11212;shard2=localhost:11213,localhost:11214");
final EVCacheClientPoolManager evcacheClientPoolManager = new EVCacheClientPoolManager(new ConnectionFactoryBuilder(),
new SimpleNodeListProvider(),
config);
final EVCacheClientPoolConfigurationProperties properties = new EVCacheClientPoolConfigurationProperties();
properties.setKeyPrefix("todo");
properties.setTimeToLive(Duration.ofSeconds(10));
properties.setRetryEnabled(true);
properties.setExceptionThrowingEnabled(false);
final EVCacheConfiguration configuration = new EVCacheConfiguration("todos", "TODO", properties); # <1>
return new EVCacheManager(Collections.singleton(configuration));
return new EVCacheManager(evcacheClientPoolManager, Collections.singleton(configuration), Collections.emptyList());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,27 @@
package com.github.aafwu00.evcache.client.spring;

import com.netflix.evcache.EVCache.Builder;
import com.netflix.evcache.pool.EVCacheClientPoolManager;
import net.spy.memcached.transcoders.Transcoder;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.support.AbstractCacheManager;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.Collections.emptyList;

/**
* {@link CacheManager} backed by an {@link EVCacheImpl}.
*
* @author Taeho Kim
*/
public class EVCacheManager extends AbstractCacheManager {
private final EVCacheClientPoolManager evcacheClientPoolManager;
private final Set<EVCacheConfiguration> configurations;
private final List<Builder.Customizer> customizers;
/**
Expand All @@ -48,21 +50,23 @@ public class EVCacheManager extends AbstractCacheManager {
*/
private Transcoder<? extends Object> transcoder;

public EVCacheManager(final Set<EVCacheConfiguration> configurations) {
this(configurations, emptyList());
}

/**
* Create a new EVCacheManager for the given configurations and customizers
*
* @param configurations To be applied by for {@link Builder}`s {@link com.netflix.evcache.EVCacheClientPoolConfigurationProperties}
* @param customizers To be applied by for {@link Builder}`s customizer
* @param evcacheClientPoolManager To be applied by for {@link Builder}`s poolManager
* @param configurations To be applied by for {@link Builder}`s
* {@link com.netflix.evcache.EVCacheClientPoolConfigurationProperties}
* @param customizers To be applied by for {@link Builder}`s customizers
*/
public EVCacheManager(final Set<EVCacheConfiguration> configurations,
public EVCacheManager(final EVCacheClientPoolManager evcacheClientPoolManager,
final Set<EVCacheConfiguration> configurations,
final List<Builder.Customizer> customizers) {
super();
Assert.notNull(evcacheClientPoolManager, "`evcacheClientPoolManager` must not be null");
Assert.notNull(configurations, "`configurations` must not be null");
Assert.notEmpty(configurations, "`configurations` must not be empty");
Assert.notNull(customizers, "`customizers` must not be null");
this.evcacheClientPoolManager = evcacheClientPoolManager;
this.configurations = configurations;
this.customizers = customizers;
}
Expand All @@ -82,8 +86,11 @@ private EVCache create(final EVCacheConfiguration configuration) {
}

private com.netflix.evcache.EVCache build(final EVCacheConfiguration configuration) {
return Builder.forApp(configuration.getAppName())
.withConfigurationProperties(configuration.getProperties())
final Builder builder = Builder.forApp(configuration.getAppName());
final Field field = ReflectionUtils.findField(Builder.class, "_poolManager");
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, builder, evcacheClientPoolManager);
return builder.withConfigurationProperties(configuration.getProperties())
.addCustomizers(customizers)
.setTranscoder(transcoder)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import com.netflix.archaius.config.EmptyConfig;
import com.netflix.evcache.EVCache;
import com.netflix.evcache.EVCacheImpl;
import com.netflix.evcache.connection.ConnectionFactoryBuilder;
import com.netflix.evcache.pool.EVCacheClientPoolManager;
import com.netflix.evcache.pool.SimpleNodeListProvider;
import com.netflix.evcache.util.EVCacheConfig;
import org.junit.jupiter.api.Test;
import org.springframework.cache.Cache;
Expand Down Expand Up @@ -62,8 +65,11 @@ void loadCaches() {
configurations.add(configuration1);
configurations.add(configuration2);
final EVCache.Builder.Customizer customizer = mock(EVCache.Builder.Customizer.class);
new EVCacheConfig(DefaultPropertyFactory.from(EmptyConfig.INSTANCE));
final EVCacheManager manager = new EVCacheManager(configurations, singletonList(customizer));
final EVCacheConfig config = new EVCacheConfig(DefaultPropertyFactory.from(EmptyConfig.INSTANCE));
final EVCacheClientPoolManager evcacheClientPoolManager = new EVCacheClientPoolManager(new ConnectionFactoryBuilder(),
new SimpleNodeListProvider(),
config);
final EVCacheManager manager = new EVCacheManager(evcacheClientPoolManager, configurations, singletonList(customizer));
final List<? extends Cache> caches = new ArrayList<>(manager.loadCaches());
assertThatCache(getNativeCache(caches, 0), configuration1);
assertThatCache(getNativeCache(caches, 1), configuration2);
Expand Down
16 changes: 11 additions & 5 deletions samples/spring-sample/src/main/java/sample/TodoApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import com.github.aafwu00.evcache.client.spring.EVCacheConfiguration;
import com.github.aafwu00.evcache.client.spring.EVCacheManager;
import com.netflix.evcache.EVCacheClientPoolConfigurationProperties;
import com.netflix.evcache.connection.ConnectionFactoryBuilder;
import com.netflix.evcache.pool.EVCacheClientPoolManager;
import com.netflix.evcache.pool.SimpleNodeListProvider;
import com.netflix.evcache.util.EVCacheConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -52,16 +55,19 @@ public static void main(final String[] args) {

@Bean
public CacheManager cacheManager() {
EVCacheConfig.getInstance()
.getPropertyRepository()
.get("TODO-NODES", String.class)
.orElseGet("shard1=localhost:11211,localhost:11212;shard2=localhost:11213,localhost:11214");
final EVCacheConfig config = EVCacheConfig.getInstance();
config.getPropertyRepository()
.get("TODO-NODES", String.class)
.orElseGet("shard1=localhost:11211,localhost:11212;shard2=localhost:11213,localhost:11214");
final EVCacheClientPoolManager evcacheClientPoolManager = new EVCacheClientPoolManager(new ConnectionFactoryBuilder(),
new SimpleNodeListProvider(),
config);
final EVCacheClientPoolConfigurationProperties properties = new EVCacheClientPoolConfigurationProperties();
properties.setKeyPrefix("todo");
properties.setTimeToLive(Duration.ofSeconds(10));
properties.setRetryEnabled(true);
properties.setExceptionThrowingEnabled(false);
final EVCacheConfiguration configuration = new EVCacheConfiguration("todos", 1, "TODO", properties);
return new EVCacheManager(Collections.singleton(configuration));
return new EVCacheManager(evcacheClientPoolManager, Collections.singleton(configuration), Collections.emptyList());
}
}

0 comments on commit 6febe0c

Please sign in to comment.