import org.cloudfoundry.runtime.service.CloudPoolConfiguration;
import org.cloudfoundry.runtime.service.keyvalue.CloudRedisConnectionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
@Configuration
@ComponentScan(basePackages = { "..." })
@Order(1)
public class KeyValueConfig {
@Inject
private RedisConnectionFactory redisConnectionFactory;
@Bean
public StringRedisTemplate redisTemplate() {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(
redisConnectionFactory);
return stringRedisTemplate;
}
/**
* Properties to support the local and test mode of operation.
*/
@Configuration
@Profile({ Profiles.LOCAL, Profiles.TEST, Profiles.PROD })
static class Default {
@Inject
private Environment environment;
@Bean
public RedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory redis = new JedisConnectionFactory();
redis.setHostName(environment.getProperty("redis.hostname"));
redis.setPort(environment.getProperty("redis.port", Integer.class));
redis.setPassword(environment.getProperty("redis.password"));
redis.setUsePool(true);
return redis;
}
}
/**
* Properties to support the cloud mode of operation.
*/
@Configuration
@Profile(Profiles.CLOUDFOUNDRY)
static class Cloud {
@Bean
public RedisConnectionFactory redisConnectionFactory() throws Exception {
CloudPoolConfiguration cloudPoolConfiguration = new CloudPoolConfiguration();
cloudPoolConfiguration.setPoolSize("3-5");
CloudRedisConnectionFactoryBean factory = new CloudRedisConnectionFactoryBean();
factory.setCloudPoolConfiguration(cloudPoolConfiguration);
return factory.getObject();
}
}
}
And that's all!